This discussion is archived
13 Replies Latest reply: Oct 1, 2010 5:22 PM by 796367 RSS

How to get booleans out of a byte?

800670 Newbie
Currently Being Moderated
I have a ByteBuffer where the first byte contains some booleans. How can I get the booleans out of that byte?
  • 1. Re: How to get booleans out of a byte?
    jduprez Pro
    Currently Being Moderated
    You can use bitwise operators manually: http://download.oracle.com/javase/tutorial/java/nutsandbolts/op3.html
    e.g. if you want to know if bit 2 (value 4) is set:
    if (value&4==4) {...}
    Edited by: jduprez on Sep 29, 2010 3:20 PM
    Removed references to java.util.BitSet: I find no built-in method to stuff a byte's bits into a bit set, sigh...
  • 2. Re: How to get booleans out of a byte?
    796440 Guru
    Currently Being Moderated
    Roxxor wrote:
    I have a ByteBuffer where the first byte contains some booleans.
    No it doesn't. Perhaps you mean that you want to convert its bits to booleans, or interpret them as such?
  • 3. Re: How to get booleans out of a byte?
    796367 Explorer
    Currently Being Moderated
    Roxxor
    How can I get the booleans out of that byte?
    Any variable that is of type <tt>byte</tt> only holds values of type <tt>byte</tt>, period. With that said, how hard would it be to convert that value to an <tt>int</tt>, a <tt>char</tt>, or a <tt>String</tt> for that matter? And who can answer your question when you haven't told anyone what the criteria is for making a <tt>byte</tt> value TRUE or FALSE? All we can do is assume, and throw out techniques we have used in the past. In other words, you have to participate in this topic.
  • 4. Re: How to get booleans out of a byte?
    802316 Pro
    Currently Being Moderated
    Something you might find useful if you don't what your data looks like.
    byte b = ...
    // byte as a binary number.
    String binary = Integer.toString(b, 2);
  • 5. Re: How to get booleans out of a byte?
    800670 Newbie
    Currently Being Moderated
    jverd wrote:

    No it doesn't. Perhaps you mean that you want to convert its bits to booleans, or interpret them as such?
    Yes! I want the bits to be converted to booleans.
    And who can answer your question when you haven't told anyone what the criteria is for making a byte value TRUE or FALSE?
    My only criteria I have is that all 1-bits shall be booleans with the value of true, and all 0-bits shall be booleans with the value of false. Or did I misunderstand you question? :-S
  • 6. Re: How to get booleans out of a byte?
    jduprez Pro
    Currently Being Moderated
    Yes! I want the bits to be converted to booleans.
    And who can answer your question when you haven't told anyone what the criteria is for making a byte value TRUE or FALSE?
    My only criteria I have is that all 1-bits shall be booleans with the value of true, and all 0-bits shall be booleans with the value of false. Or did I misunderstand you question? :-S
    He probably meant that you could have had a convention that 1 byte value held one yes/no value (e.g. false=0 and true=anything else).

    Without any other info, I assumed your meant your bytes carry the 8 "boolean bits" of their 2-complement binary representation, which you can extract with the one of the techniques mentioned by me and the 4th post, and stuff into a java.util.bitset for example (or an instance of a custom class exposing meaningful getters, or a boolean array of size 8,...) for handier processing by the business logic.
  • 7. Re: How to get booleans out of a byte?
    800670 Newbie
    Currently Being Moderated
    jduprez wrote:
    You can use bitwise operators manually: http://download.oracle.com/javase/tutorial/java/nutsandbolts/op3.html
    e.g. if you want to know if bit 2 (value 4) is set:
    if (value&4==4) {...}
    Where does "value 4" come from?

    If I want to check if bit at index 5 is true or false, how is that done?
    bit       |1|0|0|0|1|0|1|0|
    index      0 1 2 3 4 5 6 7
    To use this code:
    byte b = ...
    // byte as a binary number.
    String binary = Integer.toString(b, 2);
    and then make the string to a boolean must be a very expensive and CPU consuming way to parse the bytes, right?
    Isn´t it any better way?
  • 8. Re: How to get booleans out of a byte?
    796440 Guru
    Currently Being Moderated
    Roxxor wrote:
    To use this code:
    byte b = ...
    // byte as a binary number.
    String binary = Integer.toString(b, 2);
    and then make the string to a boolean must be a very expensive and CPU consuming way to parse the bytes, right?
    No, don't do that.
    Isn´t it any better way?
    if ((b & (1 << (7 - N))) != 0) {
      // bit N is 1, by your bit numbering scheme
    }
  • 9. Re: How to get booleans out of a byte?
    796367 Explorer
    Currently Being Moderated
    bits:     8   7   6   5   4   3   2   1
    TRUE:   128  64  32  16   8   4   2   1
    FALSE:    0   0   0   0   0   0   0   0
    
    byte b = 17;
    
    binary:   0   0   0   1   0   0   0   1
    result:   F   F   F   T   F   F   F   T
    
    // Notice bit5 and bit1 (16 + 1 = 17).
    
    //check bit5
    //0 + 0 + 0 + 16 + 0 + 0 + 0 + 0
    System.out.println((b & 16) == 16); //true
    
    //check bit1
    //0 + 0 + 0 + 0 + 0 + 0 + 0 + 1
    System.out.println((b & 1) == 1); //true
    
    //check bit5 and bit1
    //0 + 0 + 0 + 16 + 0 + 0 + 0 + 1
    System.out.println((b & 17) == 17); //true
    
    //check bit5 and NOT bit1
    //0 + 0 + 0 + 16 + 0 + 0 + 0 + 0
    System.out.println((b & 17) == 16); //false
    
    //check NOT bit5 and bit1
    //0 + 0 + 0 + 0 + 0 + 0 + 0 + 1
    System.out.println((b & 17) == 1); //false
    
    //check NOT bit5 and NOT bit1
    //0 + 0 + 0 + 0 + 0 + 0 + 0 + 0
    System.out.println((b & 17) == 0); //false
  • 10. Re: How to get booleans out of a byte?
    796367 Explorer
    Currently Being Moderated
    jverd wrote:
    if ((b & (1 << (7 - N))) != 0) {
    // bit N is 1, by your bit numbering scheme
    }
    Actually, I think it's his indexing scheme.
  • 11. Re: How to get booleans out of a byte?
    jduprez Pro
    Currently Being Moderated
    You can use bitwise operators manually: http://download.oracle.com/javase/tutorial/java/nutsandbolts/op3.html
    e.g. if you want to know if bit 2 (value 4) is set:
    if (value&4==4) {...}
    Where does "value 4" come from?
    It's the decimal value of a byte whose "2" bit is set (1) and all other bits are 0.
    My "indexing" scheme is the exact contrary of yours, I think mine is more conventional (see this Wikipedia entry: [url http://en.wikipedia.org/wiki/Binary_numeral_system]binary numeral system): the idea is the same as in decimal, rightmost "figures" have lower value than their left neighbor (e.g in 12, the leftmost figure is 1, but it actually represents value 10).
    bit                .  .  .  .  .  .  .  . 
    index              7  6  5  4  3  2  1  0
    value (weight)   128 64 32 16  8  4  2  1
    The respective values for each of the 8 bits of a byte are, right to left, 1,2,4,8,16,32,64,128 (these are the power of 2; exactly as in base 10 the values of the positions are 1,10,100,1000,... as you go from right to left). The rule is: value(a.k.a. "weight")=2^index

    Note that the value 128 does not exist for a byte (because it is signed), but the bitwise operation would work anyway (as the operands are converted to 32 bits ints). See again Wikipedia on [url http://en.wikipedia.org/wiki/Two%27s_complement]Two's complement.
  • 12. Re: How to get booleans out of a byte?
    796440 Guru
    Currently Being Moderated
    pierrot_2 wrote:
    jverd wrote:
    if ((b & (1 << (7 - N))) != 0) {
    // bit N is 1, by your bit numbering scheme
    }
    Actually, I think it's his indexing scheme.
    Not sure what you're saying. He numbered the bits from 0 (high order) to 7 (low order). I followed that numbering scheme (or indexing scheme, if you prefer the term) when referring to "bit number N" in my solution.
  • 13. Re: How to get booleans out of a byte?
    796367 Explorer
    Currently Being Moderated
    jverd wrote:
    pierrot_2 wrote:
    jverd wrote:
    if ((b & (1 << (7 - N))) != 0) {
    // bit N is 1, by your bit numbering scheme
    }
    Actually, I think it's his indexing scheme.
    Not sure what you're saying. He numbered the bits from 0 (high order) to 7 (low order). I followed that numbering scheme (or indexing scheme, if you prefer the term) when referring to "bit number N" in my solution.
    Oh, the expression in your IF() is perfect... I was just commenting on your comment highlighted in green, where the N would be the number in Roxxor's indexing scheme as layed out on his post. I need another two pots of coffee.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points