网站制作报价,乐清网络,版式设计排版,网站做等保是什么意思清除缓存 c语言Given a number and we have to 1) set a bit, 2) clear a bit and 3) toggle a bit. 给定一个数字#xff0c;我们必须1)设置一个位#xff0c;2)清除一个位#xff0c;3)切换一个位。 1)设置一点 (1) Setting a bit) To set a particular bit of a number,…清除缓存 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 | (12);
//now value will be: 1010 1110 0xAE (HEX) 174 (DEC)
printf(num %02X\n, num);
//clearing 3rd bit
num ~(13);
//now value will be: 1010 0110 0xA6 (HEX) 166 (DEC)
printf(num %02X\n, num);
//toggling bit 4th bit
num ^ (14);
//now value will be: 1011 0110 0xB6 (HEX) 182 (DEC)
printf(num %02X\n, num);
//toggling bit 5th bit
num ^ (15);
//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语言