Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Reversing Bit Order.. i.e. MSB becomes bit 0, LSB becomes bit 15 (http://www.velocityreviews.com/forums/t530065-reversing-bit-order-i-e-msb-becomes-bit-0-lsb-becomes-bit-15-a.html)

 benn686@hotmail.com 08-18-2007 12:36 AM

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

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

 int2str@gmail.com 08-18-2007 01:03 AM

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

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

htons() <-> ntohs()

 Ian Collins 08-18-2007 02:32 AM

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

int2str@gmail.com wrote:
> On Aug 17, 5:36 pm, benn...@hotmail.com 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 08-18-2007 02:34 AM

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

benn686@hotmail.com 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,

--
Ian Collins.

 BobR 08-18-2007 02:44 AM

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

<benn686@hotmail.com> 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

 Ron Natalie 08-18-2007 11:21 AM

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

Ian Collins wrote:
> int2str@gmail.com wrote:
>> On Aug 17, 5:36 pm, benn...@hotmail.com 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.

 Juha Nieminen 08-18-2007 04:06 PM

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

benn686@hotmail.com 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) ;

 benn686@hotmail.com 08-20-2007 09:55 PM

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

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:
> benn686@hotmail.com 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) ;

 hurcan solter 08-20-2007 11:52 PM

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

On Aug 21, 12:55 am, benn...@hotmail.com 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;
}

 Juha Nieminen 08-22-2007 12:13 AM

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

benn686@hotmail.com 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) ;