简体   繁体   English

Java“ int i = byte1 | 0x0200”与“ int i = byte1”?

[英]Java “int i = byte1 | 0x0200” vs “int i = byte1”?

In the page Wikipedia - Shifts in Java : Wikipedia-Java中的Shifts页面

In bit and shift operations, the type byte is implicitly converted to int . 在位和移位操作中,类型byte被隐式转换为int If the byte value is negative , the highest bit is one, then ones are used to fill up the extra bytes in the int. 如果字节值是负数 ,则最高位是1,那么将使用1来填充int中的额外字节。 So 所以

 byte b1=-5; int i = b1 | 0x0200; 

will give i == -5 as result. 将给出i == -5作为结果。

I understand that 0x0200 is equal to 0b0000 0010 0000 0000 . 我了解0x0200等于0b0000 0010 0000 0000 But what is the significance of 0x0200 in the passage shown above? 但是在上面显示的段落中0x0200的意义是什么?

I mean— b1 | 0x0200 我的意思是-b1 b1 | 0x0200 b1 | 0x0200 will always be equal to i (see " My Test " below), then in the passage above, why not simply write byte b1=-5; int i = b1 b1 | 0x0200将始终等于i (请参见下面的“ 我的测试 ”),然后在上面的段落中,为什么不简单地将byte b1=-5; int i = b1写入byte b1=-5; int i = b1 byte b1=-5; int i = b1 ? byte b1=-5; int i = b1吗?

My Test : 我的测试

public static void main(final String args[]) {
    final byte min_byte = Byte.MIN_VALUE; // -128
    final byte limit = 0; // according to the bolded words in the passage
    for (byte b = min_byte; b < limit; ++b) {
        final int i1 = b;
        final int i2 = b | 0x0200;
        if (i1 != i2) { // this never happens!
            System.out.println(b);
        }
    }
}

But what is the significance of 0x0200 in the passage shown above? 但是在上面显示的段落中0x0200的意义是什么?

This is done for illustration purposes only: the value 0x200 ORs in a one in a position that is equal to 1 already. 这样做仅出于说明目的:值0x200在一个位置等于1的位置成1 The idea is to show that the result is not 0x000002FB , but actually -5 , ie 0xFFFFFFFB . 这个想法是要表明结果不是0x000002FB ,而是实际为-5 ,即0xFFFFFFFB

I understand that 0x0200 is equal to 0b1111 1110 0000 0000 我了解0x0200等于0b1111 1110 0000 0000

No, it isn't. 不,不是。 The correct value is given by, 正确的值由

int i = 0x0200; // <-- decimal 512
System.out.println(Integer.toBinaryString(i));

Which outputs 哪个输出

1000000000

If we examine your second value, 如果我们检查您的第二个价值,

byte b1 = -5;
System.out.println(Integer.toBinaryString(b1));

We get 我们得到

11111111111111111111111111111011

Lining up both numbers 排列两个数字

11111111111111111111111111111011
00000000000000000000001000000000

It seems clear that the result will be the bit value of -5 (since the only 0 in -5 is also 0 in 0x0200 ). 它似乎很清楚,结果将是位值-5 (因为只有0-5也是00x0200 )。 To determine the significance we can examine 为了确定意义,我们可以检查

int i = 0x0200; // <-- Decimal 512
System.out.println("Dec: " + Integer.toBinaryString(i).length());

Output 产量

Dec: 10

So, the given bitwise OR will force the tenth bit to be true. 因此,给定的按位或将强制第十位为真。 It was true in your input byte, but if you used - Decimal 1535 ( 0b 101 1111 1111 ) then you would get, 在您的输入字节中确实如此,但是如果您使用-十进制1535( 0b 101 1111 1111 ),那么您会得到,

System.out.println(1535 | 0x0200);

Output is 输出是

2047

Because if you perform a bitwise-or on the two numbers 因为如果您对两个数字进行按位或运算

01000000000
10111111111

you get 你得到

11111111111

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

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