Detailed java bit operation

There are three types of java bitwise operators

<<       左移(又叫:算数左移,此时,它又等价于逻辑左移)
>>       右移(又叫:算数右移)
>>>      无符号右移(又叫:逻辑右移)

1. Basic knowledge pave the way 


We all know that for signed data types, the leftmost digit of the binary is the sign bit, 0 represents positive, and 1 represents negative. Here are a few concepts

Logic left shift=arithmetic shift left: high bit overflow, low bit complement 0
Logic right shift: low bit overflow, high bit complement 0
Arithmetic shift right: low bit overflow, high bit complement the value of the sign bit

For example, a signed 8-bit binary number 10101010, [] is the added number

Logic left by one bit: 0101010[0]
Logic left by two bits: 101010[00]

Arithmetic shift left by one bit: 0101010[0]
Arithmetic shift left by two bits: 101010[00]

Logic shift one bit to the right: [0]1010101
Logic shift two digits to the right: [00]101010

Arithmetic shift right by one bit: [1]1010101
Arithmetic shift right by two bits: [11]101010

Arithmetic shift left and arithmetic shift right are mainly used to multiply and halve signed numbers.
Logical shift left and logic right are mainly used to multiply and halve unsigned numbers
(there is no unsigned data type in Java, C And C++)

symbol example Explanation
<< num<< n Equivalent to Surely * 2 ^ {n}arithmetic shift left (logical shift left)
>> num>>n Equivalent to \frac{num}{2^{n}}, arithmetic shift right
>>> num>>>n Logical right shift, when num is a positive number and arithmetic shift right has an effect

2. Detailed examples

<< means shift to the left, regardless of positive and negative numbers, with 0 in the low digit; 

Note: The following data types are byte-8 by default

Regardless of the positive or negative when shifting to the left, the low bit is filled with 0

Positive number: r = 20 << 2

  Two's complement of 20: 0001 0100

  After moving two bits to the left: 0101 0000

         Result: r = 80

Negative numbers: r = -20 << 2

  -20 original binary code: 1001 0100

  The binary complement of -20  : 1110 1011

  Two's complement of -20: 1110 1100

  Two's complement after shifting to the left: 1011 0000

        Inverse code: 1010 1111

        Original code: 1101 0000

        Result: r = -80

>> means shift to the right, if the number is positive, the high digit is filled with 0, if it is negative, the high digit is filled with 1;

Note: The following data types are byte-8 by default

Positive number: r = 20 >> 2

  Two's complement of 20: 0001 0100

  After moving two bits to the right: 0000 0101

       Result: r = 5

Negative numbers: r = -20 >> 2

  -20 original binary code: 1001 0100

  The binary complement of -20  : 1110 1011

  Two's complement of -20: 1110 1100 

  Two's complement after shifting to the right: 1111 1011 

        Inverse code: 1111 1010

        Original code: 1000 0101

        Result: r = -5

>>> means unsigned right shift, also called logical right shift, that is, if the number is positive, the high bit is filled with 0, and if the number is negative, the high bit is also filled with 0 after right shift

Positive number: r = 20 >>> 2

    The result is the same as r = 20 >> 2;

Negative numbers: r = -20 >>> 2

Note: The following data types default to int 32 bits

  -20: Source code: 10000000 00000000 00000000 00010100

    Inverse code: 11111111 11111111 11111111 11101011

    Complement: 11111111 11111111 11111111 11101100

Move    right: 00111111 11111111 11111111 11111011

    Result: r = 1073741819

 

Supplement: The one's complement is converted to the complement, which means adding 1

           The complement is converted to the inverse code, which means subtracting 1

          Original-"Complement: Invert, add 1 to
          complement -" Original: Subtract 1, Invert

So, have you abandoned your studies?

Guess you like

Origin blog.csdn.net/Brave_heart4pzj/article/details/114526306