清除缓存 c语言

Given a number and we have to 1) set a bit, 2) clear a bit and 3) toggle a bit.

给定一个数字,我们必须1)设置一个位,2)清除一个位,3)切换一个位。

1)设置一点 (1) Setting a bit)

To set a particular bit of a number, we can use bitwise OR operator (|), it sets the bits – if the bit is not set, and if the bit is already set, bit’s status does not change.

要设置数字的特定位,我们可以使用按位或运算符( | ),它设置位–如果未设置位,并且如果该位已设置,则位的状态不会改变。

Let suppose there is a number num and we want to set the Xth bit of it, then the following statement can be used to set Xth bit of num

让我们假设有一个数NUM,我们要设置X的这一点,那么下面的语句可以用来设置X的num

    num |= 1 << x;

2)清除一点 (2) Clearing a bit)

To clear a particular bit of a number, we can use bitwise AND operator (&), it clears the bit – if the bit is set, and if the bit is already cleared, bit’s status does not change.

要清除数字的特定位,我们可以使用按位AND运算符(&),清除该位–如果该位已设置,并且如果该位已被清除,则位的状态不会更改。

Let suppose there is a number num and we want to clear the Xth bit of it, then the following statement can be used to clear Xth bit of num

让我们假设有一个数NUM,我们要清除的 X的这一点,那么下面的语句可以用于清除X的num

    num &= ~(1 << x);

3)切换一点 (3) Toggling a bit)

To toggle a particular bit of a number, we can use bitwise XOR operator (^), it toggles the bit – it changes the status of the bit if the bit is set – it will be un-set, and if the bit is un-set – it will be set.

要切换某个数字的特定位,我们可以使用按位XOR运算符(^),它会切换该位–如果该位被设置,它将更改该位的状态–将被取消设置,并且如果该位未被设置, -set –将被设置。

Let suppose there is a number num and we want to toggle the Xth bit of it, then the following statement can be used to toggle Xth bit of num

让我们假设有一个数NUM,我们要切换X的这一点,那么下面的语句可以用来切换 X NUM位

    num ^= 1 << x;

Consider the following code – Here, we are performing all above mentioned operations

考虑以下代码–在这里,我们正在执行上述所有操作

#include <stdio.h>
int main(){//declaring & initializing an 1 byte number
/*
num (hex) = 0xAA
its binary = 1010 1010
its decimal value = 170
*/
unsigned char num = 0xAA;
printf("num = %02X\n", num);
//setting 2nd bit
num |= (1<<2);
//now value will be: 1010 1110 = 0xAE (HEX) = 174 (DEC)
printf("num = %02X\n", num);
//clearing 3rd bit
num &= ~(1<<3);
//now value will be: 1010 0110 = 0xA6 (HEX) = 166 (DEC)
printf("num = %02X\n", num);
//toggling bit 4th bit
num ^= (1<<4);
//now value will be: 1011 0110 = 0xB6 (HEX) = 182 (DEC)
printf("num = %02X\n", num);
//toggling bit 5th bit
num ^= (1<<5);
//now value will be: 1001 0110 = 0x96 (HEX) = 150 (DEC)
printf("num = %02X\n", num);
return 0;
}

Output

输出量

num = AA
num = AE
num = A6
num = B6
num = 96

Read more...

...

  • Hexadecimal literals in C language

    C语言的十六进制文字

  • Working with hexadecimal values in C language

    使用C语言处理十六进制值

  • How to check whether a bit is SET or not in C language?

    如何检查C语言中的SET是否设置?

  • unsigned char in C language

    C语言中的未签名字符

  • Bitwise operator programs in C

    C语言中的按位运算符程序

翻译自: https://www.includehelp.com/c/how-to-set-clear-and-toggle-a-single-bit-in-c-language.aspx

清除缓存 c语言

清除缓存 c语言_如何用C语言设置,清除和切换单个位?相关推荐

  1. python搭建自动化测试平台_如何用python语言搭建自动化测试环境

    原标题:如何用python语言搭建自动化测试环境 技术分享:基于Python语言的Web自动化测试环境搭建 近期发现很多初学者在学习自动化的过程当中,在环境安装环节总是出现问题,所以详细的出一篇环境搭 ...

  2. ie加载项存在残留是什么_如何用百度杀毒清除IE浏览器加载项残余

    如何用百度杀毒清除IE浏览器加载项残余?很久没有主动查杀毒了,清理了一下,突然发现IE浏览器有加载项残余,一探究竟. 方法/步骤 一般扫描到系统异常,都会仔细查看,寻根溯源,而不是随手清理了之,能自己 ...

  3. 如何下载c语言游戏,如何用C语言编写游戏.doc

    <如何用C语言编写游戏.doc>由会员分享,可在线阅读,更多相关<如何用C语言编写游戏.doc(71页珍藏版)>请在皮匠网上搜索. 1.如何用C语言编写游戏网络游戏是常谈的话题 ...

  4. 互联网最新开发语言_互联网是多语言的,但您需要学习普通话

    互联网最新开发语言 The internet is becoming the town square for the global village of tomorrow. - Bill Gates. ...

  5. c 语言画图片,如何用 C 语言画「心形」?

    原标题:如何用 C 语言画「心形」? 在我们IT行业每天面对的就是敲代码,所以很多人无法接受这份工作,因为很无聊也很枯燥,长期工作会使人情绪低落,其实我们编程很多时候也有有趣的地方,接下来我就用一个简 ...

  6. 浅谈python语言_浅谈Python语言基础知识点!

    一.Python 语言的简述 Python语言是一种解释型.面向对象的编程语言,是一种开源语言. Python属于动态类定义语言,也是一种强调类型语言. 二.Python 语言的特点 1.简单.高级 ...

  7. 如何用c语言读取硬盘串号_如何用C语言实现OOP

    我们知道面向对象的三大特性分别是:封装.继承.多态.很多语言例如:C++和Java等都是面向对象的编程语言,而我们通常说C是面向过程的语言,那么是否可以用C实现简单的面向对象呢?答案是肯定的!C有一种 ...

  8. python编程计算1!+2!+...+10!_如何用C语言编程计算 1!+2!+3!+…+10!?

    解决这个问题,首先要明白阶乘. n!=n*(n-1)*(n-2)*--*1:就是自己乘以自己减一,一直乘到一. 循环实现 这个过程必定是一系连续相乘的过程,一直重复着"乘"这个动作 ...

  9. c语言如何实现水平和垂直镜像_如何用C语言实现OOP

    点击上方蓝字关注我们 我们知道面向对象的三大特性分别是:封装.继承.多态.很多语言例如:C++和Java等都是面向对象的编程语言,而我们通常说C是面向过程的语言,那么是否可以用C实现简单的面向对象呢? ...

最新文章

  1. 关于一些Linux SVN的安装使用
  2. 【深度学习入门到精通系列】医学图像预处理—CLAHE变换代码
  3. (转)asp和asp.net区别
  4. Hadoop系列二:Hadoop单节点伪分布部署并执行mapreduce示例wordcount
  5. H5页面与ios交互返回上一级
  6. android list 比较,LinkedList 和 ArrayList 的区别
  7. c++变量的作用域、生存期和可见性
  8. ASP.NET Core管道深度剖析(3):管道是如何处理HTTP请求的?
  9. hanoi塔java_Java实现hanoi塔
  10. 系统 应用 数据缺一不可 云优化三步走
  11. pta 编程题10 Root of AVL Tree
  12. [短线是银]条件选股公式集源码!
  13. R 实现层次分析法确定指标权重
  14. Android前景怎么样、是不是要凉了、是不是应该考虑要转行?看完你就知道
  15. 烟大计算机考研二战,以坚持铺就考研之路——记经济管理学院考研优秀个人程林林...
  16. 反向代理是什么意思?正向代理和反向代理的区别是什么?
  17. Java学习lesson 10
  18. 数组最大值和最小值的求法
  19. 循环调用scrapy框架出现的问题:twisted.internet.error.ReactorNotRestartable,解决方法
  20. 风控术语手册(银行_消费金融信贷业务)_version6

热门文章

  1. 二级MS Office公共基础知识错题本(1)
  2. Qt图形界面编程入门(2)
  3. 眼图 非差分线_利用眼图解决USB在布线中的信号完整性问题
  4. ajax php接收不到数据库,PHP更新MySQL数据库与AJAX调用没有做任何事情
  5. go实现数组切片洗牌函数Shuffle
  6. Problem I: 函数---判断某年某月某日是这一年中的第几天
  7. 在加拿大读大学被开除了,以后该怎么办?
  8. 二分查找和二叉查找树
  9. Can’t Activate Reporting Services Service in SharePoint
  10. 在Linux下查看环境变量