The bitwise AND operator (&) in Java performs a bitwise AND operation between the binary representations of two integers. In this case, 4 & 6 is equivalent to 0100 & 0110, resulting in 0010, which is 2 in decimal.
The result of the expression 4 & 6 using the bitwise AND operator in Java is 4.
Here's the explanation:
The bitwise AND operator (&) performs a bitwise AND operation between corresponding bits of two integers. In binary representation:
4 in binary is 100.
6 in binary is 110.
Performing bitwise AND operation on each pair of corresponding bits:
100 (4 in binary)
& 110 (6 in binary)
-----
100 (Result in binary)
Converting the binary result 100 back to decimal gives 4, so 4 & 6 evaluates to 4.
public class BitwiseANDExample {
public static void main(String[] args) {
// Perform bitwise AND operation
int result = 4 & 6;
// Display the result
System.out.println("Result of 4 & 6: " + result);
}
}