Bit level operations in Java

I'm trying to do something in Java to apply masks, representation sets, etc

int one=1;
int two=2;
int andop=1&2;
System.out.println(andop);

Print "0" if "3" is expected:

0...001
0...010
_______
0...011

How can I get this behavior?

Thank you in advance

Solution

Use binary 'or' operator:

int andop = 1 | 2;

Binary 'and' operators will retain the bit sets on both sides; In the case of 1 and 2, there are no bits at all

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>