Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to reverse bits (big endian to little endian)?

800306Jul 27 2007 — edited Jul 27 2007

Hi,

I have a problem with some bits. I have two bytes (surprise surprise) with 12 bits (they are padded with four zeros). The bytes SHOULD look like this (x and n represent bits):

| x n x n  x n x n | x n x n  0 0 0 0 |

However, they are coming in as such:

| n x n x  n x n x | 0 0 0 0  n x n x |

Obviously, this is backwards. I am trying to figure out how to basically reverse the bits so I can manipulate them correctly. I've tried looking at two's complement, bit shifting, bitwise operations, etc., and I don't know how to do this. I found an external library that apparently can do this, but at this point, I'm just dying to know how the algorithm works; I don't just want a reverse() method.

Can anyone offer any advice/tutorials/whatever?

Thanks

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 24 2007
Added on Jul 27 2007
9 comments
4,676 views