Masking operations

Using a mask, multiple bits in a nibble, byte, words can be set either on, off or inverted from on to off (or vice versa) in a single bitwise operation.

More detail is in

https://en.wikipedia.org/wiki/Mask_(computing)

1) Masking bits to 1

To turn certain bits on, we need to use OR.

To make sure a bit is on, OR can be used as 1.

To make sure a bit is off, OR can be used as 0.

ex:

     0000 1111
OR   0010 0010
     ------------
     0010 1111

  

2) Masking bits to 0

To turn certain bits off, we need to use AND.

To make sure a bit is off, AND can be used as 0. 

ex: 

 

    0000 1111
AND 0010 0010
-------------
    0000 0010

  

3) Toggling bit values

If the original bit is 0, it returns 0 XOR 1 to 1.

If the original bit is 1, it returns 1 XOR 1 to 0.

EX:

    0000 1111
XOR 0010 0010
-------------
    0010 1101

转载于:https://www.cnblogs.com/KennyRom/p/6426136.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值