当前位置:网站首页>对二进制的某一位操作
对二进制的某一位操作
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.读入数据


边栏推荐
- Storage mode of data in memory (C language)
- Endnote20 tutorial sharing (unfinished
- MySQL redo log redo log
- QT method of compiling projects using multithreading
- 301. 删除无效的括号
- 如何在 PHP 中进行日期格式验证检查(正则)
- junit单元测试报错org.junit.runners.model.InvalidTestClassError: Invalid test class ‘xxx‘ .No runnable meth
- WPF command directive and inotifypropertychanged
- 深度学习系列47:styleGAN总结
- Anti chicken soup speech
猜你喜欢

Swagger3 integrates oauth2 authentication token

407-栈与队列(232.用栈实现队列、225. 用队列实现栈)

How to migrate virtual machines from VirtualBox to hype-v

994. rotten oranges - non recursive method

WPF command directive and inotifypropertychanged

MySQL mvcc multi version concurrency control

Eureka

深度学习系列46:人脸图像超分GFP-GAN

Badly placed()'s problem

RFID数据安全性实验:C#可视化实现奇偶校验、CRC冗余校验、海明码校验
随机推荐
Xshell7 Download
MySQL的意向共享锁、意向排它锁和死锁
Side effects of threads in embedded real-time systems
899. 有序队列
316. remove duplicate letters
313. 超级丑数
897. 递增顺序搜索树
深度学习系列47:styleGAN总结
asp. Net file download demo and related problems
Summarized benefits
[system] right click the desktop icon. After turning around, the Explorer will crash and the desktop will be refreshed
XML schema record
MySQL redo log redo log
Solve the mining virus sshd2 (redis does not set a password and clear the crontab scheduled task)
301. 删除无效的括号
Intentional shared lock, intentional exclusive lock and deadlock of MySQL
[daily training] 513 Find the value in the lower left corner of the tree
309. 最佳买卖股票时机含冷冻期
MySQL index
MySQL Redo log Redo log