printing double in binary

Discussion in 'C++' started by bauran, May 2, 2010.

  1. bauran

    bauran Guest

    In Thinking in C++ by Bruce eckel, there is a program given to print a
    double value
    in binary.(Chapter 3, page no. 189)

    int main(int argc, char* argv[])
    {
    if(argc != 2)
    {
    cout << "Must provide a number" << endl;
    exit(1);
    }
    double d = atof(argv[1]);
    unsigned char* cp = reinterpret_cast<unsigned char*>(&d);
    for(int i = sizeof(double); i > 0 ; i -= 2)
    {
    printBinary(cp[i-1]);
    printBinary(cp);
    }
    }

    assume printbinary() prints (const unsigned char) in binary i.e. 8
    bits. Moreover I haven't provided header files to shorten code. Please
    assume proper header files and namespaces are included.

    Here while printing cp when i=8(assuming double is of 8 bytes),
    wouldn't it be undefined behaviour?
    I mean this code doesn't work as it doesn't print cp[0].
    Bruce Eckel is a very renowned writer. How can he do this type of
    mistake?
    bauran, May 2, 2010
    #1
    1. Advertising

  2. bauran

    James Kanze Guest

    On May 2, 7:15 pm, bauran <> wrote:
    > In Thinking in C++ by Bruce eckel, there is a program given to
    > print a double value in binary.(Chapter 3, page no. 189)


    > int main(int argc, char* argv[])
    > {
    > if(argc != 2)
    > {
    > cout << "Must provide a number" << endl;
    > exit(1);
    > }
    > double d = atof(argv[1]);
    > unsigned char* cp = reinterpret_cast<unsigned char*>(&d);
    > for(int i = sizeof(double); i > 0 ; i -= 2)
    > {
    > printBinary(cp[i-1]);
    > printBinary(cp);
    > }
    > }


    > assume printbinary() prints (const unsigned char) in binary
    > i.e. 8 bits. Moreover I haven't provided header files to
    > shorten code. Please assume proper header files and namespaces
    > are included.


    > Here while printing cp when i=8(assuming double is of 8
    > bytes), wouldn't it be undefined behaviour?


    Maybe. If sizeof double is 8, it's undefined behavior.

    I'm not too sure what this code is supposed to do. Printing a
    hexadecimal dump of objects is pretty standard, and usually done
    with something like:

    template<typename T>
    void
    dump( std::eek:stream& dest, T const& obj )
    {
    IOSave state( dest );
    dest.setf( std::hex, std::base_field);
    dest.fill( '0' );
    unsigned char const* p = reinterpret_cast<unsigned char const
    *>( &obj );
    for ( int i = 0; i != sizeof(T); ++ i) {
    if ( i != 0 ) {
    dest << ' ';
    }
    dest << std::setw(2) << p[ i ];
    }
    }

    > I mean this code doesn't work as it doesn't print cp[0].
    > Bruce Eckel is a very renowned writer. How can he do this type
    > of mistake?


    I've yet to meet a human being who didn't make mistakes.

    --
    James Kanze
    James Kanze, May 2, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Web learner

    from List <double> to double[]

    Web learner, Apr 25, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    462
  2. sb
    Replies:
    4
    Views:
    297
    Alberto Barbati
    Feb 19, 2004
  3. Jacek Dziedzic
    Replies:
    5
    Views:
    370
    Old Wolf
    Apr 8, 2004
  4. ferran
    Replies:
    9
    Views:
    3,006
    Kevin Goodsell
    Apr 12, 2004
  5. Sydex
    Replies:
    12
    Views:
    6,452
    Victor Bazarov
    Feb 17, 2005
Loading...

Share This Page