当前位置:网站首页>[C language foundation] 15 bit operation
[C language foundation] 15 bit operation
2022-07-23 10:16:00 【One reed to sail FP】
One 、 Shift operation
| Operator | meaning |
|---|---|
<< | Move left |
>> | Move right |
1.1 << Move left
- i << j
- take i All bits in move left j A place , The right to repair 0;
- All less than int The type of , Shift to int The way to do it , The result is int type .
- x << 1 Equivalent to x *= 2
- x << n Equivalent to x *= xn
- a number ** Every left shift 1 position , Equivalent to multiplying 2**
1.2 >> Move right
- i >> j
- take i All bits in move left j A place , The right to repair 0;
- All less than int The type of , Shift to int The way to do it , The result is int type .
- about unsigned type , Left complement 0;
- about signed type , Fill in the original highest digit on the left (0 or 1, Leave the sign unchanged )
- x >> 1 Equivalent to x /= 2
- x >> n Equivalent to x /= xn
- a number ** Every shift right 1 position , Equivalent to dividing 2**
int a = 0x80000000;
unsigned int b = 0x80000000;
printf(" a = %d\n", a);
printf(" b = %u\n", b);
printf("a>>16 = %d\n", a>>16);
printf("b>>16 = %u\n", b>>16);
Output results :
1.3 Be careful
Do not set the number of bits shifted left or right to a negative number , Otherwise, it will result in undefined behavior .
Two 、 Bitwise operation
Bitwise operators :
| Operator | meaning |
|---|---|
& | Bitwise AND |
| ` | ` |
~ | According to the not |
^ | Bitwise XOR |
2.1 & Bitwise AND
- If (x)i == 1 And (y)i == 1, be (x & y)i = 1. otherwise (x & y)i = 0;
- Yes 0 Must be 0, The same is 1 Only then 1
- and 0 Meet each other Must be 0, and 1 Meet each other Is the same
give an example :
0101 1010 & 10001 100 = 0000 1000(5A & 8C = 08)
Bitwise and common applications :
- Let one or more bits be 0:x & 0xFE( Give Way x The lowest is 0)
- Take a paragraph from a number : x & 0xFF( take x Lowest 1 Bytes , Others are 0)
give an example :
1010 0101 & 0xFE = A5 & FE = A4
1010 1010 1010 1010 & 0xFF = AAAA & FF = AA(125252 & FF = 252)
2.2 | Press bit or
- If (x)i == 1 And (y)i == 1, be (x | y)i = 1. otherwise (x | y)i = 0;
- Yes 1 Must be 1, The same is 0 Only then 0
- and 1 Phase or Must be 1, and 0 Phase or Is the same
Bitwise and common applications :
- Make one or more bits 1:x | 0x01( Give Way x The lowest is 1)
- Put the two numbers together :0x00FF | 0xFF00 = 0xFFFF
2.3 ~ According to the not
- ( ~x )i = 1 - ( x )i
- 01 swap .
give an example :
~1010 1010 = 0101 0101(~AA = 55)
Bitwise negation is different from complement :
- Bitwise inversion is to put the 01 Flip ;
- ~1010 1010 = 0101 0101
- Ask for a complement :
- The complement of a positive number is equal to the original code ;
- A negative complement , The sign bits remain the same , The other bits are reversed and then added 1.
2.4 ^ Bitwise XOR
- If ( x )i == ( y )i, that ( x ^ y )i = 0, Otherwise 1;
- Same as 0, Dissimilarity is 1
- XOR a variable twice with the same value , It's like doing nothing . x ^ y ^ y = x
2.5 Logical operation and bitwise operation
Logical operation is different from bitwise operation , Don't confuse , But you can think of logical operations as all non 0 Value change 1, Then perform bitwise operation .
- 5 & 4 = 4 and 5 && 4 ——> 1 & 1 = 1
- 5 | 4 = 5 and 5 || 4 ——> 1 | 1 = 1
- ~4 = 3 and !4 ——> !1 = 0
3、 ... and 、 Examples of bit operations
3.1 Output a binary number of numbers
int number;
scanf("%d", &number);
unsigned mask = 1u<<31; //unsigned Omit types between and variables , The default is unsigned int type
//1u Expressed as unsigned int Type of 1
//1u<<31 = 10000...00(31 individual 0)
for(; mask; mask>>=1){
//mask Move right , There is always only one at a time 1
printf("%d", number & mask ? 1 : 0);
}
printf("\n");
3.2 Control the specific position of a number
const unsigned int SBS = 1u<<2; //SBS = 00...0100
const unsigned int PE = 1u<<3; //PE = 00...01000
U0LCR |= SBS | PE; // Give Way U0LCR Of the 2 Position and number 3 Position as 1, The remaining bits remain unchanged (SBS|PE = 00...01100)
U0LCR &= ~(SBS | PE); // Give Way U0LCR Of the 2 Position and number 3 Position as 0, The remaining bits remain unchanged
Four 、 Bit segment
4.1 explain
- Put one int Several bits of are combined into a structure , Specific bits form a bit segment , For example struct, Control specific positions through members in the structure .
struct{
unsigned int leading : 3; // Colon after member : The last number is the number of bits occupied by the member
unsigned int FLAG1 : 1;
unsigned int FLAG2 : 1;
int trailing : 11;
};
4.2 give an example
void prtBin(unsigned int number);
struct U0{
unsigned int leading : 3; // Colon after member : The last number is the number of bits occupied by the member , Starting from the low
unsigned int FLAG1 : 1;
unsigned int FLAG2 : 1;
int trailing : 27; // Guarantee a total of 32 position ,unsigned int The number of digits
};
int main(){
struct U0 uu;
uu.leading = 2;
uu.FLAG1 = 0;
uu.FLAG2 = 1;
uu.trailing = 0;
printf("sizeof(uu)=%lu\n", sizeof(uu));
prtBin(*(int*)&uu); // take uu The data in is forcibly converted to int type ( It was originally struct U0 type )
return 0;
}
void prtBin(unsigned int number)
{
unsigned mask = 1u<<31;
for(; mask; mask >>=1){
printf("%d", number & mask ? 1:0);
}
printf("\n");
}
Output results :
explain :
- Lowest 3 Position as uu.leading = 2, Next is uu.FLAG1 = 0,uu.FLAG2 = 1. Others are uu.trailing = 0.
- If in the structure definition trailing Change to occupy 28 position , Then the total number of structural digits exceeds 32 position , The compiler will add a int, Output sizeof(uu) = 8 .
4.3 Be careful
- After combining the special positioning into bit segments , It can be accessed directly by the member name of the bit segment . Specific shift 、 And 、 Or more convenient
- The editor will arrange the arrangement of bits , Not portable , For example, some editors start with the rightmost bit , Some start at the far left .
- When more than one bit is required int when , Multiple int.
边栏推荐
- Can Huatai Securities open an account online? Is it safe
- Seven sorts -- detailed explanation of ten thousand words
- Tsinghua, air, Tencent | 3D isovariant molecular map pre training
- Airtest脚本的点击位置与点击偏移
- This is how the permission system is designed, yyds
- The gospel of small and medium-sized enterprises is coming! Jnpf is becoming popular, helping business digital upgrading
- The method of page Jump in PHP
- 专题训练-链表
- Qt报错:错误 C2039 “Value“: 不是 “`global namespace‘“ 的成员
- 时间序列-数据集:电力变压器数据集 (ETDataset)
猜你喜欢
随机推荐
C語言——幾道C語言經典習題
jeecgboot 导入文档
C语言——几道C语言经典习题
【MySQL】游标「Cursor」
[node middle layer practice (IV)] ---- logical processing of express middle layer
【汇总篇】
Seven sorts -- detailed explanation of ten thousand words
什么是Per-Title编码?
How to build and use redis fragment cluster
C language -- several classic exercises of C language
XML的优缺点
Leetcode-99. restore binary search tree
Time series dataset: power transformer dataset (etdataset)
S2SH+mysql的在线英语学习系统
软考 系统架构设计师 简明教程 | 逆向工程
Android开发学习日记--内容提供者(跨应用间的数据库修改)
数据库范式与模式分解
从业务开发中学习和理解架构设计
[introduction to node basics] - node middle layer does interface forwarding to realize cross domain requests
实现城市治理一网统管,必须这 4 个关键技术









