当前位置:网站首页>对二进制的某一位操作
对二进制的某一位操作
2022-06-23 06:21:00 【拾至灬名瑰】
前言
今天给大家讲解关于对二进制的某一位进行处理置1或者清0.
本章需要用到位运算符&和|。
不懂的话可以移步到C语言–操作符详解
圈出来的就是本节需要使用的
一、分析步骤

二、使用步骤
1.引入库
1.置1
#define SETBIT(x,n) (x|=1<<(n-1))
void ShowBits(int x) {
int num = sizeof(x) * 8 - 1;//减一是因为二进制一共有32位 移动的范围是0~31位
while (num >= 0)
{
if (x&1<<(num))
{
printf("1");
}
else
{
printf("0");
}
num--;
}
printf("\n");
}
int main() {
int x = 0;
SETBIT(x,5);//将第五位置1
ShowBits(x);//打印二进制的每一位
return 0;
}
2.置0
#define CLRBIT(x,n) (x&=(~(1<<(n-1))))
void ShowBits(int x) {
int num = sizeof(x) * 8 - 1;//减一是因为二进制一共有32位 移动的范围是0~31位
while (num >= 0)
{
if (x&1<<(num))
{
printf("1");
}
else
{
printf("0");
}
num--;
}
printf("\n");
}
int main() {
int x = 100;
CLRBIT(x, 6);//将第六位置零
ShowBits(x);//打印二进制的每一位
return 0;
}
2.读入数据


边栏推荐
- C DPI adaptation problem
- Open source oauth2 framework for SSO single sign on
- Analysis of personalized learning progress in maker Education
- MySQL basic query
- About Supervision
- GloRe
- [saison de remise des diplômes · technologie agressive er] votre choix, agenouillez - vous et partez
- 994. rotten oranges - non recursive method
- 正则表达式图文超详细总结不用死记硬背(上篇)
- Regular expression graph and text ultra detailed summary without rote memorization (Part 1)
猜你喜欢

Configuration and compilation of mingw-w64, msys and ffmpeg

Influence of steam education on domestic college students
![[STL] summary of pair usage](/img/ba/72697f0f8bf018f1b5884e9cc2be4e.png)
[STL] summary of pair usage

Cloud box is deeply convinced to create a smart dual-mode teaching resource sharing platform for Nanjing No. 1 middle school

excel高级绘图技巧100讲(八)-Excel绘制WIFI图

直播回顾 | 传统应用进行容器化改造,如何既快又稳?

数据在内存中的存储方式(C语言)

MySQL optimization

PSP代码实现

云原生落地进入深水区,博云容器云产品族释放四大价值
随机推荐
为什么TCP协议是三次握手而不是两次?
322. change exchange
在金融行业做数据产品经理是什么体验
GloRe
300. 最长递增子序列
[graduation season · advanced technology Er] it's my choice. I have to walk on my knees
asp. Net file download demo and related problems
Xshell7 Download
ssm + ftp +ueditor
Cetos7 record
MySQL optimization
.h5文件忘记数据库名字,使用h5py打印
SSM整合
【STL】关联容器之unordered_map用法总结
cmder
20220621 Three Conjugates of Dual Quaternions
云原生落地进入深水区,博云容器云产品族释放四大价值
Cloud box is deeply convinced to create a smart dual-mode teaching resource sharing platform for Nanjing No. 1 middle school
RFID数据安全性实验:C#可视化实现奇偶校验、CRC冗余校验、海明码校验
【日常训练】513. 找树左下角的值