Results 1 to 19 of 19
  1. #1
    Aaron_Sharp is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default 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. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    See the API docs for the Integer class.

  3. #3
    Aaron_Sharp is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    What does the Integer class have to do with converting a byte into an array of bits?

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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. #5
    Aaron_Sharp is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    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. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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. #7
    JavaHater is offline Senior Member
    Join Date
    Dec 2010
    Posts
    165
    Rep Power
    4

    Default

    you should instead look at classes like ByteBuffer, ShortBuffer, BitSet etc.

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by JavaHater View Post
    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. #9
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Red face

    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 Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default

    Why should it give leading zeroes when it doesn't know how many bits to pad to?

  11. #11
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by goldest View Post
    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. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    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
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default

    Quote Originally Posted by JosAH View Post
    What about good old bit fiddling?
    Isn't that illegal in public?

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    Isn't that illegal in public?
    Duh, I bet Adam and Eve already fiddled with their bits in public.

    kindest regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    Aaron_Sharp is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

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

    Thank you in advance.

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

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

    Thank you in advance.
    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
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    Aaron_Sharp is offline Member
    Join Date
    Jan 2011
    Posts
    7
    Rep Power
    0

    Default

    thank you JosAH! Your solution was marvelous!

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default

    Quote Originally Posted by JosAH View Post
    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. #19
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    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 ;-)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Converting string to byte[]
    By bobo67 in forum New To Java
    Replies: 12
    Last Post: 09-10-2010, 09:10 PM
  2. converting byte array to bmp file
    By Moorkh in forum New To Java
    Replies: 2
    Last Post: 09-07-2010, 02:58 PM
  3. Need help converting int to a 4 byte array
    By kook04 in forum Advanced Java
    Replies: 5
    Last Post: 02-26-2010, 08:59 PM
  4. Converting a byte[] into Sound
    By savage82 in forum Advanced Java
    Replies: 1
    Last Post: 11-21-2007, 11:57 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •