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
posted @ 2017-02-21 20:45  KennyRom  阅读(223)  评论(0编辑  收藏  举报