1

I have an array of 8 booleans which I want to simply convert to a byte. Is there a simple way to do this? Or do I have to use for loop?

Personally I'd prefer a simple up to two lines solution if it exists.

Thanks for help.

EDIT: Possible duplicate is just one boolean to a byte, I have an array.

ANOTHER EDIT: I get a byte from a udp packet then I set the first bit (boolean) to false, then I would need to get a byte out of that again.

5
  • possible duplicate of convert from boolean to byte in java Commented Aug 17, 2015 at 10:28
  • 1
    That question is just one boolean to byte, I have a whole array. Commented Aug 17, 2015 at 10:29
  • So iterate over array. We might be more helpful if you tell us what have you tried so far. Commented Aug 17, 2015 at 10:30
  • Yes, that's a way, but I'm looking for a simpler solution if it exists. Commented Aug 17, 2015 at 10:31
  • 1
    oneliner solution is to make method that converts boolean array to byte array - method will take something like 6 to 10 lines, but then you will use it just like byte[] arr = booleanToByteArray(booleanArrayHere); Commented Aug 17, 2015 at 10:41

3 Answers 3

8

I think a loop is better, but if you must have a one liner :

byte b = (byte)((bool[0]?1<<7:0) + (bool[1]?1<<6:0) + (bool[2]?1<<5:0) + 
                (bool[3]?1<<4:0) + (bool[4]?1<<3:0) + (bool[5]?1<<2:0) + 
                (bool[6]?1<<1:0) + (bool[7]?1:0));

For the input :

boolean[] bool = new boolean[] {false,false,true,false,true,false,true,false};

you get the byte 42.

Sign up to request clarification or add additional context in comments.

2 Comments

Is the last bool[1] there for a reason or should it be bool[7]?
@grizeldi Thanks, that was a copy and paste error :)
3

if i understood your question correct, then you want to convert a byte that is represented by it's bit pattern as an array of booleans ... if that is the case then this is one solution :

 public static void main(String[] args) {
        // The integer where the result will be build up
        int result = 0;
        // The bit-pattern as an array of booleans
        // 2^0 Position is at bools[0] !!
        // so this is the byte '0 1 0 1 0 1 0 1' --> decimal : 85
        boolean[] bools = new boolean[]{ true, false, true, false, true, false, true, false };
        // iterate through the 'bits'
        // and set the corresponding position in the result to '1' if the 'bit' is set
        for (int i = 0; i < bools.length; i++) {
            if (bools[i]) {
                result |= 1 << i;
            }
        }
        System.out.println(result);  // Prints '85'
    }

1 Comment

Yes I wanted something like that, but @Eran's oneliner is what I'm using now.
0

I'd do it like this:

byte b = 0;
for(int i=0;i<8;i++) if(binaryValues[i]) b |= (128 >> i);
return b;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.