当前位置:网站首页>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
边栏推荐
- 【MySQL】表结构的增删查改操作(DDL)
- Exec function of PHP
- JS mobile terminal copy text to clipboard code
- 用少儿编程思维塑造青少年领悟能力
- Array rotates the array from bits of the specified length
- Shaping teenagers' comprehension ability with children's programming thinking
- 【MySQL】常见数据类型总结
- Detailed explanation of Lora module wireless transceiver communication technology
- 【MySQL】表的约束
- How to view the occupied space of a table in MySQL database
猜你喜欢

【MySQL】表的约束
To do desktop plug-in, a good helper for office workers

GMPNN:Drug-drug interaction prediction with learnable size-adaptive molecular substructures.

oc swift 混编

ThinkPHP v6.0. X deserialization vulnerability recurrence

2022 - 06 - 09 rk817 PMU Battery Temperature Detection

Introduction to abbexa bacterial genome DNA Kit

Icml2022 | sharp maml: model independent meta learning for sharpness perception
![Ajout, suppression et modification des données du tableau [MySQL] (DML)](/img/08/4239bc0486fe8db2e98e54919300b5.png)
Ajout, suppression et modification des données du tableau [MySQL] (DML)

Understanding of related concepts of target detection
随机推荐
[nk] Niuke monthly competition 51 f-average question
To do desktop plug-in, a good helper for office workers
【MySQL】錶數據的增删查改(DML)
Array intersection of two arrays II
C语言-排序中的快速排序(简称快排)
记录(二)
记录(三)
笔记(二)
在模仿学习中进步的智能机器人
Back to table query of MySQL? How to avoid it?
Cordova Plugin /JPush PhoneGap 极光推送_本地推送_消息推送
leetcode:333. 最大 BST 子树
If else is too easy to use? (understanding this article will further improve your logic)
字符串 反转
About type-C
2022-06-09 RK817 PMU 電池溫度檢測
如何激发文化创新的活力和驱动力
Business based precipitation component = & gt; manage-table
数组 删除数组中的重复项
Has the samesite cookie problem occurred when using identityserver?