www.cnblogs.com/zichi/p/4792589.html
We've already learned about the six-bit operators (& | ~ ^ << >> usage (JavaScript bit operation), also sorting out some commonly used bitwise operation operations (commonly used bit operation collation), we continue to delve into the bit operations to understand the next binary classical applications-the logo bit and mask.
Bit operations are often used to create, process, and read flag bit sequences--a binary-like variable. Although you can use variables instead of flag bit sequences, you can save memory (1/32).
For example, there are 4 flag bits: sign A: We have orange sign bit B: We have the apple logo bit C: We have the banana logo bit d: we have pear
The flag bit is represented by a bit sequence DCBA, when one position is set to 1 o'clock, indicating that the item is, and is set to 0 o'clock, indicating that there is no such item. For example, a variable flag=9, a binary representation of 1001, means that we have D and a.
A mask (bitmask) is a sequence of bits that reads a sign bit by and/or. A typical primitive mask that defines each flag bit is as follows:
var flag_a = 1; 0001
var flag_b = 2;//0010
var flag_c = 4;//0100
var flag_d = 8;//1000
The new mask can be created using logical operations on the above mask. For example, mask 1011 can be obtained by flag_a, Flag_b, and flag_d logic:
var mask = flag_a | Flag_b | Flag_d; 0001 | 0010 | 1000 => 1011
A particular bit can be obtained by logic and operation with the mask, and by the operation of the mask, the extraneous bits can be removed. For example, mask 0100 can be used to check whether the flag bit C is set: ( the core is to judge a bit of the number of reference common bit operation collation)
If we had banana if
(Flags & Flag_c) {//0101 & 0100 => 0100 => true
//do stuff
}
A mask that has multiple bit bits to express any/or meaning. For example, the following two expressions are equivalent:
If we have Apple or banana at least one
//(0101 & 0010) | | (0101 & 0100) => 0000 | | 0100 => true
if (Flags & Flag_b) | | (Flags & Flag_c)) {
//do stuff
}
var mask = Flag_b | Flag_c; 0010 | 0100 => 0110
if (Flags & mask) {//0101 & 0110 => 0100 => true
//do stuff
}
You can set the corresponding bit by setting a flag bit with the mask or an operation, and a bit of 1 in the mask. For example, mask 1100 can be used to set bits C and D: ( The core is to change a bit to 1 )
We have banana and pear
var mask = Flag_c | Flag_d; 0100 | 1000 => 1100
flags |= mask; 0101 | 1100 => 1101
You can clear the flag bit with the mask, and the bits in the mask 0 can set the corresponding bit. The mask can be obtained by doing a non operation on the primitive mask. For example, Mask 1010 can be used to clear flags A and C: ( The core is to change a bit to 0)
We have no orange and no banana
var mask = ~ (Flag_a | Flag_c); ~0101 => 1010
flags &= mask; 1101 & 1010 => 1000
The mask above can also be obtained by ~flag_a & ~flag_c (Demogan law):
We have no orange and no banana
var mask = ~flag_a & ~flag_c;
Flags &= Mask; 1101 & 1010 => 1000
The flag bits can be switched using XOR or operation. All values of 1 are able to toggle the corresponding bit. For example, mask 0110 can be used to toggle flag bits B and C: ( the core is to reverse a bit )
If we didn't have Apple before, then we now have Apple
//But if we already have one, then now there is no
//the same situation for banana
var mask = Flag_b | Flag_c;
Flags = flags ^ mask; 1100 ^ 0110 => 1010
Finally, all flag bits can be flipped by a non operation:
Entering Parallel universe
... Flags = ~flags; ~1010 => 0101
may be the most detailed underscore source analysis in history:Https://github.com/hanzichi/underscore-analysis
programmers should learn some algorithms:Https://github.com/hanzichi/leetcode
Learn about Bloggers Han Zixuan:Http://www.cnblogs.com/zichi/p/about.html
GitHub:Https://github.com/hanzichi
Follow landlord to the landlord more writing power