What is it when a pipe is used in print statement in java? For example
System.out.println(6 | 3);
Output : 7
System.out.println(6 | 4);
Output : 6
It's a binary bitwise or operator, and it produces an int
. We can inspect the values in binary and decimal with a small program like
int i = 6;
for (int j = 3; j < 5; j++) {
System.out.printf("%d (%s) | %d (%s) = %d (%s)%n", i,
Integer.toBinaryString(i), j, Integer.toBinaryString(j),
(i | j), Integer.toBinaryString(i | j));
}
And the output is
6 (110) | 3 (11) = 7 (111)
6 (110) | 4 (100) = 6 (110)
Quite simple
System.out.println(6 | 3);
Performing
"OR" operation
of two values using corresponding binary values.System.out.println(6 | 4);