# Thread: Converting a byte to individual bits

1. Member
Join Date
Jan 2011
Posts
7
Rep Power
0

## 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? :(

2. Senior Member
Join Date
Jun 2008
Posts
2,565
Rep Power
8
See the API docs for the Integer class.

3. Member
Join Date
Jan 2011
Posts
7
Rep Power
0
What does the Integer class have to do with converting a byte into an array of bits?

4. Senior Member
Join Date
Jun 2008
Posts
2,565
Rep Power
8
Why don't you take a look at it, see if there is a mthod there that looks promising, and try a few things.

5. Member
Join Date
Jan 2011
Posts
7
Rep Power
0
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?

6. Senior Member
Join Date
Jun 2008
Posts
2,565
Rep Power
8
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.

7. Senior Member
Join Date
Dec 2010
Posts
165
Rep Power
4
you should instead look at classes like ByteBuffer, ShortBuffer, BitSet etc.

8. Senior Member
Join Date
Jun 2008
Posts
2,565
Rep Power
8
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.

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

Java 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

10. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18
Why should it give leading zeroes when it doesn't know how many bits to pad to?

11. Senior Member
Join Date
Jun 2008
Posts
2,565
Rep Power
8
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.

Java 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
Java Code:
```String temp = "00000000" + Integer.toBinaryString(10);
System.out.println(temp.substring(temp.length() - 8));```
And?

12. You're all a bunch of sissies: creating a a String to fetch the individual bits, tsss ... ;-)

What about good old bit fiddling?

Java 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

13. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18
Originally Posted by JosAH
What about good old bit fiddling?
Isn't that illegal in public?

14. 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 ;-)

15. Member
Join Date
Jan 2011
Posts
7
Rep Power
0
K guys I'm gonna start trying the solutions you suggested, I'll post back once I have it all sorted.

16. 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

17. Member
Join Date
Jan 2011
Posts
7
Rep Power
0
thank you JosAH! Your solution was marvelous!

18. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18
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...

19. 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 ;-)