当前位置:网站首页>C Primer Plus Chapter 15 (bit operation)
C Primer Plus Chapter 15 (bit operation)
2022-07-05 06:15:00 【His Last Bow】
Catalog
1. Binary system 、 Bits and bytes
- With 2 The number represented by the base is called Binary number (binary number)
1 1 0 1
1 * 2^3 + 1 * 2^2 + 0 * 2^1 + 1 * 2^0 = 13
1.1 Binary integer
C Language use byte (byte) Represents the required size of the storage system character set
- 1 byte = 8 position
Yes 1 byte (8 position ) In different ways Bit combination (bit pattern)
unsigned char: 0000 0000 ~ 1111 1111 = 0 ~ 255
signed char : 1000 0000 ~ 0111 1111 = -128 ~ 127
1.2 Signed integers
Sign quantity (sign-magnitude) notation :1 position ** Higher order potential (high-order bit)** Store symbols , be left over 7 Bits represent the number itself
- 1111 1111 ~ 0111 1111 = -127 ~ 127
- There are two 0 :
- 0000 0000 = +0
- 1000 0000 = -0
Binary complement (two’s-complement) Method : Take the opposite , Add 1
- 1111 1111 ~ 0111 1111 = -128 ~ 127
Binary inverse (one’s-complement) Method : Take the opposite
- 1000 0000 ~ 0111 1111 = -127 ~ 127
- There are two 0 :
- 0000 0000 = +0
- 1111 1111 = -0
1.3 Binary floating point numbers
- Floating point numbers are stored in two parts
- Binary decimals
- Binary index
1.3.1 Binary decimals
- With 2 Power of as denominator
1 0 1
1/2 + 0/4 + 1/8 = 0.625
- Many fractions cannot be accurately represented by binary decimals
- It can only accurately represent multiple 1/2 The sum of the powers of
1.3.2 floating-point number
- Several bits store binary fractions , Other bits store index
2. Other hexadecimal numbers
- Octal and hexadecimal numeration systems are usually used
2.1 octal
- octal (octal) It refers to octal numeration system
- With 8 Represent numbers for the base
4 5 1
4 * 8^2 + 5 * 8^1 + 1 * 8^0 = 297
Each octal digit corresponds to 3 Binary bits
Binary equivalent to octal
Octal digit Equivalent binary 0 000 1 001 2 010 3 011 4 100 5 101 6 110 7 111
2.2 Hexadecimal
- Hexadecimal (hexadecimal or hex) It refers to the hexadecimal numeration system
- With 16 Represent numbers for the base
A 3 F
10 * 16^2 + 3 * 16^1 + 15 * 16^0 = 2623
Each hexadecimal digit corresponds to 4 Binary bits
Decimal system 、 Hexadecimal and equivalent binary digits
Decimal system Hexadecimal Equivalent binary 0 0 0000 1 1 0001 2 2 0010 3 3 0011 4 4 0100 5 5 0101 6 6 0110 7 7 0111 8 8 1000 9 9 1001 10 A 1010 11 B 1011 12 C 1100 13 D 1101 14 E 1110 15 F 1111
3. C Bitwise operators
3.1 Bitwise logical operators
- Bitwise (bitwise) operation : Operations are carried out for each bit , It doesn't affect the left and right bits
3.1.1 Binary inversion or bitwise inversion :~
- hold 1 Turn into 0 , hold 0 Turn into 1
- The operator does not change the original value
char c = 0b10011010;
~c;
printf("%d\n%d\n", c, ~c);
// ~(1001 1010) = -102
// 0110 0101 = 101
3.1.2 Bitwise AND :&
- A fellow 1 Only then 1
char c1 = 0b10010011;
char c2 = 0b00111101;
printf("%d\n", c1 & c2);
// 1001 0011
// 0011 1101
// &
// 0001 0001
3.1.3 Press bit or :|
- Yes 1 Then for 1
char c1 = 0b10010011;
char c2 = 0b00111101;
printf("%d\n", c1 | c2);
// 1001 0011
// 0011 1101
// |
// 1011 1111
3.1.4 Bitwise XOR :^
- The difference is 1
char c1 = 0b10010011;
char c2 = 0b00111101;
printf("%d\n", c1 ^ c2);
// 1001 0011
// 0011 1101
// ^
// 1010 1110
3.7 Shift Operators
3.7.1 Move left :<<
- Move the object to the left of the operator to the left , The number of bits specified by the right object
- The operator does not change the original value
char c = 0b10001010;
c << 2;
printf("%d\n%d\n", c, c << 2);
// 00 1000 1010
// 10 0010 1000
// High truncation
// 0010 1000
3.7.2 Move right :>>
- Move the object to the left of the operator to the right , The number of bits specified by the right object
- The operator does not change the original value
char c = 0b10001010;
c >> 2;
printf("%d\n%d\n", c, c >> 2);
// 1000 1010
// 1110 0010 10
// Low truncation
// 1110 0010
3.7.3 usage : Shift Operators
- in the light of 2 The power of provides fast multiplication and division
- number << n :number multiply 2 Of n The next power
- number >> n : If number non-negative , Then use number Divide 2 Of n The next power
4. Bit fields
- Bit fields (bit field) It's a signed int or unsigned int A set of adjacent bits in a type variable
- Build through a structure declaration
struct {
int a : 1;
int b : 2;
int c : 3;
} abc;
// abc Include for 3 individual 1 Bit field
abc.a = 0;
abc.c = 1;
// Because the number of digits in the field is 1 , So it can only be assigned 0 or 1
If the total number of digits declared exceeds int (unsigned int Empathy ) Number of digits , Will use the next one int Storage location
- When this happens , first int Will leave an unnamed “ hole ”
- You can use unnamed field widths “ fill ” Unnamed “ hole ”
- Use a width of 0 The unnamed field of causes the next field to be stored in the next int in
struct { int a : 1; int : 2; int b : 2; int : 0; int c : 3; } abc; // a and b There is a 2 Bit gap // c Will be stored in the next int z
边栏推荐
- SQLMAP使用教程(一)
- TypeScript 基础讲解
- Groupbykey() and reducebykey() and combinebykey() in spark
- Smart construction site "hydropower energy consumption online monitoring system"
- 数据可视化图表总结(一)
- [rust notes] 17 concurrent (Part 2)
- In depth analysis of for (VaR I = 0; I < 5; i++) {settimeout (() => console.log (I), 1000)}
- 2022年貴州省職業院校技能大賽中職組網絡安全賽項規程
- The difference between CPU core and logical processor
- Appium基础 — 使用Appium的第一个Demo
猜你喜欢
传统数据库逐渐“难适应”,云原生数据库脱颖而出
LeetCode 0108. Convert an ordered array into a binary search tree - the median of the array is the root, and the left and right of the median are the left and right subtrees respectively
leetcode-6110:网格图中递增路径的数目
redis发布订阅命令行实现
MIT-6874-Deep Learning in the Life Sciences Week 7
1.14 - assembly line
快速使用Amazon MemoryDB并构建你专属的Redis内存数据库
数据可视化图表总结(二)
WordPress switches the page, and the domain name changes back to the IP address
SPI 详解
随机推荐
MySQL advanced part 1: index
Leetcode-1200: minimum absolute difference
对for(var i = 0;i < 5;i++) {setTimeout(() => console.log(i),1000)}的深入分析
MySQL advanced part 2: the use of indexes
927. Trisection simulation
Introduction and experience of wazuh open source host security solution
传统数据库逐渐“难适应”,云原生数据库脱颖而出
The difference between CPU core and logical processor
The connection and solution between the shortest Hamilton path and the traveling salesman problem
Leetcode-22: bracket generation
JS quickly converts JSON data into URL parameters
[practical skills] technical management of managers with non-technical background
Currently clicked button and current mouse coordinates in QT judgment interface
Basic explanation of typescript
Some common problems in the assessment of network engineers: WLAN, BGP, switch
Traditional databases are gradually "difficult to adapt", and cloud native databases stand out
数据可视化图表总结(一)
redis发布订阅命令行实现
2022年貴州省職業院校技能大賽中職組網絡安全賽項規程
Leetcode recursion