当前位置:网站首页>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
边栏推荐
- 数据可视化图表总结(一)
- 1039 Course List for Student
- TypeScript 基础讲解
- Network security skills competition in Secondary Vocational Schools -- a tutorial article on middleware penetration testing in Guangxi regional competition
- leetcode-556:下一个更大元素 III
- Leetcode stack related
- 7. Processing the input of multidimensional features
- Some common problems in the assessment of network engineers: WLAN, BGP, switch
- SPI details
- MySQL advanced part 1: View
猜你喜欢
liunx启动redis
WordPress switches the page, and the domain name changes back to the IP address
Doing SQL performance optimization is really eye-catching
MySQL advanced part 1: View
Appium基础 — 使用Appium的第一个Demo
Overview of variable resistors - structure, operation and different applications
Quickly use Amazon memorydb and build your own redis memory database
MySQL advanced part 1: index
LeetCode 0108.将有序数组转换为二叉搜索树 - 数组中值为根,中值左右分别为左右子树
Sqlmap tutorial (1)
随机推荐
“磐云杯”中职网络安全技能大赛A模块新题
【Rust 笔记】15-字符串与文本(下)
MySQL advanced part 2: optimizing SQL steps
数据可视化图表总结(一)
MySQL advanced part 1: triggers
Leetcode-3: Longest substring without repeated characters
Leetcode dynamic programming
Leetcode-6110: number of incremental paths in the grid graph
Records of some tools 2022
One question per day 1447 Simplest fraction
927. Trisection simulation
redis发布订阅命令行实现
MySQL advanced part 2: SQL optimization
Smart construction site "hydropower energy consumption online monitoring system"
leetcode-31:下一个排列
WordPress switches the page, and the domain name changes back to the IP address
One question per day 2047 Number of valid words in the sentence
Overview of variable resistors - structure, operation and different applications
QT判断界面当前点击的按钮和当前鼠标坐标
leetcode-22:括号生成