Hi,
This is my first post here. I have have some code that prints out the possible 2-set combinations of numbers in a list, with some numbers on the right side and the rest of the numbers on the left side. However, I have been studying it for quite a while and I do not understand how it is doing what it is doing. Specifically, I do not understand the function of the bitwise right-shift and bitwise AND operators in the for loops. (In fact, this is the first time I have seen bitwise operators used in programming, so please forgive my ignorance).
I see how the bitwise left-shift operator is functioning to make the outer loop execute a certain number of times (which would determine how many combinations of numbers there would be, which would in turn be determined by the size of the original list, i.e. a list of length five would produce 2^5 - 2 combinations, because we have to have at least one number on each side of the list), but I do not understand how the right-shift and AND segment is working. It seems that it must be what determines which new list each number is in, but how? I do not understand what is happening here and would really appreciate any guidance you could give.
Thanks.
public class SomeNumbers { public static void main(String[] args) { int[] list = {1, 7, 6, 8, 9}; for (int i = 1; i < (1 << list.length) - 1; i++) { System.out.print("("); for (int j = 0; j < list.length; j++) { if (((i >> j) & 1) == 1) { System.out.print(list[j] + " "); } } System.out.print(") ("); for (int j = 0; j < list.length; j++) { if (((i >> j) & 1) == 0) { System.out.print(list[j] + " "); } } System.out.println(")"); } } }