当前位置:网站首页>Operation on a bit of binary
Operation on a bit of binary
2022-06-23 07:26:00 【Pick up the famous Rose】
List of articles
Preface
Today I will explain to you how to process a bit of binary 1 Or clear 0.
This chapter requires the use of the in place operator & and |.
If you don't understand, you can move to C Language – Operator details 
The circled ones are the ones to be used in this section
One 、 Analysis steps

Two 、 Use steps
1. Import and stock in
1. Set up 1
#define SETBIT(x,n) (x|=1<<(n-1))
void ShowBits(int x) {
int num = sizeof(x) * 8 - 1;// Minus one is because binary has 32 position The range of movement is 0~31 position
while (num >= 0)
{
if (x&1<<(num))
{
printf("1");
}
else
{
printf("0");
}
num--;
}
printf("\n");
}
int main() {
int x = 0;
SETBIT(x,5);// Place the fifth position 1
ShowBits(x);// Print every bit of binary
return 0;
}
2. Set up 0
#define CLRBIT(x,n) (x&=(~(1<<(n-1))))
void ShowBits(int x) {
int num = sizeof(x) * 8 - 1;// Minus one is because binary has 32 position The range of movement is 0~31 position
while (num >= 0)
{
if (x&1<<(num))
{
printf("1");
}
else
{
printf("0");
}
num--;
}
printf("\n");
}
int main() {
int x = 100;
CLRBIT(x, 6);// Zero the sixth position
ShowBits(x);// Print every bit of binary
return 0;
}
2. Read in the data


边栏推荐
- 309. the best time to buy and sell stocks includes the freezing period
- Nacos adapts to oracle11g- modify the source code of Nacos
- csrf攻击在laravel中如何解决
- 如何达到高效的网络信息传播
- npm下载报错npm ERR code ERESOLVE
- 控制台程序
- Difference between char and varchar
- [AI practice] xgb Xgbregression multioutputregressor parameter 1
- GINet
- 技术文章写作指南
猜你喜欢
随机推荐
leetcode210. 课程表 II 207. 课程表 拓扑排序 dfs bfs
Spock-sub打桩
295. median data flow
OSI分层模型对工作的具体帮助
干货来了|《PaaS》合辑抢先看~
MySQL (V) - locks and transactions
GINet
Several characteristics of MySQL database
MySQL(五) — 锁及事务
【AI实战】机器学习数据处理之数据归一化、标准化
Xshell7 Download
junit单元测试报错org.junit.runners.model.InvalidTestClassError: Invalid test class ‘xxx‘ .No runnable meth
407 stack and queue (232. implementing queue with stack, 225. implementing stack with queue)
316. remove duplicate letters
Technical article writing guide
Project_ Filter to solve Chinese garbled code
滚动播报效果的实现
MySQL(四) — MySQL存储引擎
Cirium has gradually become the standard for airlines' carbon dioxide emission reporting
About SQL: is there a way to fill in the null value in the field without adding fields on the basis of the original fields
![[game theory] basic knowledge](/img/eb/08b1ce5106e574dc42be58f72fbab9.jpg)







