清除缓存 c语言_如何用C语言设置,清除和切换单个位?
清除緩存 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) = 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語言的十六進制文字
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语言设置,清除和切换单个位?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【万字长文】Spring Cloud A
- 下一篇: 4 种方法!检查字符串是否为合法的日期格