Python mask

11.bit mask
a bit mask can help you turn specific bits on,turn other off,or just collect data from a integer about which bits are on or off.

mask = 0b1000
the fourth bit from the right is on
mask =0b0100
third bit from the right is on

13.just flip out
want to flip all of the bits in a
a = 0b110
mask = 0b111
a 与 mask 的长度一样,只是mask 全是1

12.turn it on
want to make sure the rightmost bit of number a is turned on

a = 0b110

6

mask = 0b1

1

desired = a | mask

0b111,or 7

using the bitwise | operator will turn a corresponding bit on if it is off and leave it on if it is already on

the bitwise OR(|) operator compares two numbers on a bit level and returns a number where the bits of that number are turned on if either of the corresponding bits of either number are 1.

你可能感兴趣的:(Python mask)