+ 1
I was trying to decode a java code snippet given below
class BitDemo { public static void main(String[] args) { int bitmask = 0x000F; int val = 0x2222; // prints "2" System.out.println(val & bitmask); } } here bitmask is a number val too is a number in the print it says a number and a number which prints 2. doesnt make sense... or did i get it wrong plis help
1 Answer
+ 5
& is the bitwise and operator, it performs the and operation on the respective bits of both numbers.(0010 0010 0010 0010) & (0000 0000 0000 1111) = (0000 0000 0000 0010)
AND:
1 & 1 = 1
0 & 0 = 0
1 & 0 = 0
0 & 1 = 0