# Converting a byte to individual bits

• 01-05-2011, 12:38 PM
Aaron_Sharp
Converting a byte to individual bits
Dear community,

I have been asked to implement a CRC from scratch. From what I have learnt the CRC basically works by performing a series of XOR operations on the ploynomial, bit by bit. I believe that in order to work the method I need to convert a byte into bits first.

The CRC is none of my concern, I know the algorithm. But I have no idea how to convert a byte into an array of individual bits. I know for a fact that every byte is in actuality a series of eight bits... For example 0010 0011. But seeing as the smallest JAVA data type is byte and it has no methods to convert it to a smaller type.. I am now stuck.

Can anyone help me? :(
• 01-05-2011, 12:51 PM
See the API docs for the Integer class.
• 01-05-2011, 01:11 PM
Aaron_Sharp
What does the Integer class have to do with converting a byte into an array of bits?
• 01-05-2011, 01:23 PM
Why don't you take a look at it, see if there is a mthod there that looks promising, and try a few things.
• 01-05-2011, 01:36 PM
Aaron_Sharp
If you are implying that I'm a lazy boy trying to get other people to do my work, you can rest easy for that is not my intention. :) I have in fact looked at the Integer class. Seeing as what I need is to convert a byte into bits, I don't think my solution can be found there. I'd like to post code but unfortunately I don't really know where to start. Can you explain to me if the BitSet class can be used to help? I'm reading about it right now. It's documentation states that:

"This class implements a vector of bits that grows as needed. Each component of the bit set has a boolean value. The bits of a BitSet are indexed by nonnegative integers. Individual indexed bits can be examined, set, or cleared. One BitSet may be used to modify the contents of another BitSet through logical AND, logical inclusive OR, and logical exclusive OR operations."

I suppose if there's any way to set the bits in such a way that it translates to the byte's value, my problem would be solved. But I don't know how due to lack of knowledge. If for example I have:

byte b = 3;

Is there anyway to access it bit by bit?
• 01-05-2011, 01:39 PM
toBinaryString (from the Integer class, of course) to get the 0s and 1s, maybe? And of course BitSet can be used, once you know what bits to set (with which toBinaryString may help) and then you can compare using booleans rather than charAt.
• 01-05-2011, 01:39 PM
JavaHater
you should instead look at classes like ByteBuffer, ShortBuffer, BitSet etc.
• 01-05-2011, 01:41 PM
Quote:

Originally Posted by JavaHater
you should instead look at classes like ByteBuffer, ShortBuffer, BitSet etc.

I have not a clue what the Buffer classes have to do with it.
• 01-05-2011, 01:46 PM
goldest
toBinaryString would be the right method to fetch the bits. But it doesn't give the leading 0's. Thats weird.

Code:

`System.out.println(Integer.toBinaryString(10));`
Would give you 1010, where it should be 0000 1010

I don't know if thats fine with OP.

Goldest
• 01-05-2011, 02:41 PM
Tolls
Why should it give leading zeroes when it doesn't know how many bits to pad to?
• 01-05-2011, 02:59 PM
Quote:

Originally Posted by goldest
toBinaryString would be the right method to fetch the bits. But it doesn't give the leading 0's. Thats weird.

Code:

`System.out.println(Integer.toBinaryString(10));`
Would give you 1010, where it should be 0000 1010

I don't know if thats fine with OP.

Goldest

Code:

```String temp = "00000000" + Integer.toBinaryString(10); System.out.println(temp.substring(temp.length() - 8));```
And?
• 01-05-2011, 03:26 PM
JosAH
You're all a bunch of sissies: creating a a String to fetch the individual bits, tsss ... ;-)

What about good old bit fiddling?

Code:

```byte b= ...; for (byte m= 1; m != 0; m<<= 1) {   int bit= ((b&m) != 0)?1:0;   // do something with 'bit' }```
When the world was ruled by vaxen and real programmers were real men ...

kind regards,

Jos
• 01-05-2011, 04:00 PM
Tolls
Quote:

Originally Posted by JosAH
What about good old bit fiddling?

Isn't that illegal in public?
• 01-05-2011, 04:03 PM
JosAH
Quote:

Originally Posted by Tolls
Isn't that illegal in public?

Duh, I bet Adam and Eve already fiddled with their bits in public.

kindest regards,

Jos ;-)
• 01-05-2011, 08:52 PM
Aaron_Sharp
K guys I'm gonna start trying the solutions you suggested, I'll post back once I have it all sorted.

• 01-05-2011, 09:03 PM
JosAH
Quote:

Originally Posted by Aaron_Sharp
K guys I'm gonna start trying the solutions you suggested, I'll post back once I have it all sorted.

You're welcome of course and remember, if you're going to use the solution sketched by Tolls and Masijade you have to stand in the corner where all sissies belong. Real men use bit fiddling ;-)

kind regards,

Jos
• 01-05-2011, 09:37 PM
Aaron_Sharp
thank you JosAH! Your solution was marvelous!
• 01-06-2011, 09:40 AM
Tolls
Quote:

Originally Posted by JosAH
You're welcome of course and remember, if you're going to use the solution sketched by Tolls and Masijade you have to stand in the corner where all sissies belong. Real men use bit fiddling ;-)

kind regards,

Jos

Excuuuuuuse me!
I suggested nothing.

Well, apart from some childish sniggering about bit fiddling...
• 01-06-2011, 10:09 AM
JosAH
Quote:

Originally Posted by Tolls
Excuuuuuuse me!
I suggested nothing.

Well, apart from some childish sniggering about bit fiddling...

See? Need I say more? Real men (like me) don't mind fiddling bits (preferably near the campfire) while types like you (and Masijade) (both sissies) don't mind allocating sixteen bits for one single bit. If everyone were like you both, we'd all be still digging mud while real men (like me) can squeeze more than one single bit in a single bit; real men (like me) write entire chess programs in fifteen bits! So there.

kindest regards,

Jos ;-)