Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Reverse Bit Operations

Reverse Bit Operations

Reverse Bit Operations

I am looking for a C++ function to reverse the bits of a variable. For example, assume testvar = 11100110, then reverse(testvar) = 01100111. Does anyone know of such a function? Thanks.

RE: Reverse Bit Operations

I should've mentioned that testvar is represented as a binary above, i.e. testvar = 11100110, and reverse(testvar) = 01100111.

RE: Reverse Bit Operations

Sorry, I messed up again: testvar = 11100110b, and reverse(testvar) = 01100111b

RE: Reverse Bit Operations

I also meant C, not C++... sorry again

RE: Reverse Bit Operations

Of course, no such library function but there are full set of bitwise operators in C and C++: <<, >>, &, |, ^, ~ (+ loop statements;).
You may use more sofisticated approach with byte by byte substitution too.
Alas, no binary constants with b suffix in C++...

RE: Reverse Bit Operations

If you need to do this very fast for any reason, it would be worth making a 256-byte lookup table with the results already reversed. Even if you're reversing much larger numbers than 8 bits, the look-up would pay off as you can reverse the bytes of a big number very easily, and then reverse the bits in each byte using the look-up. Reversing bits by repeated shifts and tests is quite a lot of cycles per byte.

RE: Reverse Bit Operations

Isn't there also an assembly instruction on some chips that reverses bits?  Is so, you could use that (as long as you don't care about multi-platformedness).

RE: Reverse Bit Operations

There wasn't in the 8086 family up to the pentium. I don't know if there's something whizzy since then. There were bscan instructions, forwards and backwards, that found the position of a bit in a register, but they were incredibly, outrageously slow; basically they were a repeated 1-bit shift and test, and ran just about as efficiently.

RE: Reverse Bit Operations

Well, look at this (portable - 16..24..32..64..;) monster:


static unsigned int rb[256] = /* uint contains 8*n bits */
/* this routine ignores byte structure (and addressing scheme) */
unsigned int revbits(unsigned int u)
  int    i, j, k;
  unsigned int a, b;
  unsigned int r = 0;

  static int n;
  static unsigned int uu = 0x100u;

  /* octets per unsigned int?
   it's interesting: can't calc with preprocessor!

  if (uu) /* the 1st call... */
  for (n = 2; uu <<= 8; ++n) /* not thread-safe code! */
  k = n;

  for (i = 0, j = 8*(n-1); k > 1; k -= 2, i += 8, j -=8)
    a = rb[(u >> i)&0xFFu];
    b = rb[(u >> j)&0xFFu];
    r |= (b << i);
    r |= (a << j); /* next two reverted octets swapped */
  if (k)    /* (now exotic system with odd num of octets per uint;) */
     r |= (rb[(u >> i)&0xFFu] << i);
  return r;

RE: Reverse Bit Operations

The obfuscated answers are truly remarkable but I'd love to see their execution times compared to simple look-ups and a well-placed bswap. Multiplication! Good gracious.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close