当前位置:网站首页>Bitwise and shift operators

Bitwise and shift operators

2022-06-10 22:18:00 Li_ XiaoJin

Today, I will mainly learn about bitwise operators and shift operators , Because I often encounter when I look at the source code , I didn't understand before , Take this opportunity to learn .

The bitwise operator

The bitwise operator is used to operate on one of the basic types of integers “ The bit ”, That is, binary .

And operators &

If both input bits are 1, An output bit is generated 1; Otherwise an output bit is generated 0. for instance :

13: 1101
 6: 0110
13 & 6  obtain  0100  --> 4

Or operators |

If two inputs are as long as one is 1, An output bit is generated 1; Only two input bits are 0 An output bit is generated only in the case of 0. for example :

12: 1100
 6: 0110
12 | 6  obtain  1110  --> 14

Exclusive or operation ^

If one of the two input bits is 1, But not all 1, An XOR operation generates an output bit 1. If both input bits are 1 Or both 0, An output bit is generated 0. for example :

12: 1100
 6: 0110
12 ^ 6  obtain  1010  --> 10

Bitwise non ~

Bitwise negation is also called the negation operator , Belongs to unary operator , Operate on only one operand ( The previous operators are all for two operands , It's called a binary operator ), Its main operation is to input 0, The output 1; If input 1, The output 0.

6 The binary of is : 00000000000000000000000000000110 Negate every bit , obtain : 11111111111111111111111111111001 This is a form of storage in memory . The decimal system we read is based on the original code , And in memory , Values are stored as binary complements . The complement of a positive number is the same as the original code , Rules for converting negative numbers from original code to complement or from complement to original code : The sign bits remain the same , Invert the remaining bits , Get the inverse , On the basis of the inverse code, add one to the last digit to get the complement of the negative number . 11111111111111111111111111111001 Output 10 The process of binary system : 1. The sign bits remain the same , Residual bit negation   10000000000000000000000000000110 2. The last one to add 1  10000000000000000000000000000111

        System.out.println(Integer.toBinaryString(6));
        System.out.println(~6);
        System.out.println(Integer.toBinaryString(~6));

For any number x Perform bit by bit non operation x = -(x + 1). for example ,6 The result is -7.

Shift operator

The objects that the shift operator operates on are also binary “ position ”. The shift operator can only be used to operate on integer types .

The left shift operator <<

The left shift operator can move the operand on the left of the operator to the left according to the number of bits specified on the right , Make up in the low position 0.

In decimal 1 The hexadecimal representation of is : 0000 0001 When executed 1 << 4, Move it to the left 4 position , obtain : 0001 0000 The alternate decimal system is 16

Right shift operator >>

The shift right operator shifts the operand to the left of the operator to the right by the number of bits specified on the right of the operator . The shift right operator uses “ Symbol extension ”, The main performance is :

(1) If the symbol is positive , Insert... In the high position 0;

(2) If the sign is negative , Insert... In the high position 1;

There is also an unsigned right shift operator >>> , Use zero extension , Positive or negative , It's all in the high position 0.

25 The binary of is : 00011001 When executed 25 >> 2, Move it to the right 2 position , obtain : 00000110 The alternate decimal system is 6

Let's look at the unsigned shift ,-25 The binary representation of : 11111111111111111111111111100111 When executed 25 >> 2, Move it to the right 2 position , obtain : 00111111111111111111111111111001 The alternate decimal system is 1073741817

Copyright: use Creative Commons signature 4.0 International license agreement to license Links:https://lixj.fun/archives/ The bitwise operator

原网站

版权声明
本文为[Li_ XiaoJin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206102054351628.html