清除缓存 c语言_如何用C语言设置,清除和切换单个位?
清除緩存 c語(yǔ)言
Given a number and we have to 1) set a bit, 2) clear a bit and 3) toggle a bit.
給定一個(gè)數(shù)字,我們必須1)設(shè)置一個(gè)位,2)清除一個(gè)位,3)切換一個(gè)位。
1)設(shè)置一點(diǎn) (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.
要設(shè)置數(shù)字的特定位,我們可以使用按位或運(yùn)算符( | ),它設(shè)置位–如果未設(shè)置位,并且如果該位已設(shè)置,則位的狀態(tài)不會(huì)改變。
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
讓我們假設(shè)有一個(gè)數(shù)NUM,我們要設(shè)置X的第這一點(diǎn),那么下面的語(yǔ)句可以用來(lái)設(shè)置X的num 個(gè)位
num |= 1 << x;2)清除一點(diǎn) (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.
要清除數(shù)字的特定位,我們可以使用按位AND運(yùn)算符(&),清除該位–如果該位已設(shè)置,并且如果該位已被清除,則位的狀態(tài)不會(huì)更改。
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
讓我們假設(shè)有一個(gè)數(shù)NUM,我們要清除的第 X的這一點(diǎn),那么下面的語(yǔ)句可以用于清除X的num 個(gè)位
num &= ~(1 << x);3)切換一點(diǎn) (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.
要切換某個(gè)數(shù)字的特定位,我們可以使用按位XOR運(yùn)算符(^),它會(huì)切換該位–如果該位被設(shè)置,它將更改該位的狀態(tài)–將被取消設(shè)置,并且如果該位未被設(shè)置, -set –將被設(shè)置。
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
讓我們假設(shè)有一個(gè)數(shù)NUM,我們要切換X的第這一點(diǎn),那么下面的語(yǔ)句可以用來(lái)切換第 X NUM位
num ^= 1 << x;Consider the following code – Here, we are performing all above mentioned operations
考慮以下代碼–在這里,我們正在執(zhí)行上述所有操作
#include <stdio.h>int main(){//declaring & initializing an 1 byte number/*num (hex) = 0xAAits binary = 1010 1010its 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 bitnum ^= (1<<4);//now value will be: 1011 0110 = 0xB6 (HEX) = 182 (DEC)printf("num = %02X\n", num);//toggling bit 5th bitnum ^= (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 = 96Read more...
...
Hexadecimal literals in C language
C語(yǔ)言的十六進(jìn)制文字
Working with hexadecimal values in C language
使用C語(yǔ)言處理十六進(jìn)制值
How to check whether a bit is SET or not in C language?
如何檢查C語(yǔ)言中的SET是否設(shè)置?
unsigned char in C language
C語(yǔ)言中的未簽名字符
Bitwise operator programs in C
C語(yǔ)言中的按位運(yùn)算符程序
翻譯自: https://www.includehelp.com/c/how-to-set-clear-and-toggle-a-single-bit-in-c-language.aspx
清除緩存 c語(yǔ)言
總結(jié)
以上是生活随笔為你收集整理的清除缓存 c语言_如何用C语言设置,清除和切换单个位?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【万字长文】Spring Cloud A
- 下一篇: Java LinkedList bool