I was trying to decode a java code snippet given below | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

4th Jan 2019, 5:44 PM
stephen haokip
stephen haokip - avatar
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
4th Jan 2019, 5:56 PM
xyz$