Source: http://www.cnblogs.com/mz121star/archive/2008/05/24/bit.html
Http://www.guigushi5.com/kongbumanhua/kongbumanhua_14057_3.html
I don't want to copy the full text. It's a waste of resources.
Excerpt:
1. Use the bitwise AND operator
Bitwise AND operator are generally used to select a specific bit or a group of BITs in an integer. To illustrate the meaning of this sentence, we will use the example starting with this section again to store the font features with a 16-bit integer.
Assume that a variable is declared and initialized, and a 12-pound font size, italic, and style 6 are specified. In fact, it is the font in Figure 3-1. The binary value of the style is 00000110, the Italic digit is 1, the black position is 0, and the font size is 01100. There is another unused bit. You need to initialize the font variable value to the binary number 0000 0110 0100 1100.
Because the four-bit binary number corresponds to a hexadecimal number, the simplest method is to specify the initial value in hexadecimal mode:
Unsigned short font = 0x064c; // style 6, italic, 12 point
Note:
The hexadecimal notation is more suitable than the decimal notation when a bitwise pattern like this is created.
To use the font size, you need to extract it from the font variable, which can be implemented using bitwise AND operators. Only when both bits are 1, bitwise AND operator will generate 1, so a value can be defined, select this bit when you perform the bitwise AND operation on the bit and font that defines the font size. Therefore, you only need to define a value. The value contains 1 in the bit we are interested in and 0 in the other bit. This value is called a mask and is defined using the following statement:
Unsigned short size_mask = 0x1f; // mask is 0000 0000 0001 1111
// To select size
The font variable's five low positions indicate the font size. Set these bits to 1 and the remaining bits to 0, in this way, they will be discarded (Binary 0000 0000 0001 1111 can be converted to hexadecimal 1f ).
Now we can use the following statement to extract the font size:
Unsigned short size = font & size_mask;
In the & operation, when the two corresponding bits are 1, The result bit is 1. The result of any other combination is 0. Therefore, the combined values are as follows:
Font 0000 0110 0100 1100
Size_mask 0000 0000 0001 1111
Font & size_mask 0000 0000 0000 1100
It is not important to break down binary values into a group of four bits. This is just easy to represent the corresponding hexadecimal number and shows the number of bits. The mask is used to separate the rightmost five digits. These five digits indicate the number of points (that is, the font size ).
You can use the same method to select a font style, but you also need to use the bitwise moving operator to move the style value to the right. You can use the following statement to define a mask and select the eight digits on the left, as shown below:
Unsigned short style_mask = 0xff00; // mask is 1111 1111 0000 0000
// For style
Use the following statement to obtain the style value:
Unsigned short style = (font & style_mask)> 8; // extract the style
The result of this statement is as follows:
Font 0000 0110 0100 1100
Style_masks 1111 1111 0000 0000
Font & style_mask 0000 0110 0000 0000
(Font & style_mask)> 8 0000 0000 0000
Define the mask for the bits that indicate italic and simhei, and set the corresponding bits to 1, it is easy to separate them. Of course, you also need to test the bit. For details, see chapter 4th.
Another purpose of bitwise AND operator is to disable bitwise. In the previous section, the bit with a mask of 0 will also output 0 in the result. For example, to disable a bit indicating italic, the other bit remains unchanged. You only need to define a mask so that the Italic bit in the mask is 0 and the other bit is 1, then, perform bitwise AND operations on the font variable and the mask. To do thisCodeIt is described in the bitwise OR operator section.