Java bitwise “&” on integer

I have the following:

int a = 10001000;
 int b = 10000000;

I want the following output:

(a&b) = 10000000;

But my problem is that Java converts to binary operation before using "&". I really hope to be able to use it on integers in the above way Can you do that?

Solution

First, you need to use 0b to write a and B to indicate that they are binary Second, you need to use something like integer To binarystring (int) to get your bitwise & binary result Yes,

int a = 0b10001000,b = 0b10000000;
System.out.printf("(a&b) = %s;%n",Integer.toBinaryString(a & b));

Outputs (as required)

(a&b) = 10000000;
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
分享
二维码
< <上一篇
下一篇>>