当前位置:网站首页>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
边栏推荐
- ThinkPHP v6.0. X deserialization vulnerability recurrence
- Latex error: file ‘xxx. sty‘ not found
- SQL Server2019安装的详细步骤实战记录(亲测可用)
- 在D天内送达包裹的能力[抽象类二分--抽象判定方式]
- CentOS7环境下MySQL8常用命令小结
- 记录(三)
- C use s7 Net connected to Siemens s1200plc, C # directly connected to Siemens PLC
- Sum of two array numbers
- [nk] Niuke monthly competition 51 f-average question
- String analysis and use
猜你喜欢

C语言-排序中的快速排序(简称快排)

解读创客空间下的教育新生态

【MySQL】表结构的增删查改操作(DDL)
![在D天内送达包裹的能力[抽象类二分--抽象判定方式]](/img/16/026c017e1c54d580fa2d237aa4605c.png)
在D天内送达包裹的能力[抽象类二分--抽象判定方式]

用少儿编程思维塑造青少年领悟能力

Intelligent robot making progress in imitation learning

KDD2022 | 基于对抗性知识蒸馏的深度图神经网络压缩

Principle of gravure overprint and factors affecting overprint

About type-C

Interpreting the new ecology of education in maker space
随机推荐
Interpreting the new ecology of education in maker space
Can I make up the exam if I fail the soft exam? Here comes the answer
Visio 转为高质量PDF
C语言-排序中的快速排序(简称快排)
php的exec函数
【MySQL】表的约束
数组 删除数组中的重复项
系统重装以及查询系统性能
Has the samesite cookie problem occurred when using identityserver?
Latex error: file ‘xxx. sty‘ not found
按位操作符和移位操作符
Abbexa AML1 DNA binding ELISA Kit instructions
Only this is the most true reason why leaders promote you. The rest is nonsense!
MySQL inserts query results into other tables
Principle of gravure overprint and factors affecting overprint
在Oracle表中进行关键词搜索的过程
2022-06-09 rk817 PMU battery temperature detection
2022-06-09 RK817 PMU 電池溫度檢測
Understanding of related concepts of target detection
Detailed steps and actual records of SQL server2019 installation (available for hands-on test)