清除缓存 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) = 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...

阅读更多...

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

清除缓存 c语言

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值