Notes on Java byte shift operation

Notes on Java byte shift operation

Java's + - * / >> >>> << & | ^ (add, subtract, multiply, divide, right shift, left shift, unsigned right shift, bit AND, bit OR, bit XOR) operations on byte, Both will first convert the byte to int, and then perform the operation. This fact can lead to multiple problems:

 

Suppose we want to perform the following byte operation: 1111 1000 Shift 1 bit to the right, and then OR with 0000 0001 to get 0111 1101.

The intuitive programming is as follows:

byte b = 0xf8;

byte b2 =  b >> 1 | 0x01;  

There are multiple errors in this spelling, which are corrected one by one:

1 The compiler reports an error, int cannot be directly and automatically converted to byte 

To solve this problem, strengthen the system transformation.

byte b = (byte)0xf8;

byte b2 = (byte)( (b >> 1) | 0x01);  

 

2 The output is 1111 1101 not what we want 0011 1101

The reason is that >> is a signed right shift. When the sign bit is 1, the left side is filled with 1 instead of 0.

Modified to use >>> unsigned right shift:

byte b = (byte)0xf8;

byte b2 = (byte)( (b >>> 1) | 0x01);  

3 After running, it is found that the output is still 1111 1101 

The reason is that byte is converted into int before the operation, and then the bit operation is performed, so the operation steps after decomposition are as follows:

b converts to int 1111 1000 converts to 11111111 11111111 11111111 11111000

Unsigned right shift 1 bit 01111111 11111111 11111111 11111100

Bitwise OR with 0x01 01111111 11111111 11111111 11111101

Force conversion back to byte 11111101

Solution, bitwise and 0xff before right shift operation

byte b = (byte)0xf8;

byte b2 = (byte)( ((b & 0xff )>>> 1) | 0x01); // Note that parentheses must be added, because >>> has higher priority than &

 

4 After running, it is found that the output is the result we want 0111 1101. The operation steps are broken down as follows:

b converts to int 1111 1000 converts to 11111111 11111111 11111111 11111000

& operation with 0xff 00000000 00000000 00000000 11111000

Unsigned right shift 1 bit 00000000 00000000 00000000 01111100

Bitwise OR with 0x01 00000000 00000000 00000000 01111101

Force conversion back to byte 01111101

 

5 About System.out.println();

byte b = (byte)0xf8;

System.out.println(b); -- The final output is -8

The operation steps are:

b converts to int 1111 1000 converts to 11111111 11111111 11111111 11111000

Take the sign bit - -1111111 11111111 11111111 11111000

Return +1 (because it is in complement operation) -0000000 00000000 00000000 00001000

output -8

 

final conclusion:

1 Distinguish the use of >> and >>>

2 first & 0xff before >> operation

3 Pay attention to symbol precedence and use parentheses correctly.

4 It is important to note that & has less precedence than + . So the result of a = b & 0xff + 2000 may not be what you want

 

Attached:

Print byte, int function for each bit value.

 

public static void printByte(byte b){

    for(int i = 7; i >=0 ; i --){

        int shiftleft = (b >> i) & 0x01;

        System.out.print(shiftleft);

    }

    System.out.println();

}

 

public static void printInt(int b){

    for(int i = 31; i >=0 ; i --){

        int shiftleft = (b >> i) & 0x01;

        System.out.print(shiftleft);

    }

    System.out.println();

}

 

http://blog.csdn.net/qq_30739519/article/details/50991484

1.1. java virtual machine integer

In the Java virtual machine, there are four types of integers: byte, short, int, and long, which represent 8-bit, 16-bit, 32-bit, and 64-bit signed integers respectively. Integers are represented using two's complement.

So let's first understand the original code and the inverse code.

1.1.1. Original code

The so-called original code is the binary representation of the sign bit plus the number, int as an example, the first bit represents the sign (0 positive number 1 negative number) during a simple period of one byte representation

The original code of +7 is: 00000111
 The original code of -7 is: 10000111

For the original code, positive and negative numbers with the same absolute value differ only in the sign bit.

1.1.2. Inverse code

If a number is positive, its inverse code is the same as the original code; if a number is negative, the sign bit is 1, (the sign bit does not change, and the remaining bits are inverted).

In other words, the absolute value of the number is negated (every bit is negated when the absolute value is negated).
For simplicity, we use 1 byte to represent an integer: +7's
     complement: 00000111
     -7's complement: 11111000

1.1.3. Two's complement

Complement code: If a number is positive, its original code, inverse code, and complement code are the same; if a number is negative, go to the inverse code and add 1. (One's complement plus 1's complement) For simplicity, we use 1 byte to represent an integer:
+7's complement is: 00000111
-7's complement is: 11111001

1.1.4. Summary

Positive number: its original code, inverse code and complement code are the same.

Negative numbers: the sign bit of the inverse code does not change, the remaining digits are inverted, the sign bit of the complement code does not change, and the original code of the remaining bits is inverted (inverse code) + 1, in other words, the inverse code + 1

 

A negative original code is known to negate the code:

step:

1. Invert the absolute value of the number 

Know a negative complement to find the complement:

1. Inverse code +1

 

Known a negative number inverse code steps:

Invert the absolute value of a positive number +1

 

Knowing a negative number's complement step:

1. Binary original code representation 

2. The sign bit does not change to 1, and the inverse code is obtained.

3. The sign bit does not change, and the rest is incremented by 1.

Examples are as follows:

-10 Complement steps:

Original code of -10: 10000000 00000000 00000000 00001010

-10's complement: 11111111 11111111 11111111 11110101

-10's complement: 11111111 11111111 11111111 11110110

 

Knowing the complement of a negative number, convert it to a decimal number, step

   1. Invert each bit first;
      2. Convert it to a decimal number;
      3. Add a minus sign, then subtract 1.

  E.g:

11111010, the highest bit is 1, so it is a negative number, first invert each bit to get 00000101, convert it to decimal to get 5, add the minus sign to get -5, and then subtract 1 to get -6.

1.1.5. Value range of Java byte type

1. Make sure that byte is 1 byte, that is, 8 bits.

2. Maximum 0111 1111

3. The minimum value is 1000 0000.

4.0111 1111 is 127.

5. 1000 0000 minus 1 is 1111 1111 bitwise inverse 1000 0000 to get -128

1.1.6. Frequently Asked Questions

int a=232;

 

//0000 0000 1110 1000

System.out.println(Integer.toBinaryString(a));

System.out.println((byte) a);

The output is -24:

The principle is as follows:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325517049&siteId=291194637