1

I have an integer number withing an interval [0,7]. And I want to convert this number into a Boolean list with 3 elements with Java.

1st element in the list has a value of 4, 2nd element has 2 and 3rd element is 1. Their sum makes 7 if all the elements in the list are "true". "False" has a value of 0.

Here are the all possibilities:

If the number is 7 my boolean list is [true, true, true]

If the number is 6 my boolean list is [true, true, false]

If the number is 5 my boolean list is [true, false, true]

If the number is 4 my boolean list is [true, false, false]

If the number is 3 my boolean list is [false, true, true]

If the number is 2 my boolean list is [false, true, false]

If the number is 1 my boolean list is [false, false, true]

If the number is 0 my boolean list is [false, false, false]

I don't want to code this with 8 else if blocks. I think there must be a smarter solution with combinations of the numbers.

Here is my function declaration: default List<Boolean> convertIntToBooleanList(int i);

Do you have any ideas how can I solve it without hardcoding?

Thanks!

firefighter
  • 171
  • 1
  • 14

3 Answers3

2

You need to convert your number to base 2, then, for each bit, set the corresponding value in the array to true if the bit is 1.
Example:

7 = 111 = [true, true, true]
5 = 101 = [true, false, true]
4 = 100 = [true, false, false]

Basic Java implementation (not well versed with Java, so the solution might be made more optimal):

public static void main (String[] args) throws java.lang.Exception
    {
        Integer x = 3;
        String binary = String.format("%3s", Integer.toBinaryString(x));
        List<Boolean> list = new ArrayList<Boolean>();
        for (int i = 0; i < binary.length(); i++) {
            list.add(binary.charAt(i) == '1');
        }
        System.out.println(list);
    }
Abhinav Mathur
  • 7,791
  • 3
  • 10
  • 24
1

You can use number&1 to confirm the first bit is 1 or 0, and use >> to shift each bit to the first.

public static List<Boolean> convertIntToBooleanList(int i) {
    List<Boolean> list = new ArrayList<>();
    list.add((i>>2&1)!=0);
    list.add((i>>1&1)!=0);
    list.add((i&1)!=0);
    return list;
}
banzhe
  • 651
  • 4
  • 6
  • Thanks a lot for the answer and clear explanation! Actually I preferred to use this answer because it doesn't have any for loops and its much shorter. – firefighter Apr 07 '22 at 04:23
1

If you want to convert to binary using your own method, you can repeatedly divide by 2 keeping the remainders in reverse order:

  public static void main(String[] args) {   
    for(int i=0; i<=7; i++) {
      List<Boolean> bits = DecToBin(i, 3);
      System.out.println(i + " --> " + bits);
    }    
  }

  public static List<Boolean> DecToBin(int d, int padLength) {
    List<Boolean> bits = new ArrayList<Boolean>();
    while(d>0) {
      bits.add(0, ((d % 2) == 1)); // insert remainder at front
      d = d / 2; // integer division
    }
    while (bits.size() < padLength) {
      bits.add(0, false);
    }
    return bits;
  }

Output:

0 --> [false, false, false]
1 --> [false, false, true]
2 --> [false, true, false]
3 --> [false, true, true]
4 --> [true, false, false]
5 --> [true, false, true]
6 --> [true, true, false]
7 --> [true, true, true]
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40