Boost logo

Boost :

From: Gennaro Prota (gennaro_prota_at_[hidden])
Date: 2006-08-03 15:42:45


On Thu, 03 Aug 2006 16:59:25 +0200, Gennaro Prota
<gennaro_prota_at_[hidden]> wrote:

> For instance:
>
> // widely (not completely) portable
> void calculate()
> [...]

Sorry I was in a hurry when I wrote that. This is better:

 void calculate() // very meaningful name that I
                  // preferred to keep
 {
    typedef float T;
    typedef unsigned char byte;

    T plus_1( 1 );
    T minus_1( -1 );

    const volatile byte * b_plus
        = reinterpret_cast<const volatile byte *>(&plus_1);
    const volatile byte * b_minus
        = reinterpret_cast<const volatile byte *>(&minus_1);

    bool found = false;
    int pos = 0;
    byte mask = 0;

    for (int i = 0; i < sizeof( T ); ++i )
    {
        if ( b_plus[i] != b_minus[i] )
        {
            assert( !found );
            mask = b_plus[i] ^ b_minus[i];
            pos = i;
            found = true;

            assert( (mask & (mask-1)) == 0 );
        }
    }

    assert(found == true);

    std::cout << "sign bit at byte " << pos
              <<", mask is "
              << static_cast<unsigned>( mask ) << '\n'
        ;

 }

--
[ Gennaro Prota, C++ developer for hire ]

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk