I'm trying to make some bit operations in Java for applying masks, represent sets etc. Why:
int one=1;
int two=2;
int andop=1&2;
System.out.println(andop);
Prints a "0" when is supposed to be "3":
0...001
0...010
_______
0...011
And how can I get that behavior?
Thanks in advance
Use the binary 'or' operator:
int andop = 1 | 2;
The binary 'and' operator will leave bits sets that are in both sides; in the case of 1
and 2
that is no bits at all.
You mixed up bitwise OR and bitwise AND
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With