简体   繁体   English

按位运算符java

[英]Bitwise Operators java

my code is 我的代码是

final int CONST_1 = 1010;
final int CONST_2 = 1011;

System.out.println("CONST_1 & CONST_2: " + Integer.toBinaryString(CONST_1 & CONST_2));
System.out.println("CONST_1 ^ CONST_2: " + Integer.toBinaryString(CONST_1 ^ CONST_2));
System.out.println("CONST_1 | CONST_2: " + Integer.toBinaryString(CONST_1 | CONST_2));
System.out.println("~CONST_1 : " + Integer.toBinaryString(~CONST_1));

Output is 输出是

CONST_1 & CONST_2: 1111110010
CONST_1 ^ CONST_2: 1
CONST_1 | CONST_2: 1111110011
~CONST_1 : 11111111111111111111110000001101

In my opinion it's wrong and it should be: 在我看来这是错的,它应该是:

CONST_1 & CONST_2: 1010
CONST_1 ^ CONST_2: 1
CONST_1 | CONST_2: 1011
~CONST_1 : 101

Please explain me why I have such result. 请解释我为什么会有这样的结果。 Thanks! 谢谢!

Change this: 改变这个:

final int CONST_1 = 1010;
final int CONST_2 = 1011;

to this: 对此:

final int CONST_1 = 0b1010;
final int CONST_2 = 0b1011;

Don't forget that literals are decimal by default. 不要忘记默认情况下文字是十进制的。 You clearly wanted them to be binary. 你显然希望它们是二进制的。


Binary literals require Java 1.7. 二进制文字需要Java 1.7。 So if that's not available, you can go with this: 所以,如果没有,你可以这样做:

final int CONST_1 = Integer.parseInt("1010",2);
final int CONST_2 = Integer.parseInt("1011",2);

CONST_1 is 1010 in decimal. CONST_1是十进制的1010 The binary value of CONST_1 is 1111110010 . CONST_1的二进制值为1111110010 Similarly CONST_2 is 1111110011 . 类似地, CONST_21111110011

Does the result make more sense now? 结果现在更有意义吗?

I think you know what is meant by a literal. 我想你知道字面意思是什么意思。 If not, please refer: Java Literals and Literal . 如果没有,请参阅: Java LiteralsLiteral

Now, Integer and Floating-Point literals are decimal by default in Java. 现在,在Java中,默认情况下,Integer和Floating-Point文字是十进制的。 So, the value 1010 you typed above will be decimal 1010 . 因此,您在上面输入的值1010将是十进制1010 ie., One thousand and ten . 即, 一千一十 If you want them to be binary (it is clear from the question), there are many possibilities. 如果你想要它们是二进制的(从问题中可以清楚地看出),有很多可能性。

  • Use equivalent decimal value. 使用等效的十进制值。

You may use the decimal equivalent of the binary value you want to represent. 您可以使用要表示的二进制值的十进制等值。 Here, for example, decimal equivalent of binary 1010 is 10 , and that of binary 1011 is 11 . 这里,例如,二进制1010十进制等值是10 ,二进制1011十进制等值是11

final int CONST_1 = 10;
final int CONST_2 = 11;
  • Use wrapper classes parse method 使用包装类解析方法

Each wrapper class has a parse method, which takes the base of number system also as argument. 每个包装类都有一个parse方法,它以数字系统的基础作为参数。 So 所以

final int CONST_1 = Integer.parseInt("1010", 2);
final int CONST_2 = Integer.parseInt("1011", 2);
  • If you are using Java 7, use binary literals 如果您使用的是Java 7,请使用二进制文字

Binary literals are not supported in older versions of Java. 旧版本的Java不支持二进制文字。 Java 7 introduces binary literals. Java 7引入了二进制文字。 See features . 查看功能

final int CONST_1 = 0b1010;
final int CONST_2 = 0b1011;

Don't confuse an integer composed of only 0 and 1 with the binary representation of the integer... 不要将仅由0和1组成的整数与整数的二进制表示混淆...

The integer 1010 is 1111110010 in binary, so the results are correct. 整数1010是二进制的1111110010,因此结果是正确的。

Your numbers aren't binary. 你的号码不是二进制的。 They are written in decimal. 它们以十进制编写。 You want to prepend a 0b to tell Java 7 this int is in binary. 你想在前面加一个0b告诉Java 7这个int是二进制的。 If you aren't using Java 7 there is no binary literal syntax so you can do this Integer.parseInt("1010", 2) as a work around or use HEX literal notation: 如果您没有使用Java 7,则没有二进制文字语法,因此您可以将此Integer.parseInt(“1010”,2)用作解决方法或使用HEX文字表示法:

final int CONST_1_BINARY = 0b1010;
final int CONST_1_DECIMAL = 1010;

if( CONST_1_BINARY == CONST_1_DECIMAL ) {
   System.out.println("They are the same!");
} else {
   System.out.println("They are NOT the same.");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM