Reversing Bit Order.. i.e. MSB becomes bit 0, LSB becomes bit 15

Discussion in 'C++' started by benn686@hotmail.com, Aug 18, 2007.

  1. Guest

    Anyone know of an efficient way of reversing the bits of a word??
     
    , Aug 18, 2007
    #1
    1. Advertisements

  2. Guest

    On Aug 17, 5:36 pm, wrote:
    > Anyone know of an efficient way of reversing the bits of a word??


    htons() <-> ntohs()
     
    , Aug 18, 2007
    #2
    1. Advertisements

  3. Ian Collins Guest

    Re: Reversing Bit Order.. i.e. MSB becomes bit 0, LSB becomes bit15

    wrote:
    > On Aug 17, 5:36 pm, wrote:
    >> Anyone know of an efficient way of reversing the bits of a word??

    >
    > htons() <-> ntohs()
    >

    Nope, where these are available, they reverse the byte order.

    --
    Ian Collins.
     
    Ian Collins, Aug 18, 2007
    #3
  4. Ian Collins Guest

    Re: Reversing Bit Order.. i.e. MSB becomes bit 0, LSB becomes bit15

    wrote:
    > Anyone know of an efficient way of reversing the bits of a word??
    >

    Not portably, some instructions sets may have a quick way to do this,
    otherwise shift and add.

    --
    Ian Collins.
     
    Ian Collins, Aug 18, 2007
    #4
  5. BobR Guest

    <> wrote in message...
    > Anyone know of an efficient way of reversing the bits of a word??
    >


    { using std::cout; // for NG post
    size_t num( 12345 );
    // std::bitset<32> Bits( num ); // #include <bitset>
    std::bitset<std::numeric_limits<size_t>::digits> Bits( num );

    cout<<"size_t num="<<num<<std::endl;
    cout<<"bitset<32> Bits="<<Bits<<std::endl;
    Bits.flip();
    cout<<"bitset<32> Bits="<<Bits<<std::endl;
    num = Bits.to_ulong(); // be careful with sizes.
    cout<<"size_t num="<<num<<std::endl;

    size_t num2( 12345 );
    cout<<"size_t num2="<<num2<<std::endl;
    num2 = ~num2;
    cout<<"size_t num2="<<num2<<std::endl;
    }
    /* -output-
    size_t num=12345
    bitset<32> Bits=00000000000000000011000000111001
    bitset<32> Bits=11111111111111111100111111000110
    size_t num=4294954950
    size_t num2=12345
    size_t num2=4294954950
    */

    --
    Bob R
    POVrookie
     
    BobR, Aug 18, 2007
    #5
  6. Ron Natalie Guest

    Re: Reversing Bit Order.. i.e. MSB becomes bit 0, LSB becomes bit15

    Ian Collins wrote:
    > wrote:
    >> On Aug 17, 5:36 pm, wrote:
    >>> Anyone know of an efficient way of reversing the bits of a word??

    >> htons() <-> ntohs()
    >>

    > Nope, where these are available, they reverse the byte order.
    >

    Where they are available they convert the native byte order to
    the network (big end) ordering. Whether it is a swap or not
    depends whether the machine is already in the network order.
     
    Ron Natalie, Aug 18, 2007
    #6
  7. Re: Reversing Bit Order.. i.e. MSB becomes bit 0, LSB becomes bit15

    wrote:
    > Anyone know of an efficient way of reversing the bits of a word??


    Reverse all the bits in a 32 bit word:

    n = ((n >> 1) & 0x55555555) | ((n << 1) & 0xaaaaaaaa) ;
    n = ((n >> 2) & 0x33333333) | ((n << 2) & 0xcccccccc) ;
    n = ((n >> 4) & 0x0f0f0f0f) | ((n << 4) & 0xf0f0f0f0) ;
    n = ((n >> 8) & 0x00ff00ff) | ((n << 8) & 0xff00ff00) ;
    n = ((n >> 16) & 0x0000ffff) | ((n << 16) & 0xffff0000) ;
     
    Juha Nieminen, Aug 18, 2007
    #7
  8. Guest

    Wow! That is slick, and it worked nicely too!

    Not quite sure how it works, but for 16 bits, would it work something
    like:

    n = ((n >> 1) & 0x5555) | ((n << 1) & 0xaaaa) ;
    n = ((n >> 2) & 0x3333) | ((n << 2) & 0xcccc) ;
    n = ((n >> 4) & 0x0f0f) | ((n << 4) & 0x0f0f) ;


    Juha Nieminen wrote:
    > wrote:
    > > Anyone know of an efficient way of reversing the bits of a word??

    >
    > Reverse all the bits in a 32 bit word:
    >
    > n = ((n >> 1) & 0x55555555) | ((n << 1) & 0xaaaaaaaa) ;
    > n = ((n >> 2) & 0x33333333) | ((n << 2) & 0xcccccccc) ;
    > n = ((n >> 4) & 0x0f0f0f0f) | ((n << 4) & 0xf0f0f0f0) ;
    > n = ((n >> 8) & 0x00ff00ff) | ((n << 8) & 0xff00ff00) ;
    > n = ((n >> 16) & 0x0000ffff) | ((n << 16) & 0xffff0000) ;
     
    , Aug 20, 2007
    #8
  9. On Aug 21, 12:55 am, wrote:
    > Wow! That is slick, and it worked nicely too!
    >
    > Not quite sure how it works, but for 16 bits, would it work something
    > like:
    >
    > n = ((n >> 1) & 0x5555) | ((n << 1) & 0xaaaa) ;
    > n = ((n >> 2) & 0x3333) | ((n << 2) & 0xcccc) ;
    > n = ((n >> 4) & 0x0f0f) | ((n << 4) & 0x0f0f) ;
    >


    Textbook example; and please dont top-post...
    template<typename T>
    T reverseBits(T inval){

    T retval=0;
    numberofbits= sizeof(T) * CHAR_BITS;
    for ( int i = 0; i < numberofbits; ++i ) {
    retval = ( retval << 1 ) | ( inval & 1 );
    inval >>= 1;
    }
    return retval;
    }
     
    hurcan solter, Aug 21, 2007
    #9
  10. Re: Reversing Bit Order.. i.e. MSB becomes bit 0, LSB becomes bit15

    wrote:
    > Wow! That is slick, and it worked nicely too!
    >
    > Not quite sure how it works, but for 16 bits, would it work something
    > like:
    >
    > n = ((n >> 1) & 0x5555) | ((n << 1) & 0xaaaa) ;
    > n = ((n >> 2) & 0x3333) | ((n << 2) & 0xcccc) ;
    > n = ((n >> 4) & 0x0f0f) | ((n << 4) & 0x0f0f) ;


    I believe it additionally needs:

    n = ((n >> 8) & 0x00ff) | ((n << 8) & 0xff00) ;
     
    Juha Nieminen, Aug 22, 2007
    #10
    1. Advertisements

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. Dr. David Kirkby

    Reversing the order of some loops.

    Dr. David Kirkby, Oct 22, 2003, in forum: C Programming
    Replies:
    4
    Views:
    523
    Glen Herrmannsfeldt
    Oct 26, 2003
  2. flipflop

    Reversing order of quicksort

    flipflop, May 28, 2004, in forum: C Programming
    Replies:
    3
    Views:
    452
    Martin Ambuhl
    May 28, 2004
  3. flipflop

    Reversing order of quicksort

    flipflop, May 28, 2004, in forum: C Programming
    Replies:
    2
    Views:
    502
    Robert Wessel
    May 30, 2004
  4. Marcin Ciura
    Replies:
    27
    Views:
    792
    Terry Reedy
    Mar 22, 2007
  5. Kelly B

    Reversing order of words in a given string

    Kelly B, Apr 26, 2007, in forum: C Programming
    Replies:
    2
    Views:
    442
    Kelly B
    Apr 26, 2007
  6. LSB aligned data

    , Apr 18, 2008, in forum: C++
    Replies:
    3
    Views:
    579
    James Kanze
    Apr 18, 2008
  7. Francis Moreau

    Portable way to mask the LSB

    Francis Moreau, May 29, 2010, in forum: C Programming
    Replies:
    7
    Views:
    1,004
    Keith Thompson
    May 31, 2010
  8. Stef Mientki

    and becomes or and or becomes and

    Stef Mientki, May 22, 2011, in forum: Python
    Replies:
    9
    Views:
    552
    Chris Angelico
    May 28, 2011
Loading...