Projection Transformation Derivation

Source: Internet
Author: User
Tags bitwise operators italic font

 

When modifying the bits in an integer, you can use four bitwise operators, as shown in Table 3-1. Table 3-1 bitwise operators
Operator Description
~ This is a bitwise inverse operator. It is a unary operator that can reverse the bit in the operand, that is, 1 is changed to 0, 0 is changed to 1
& This is a bitwise AND operator, it performs operations on the corresponding bits in the operands. If the corresponding bits are both 1, The result bit is 1, otherwise it is 0
^ This is a bitwise XOR or operator, it performs an exclusive or operation on the corresponding bits in the operand. If the corresponding bits are different, for example, if one bit is 1 and the other is 0, the result bit is 1. If the corresponding bit is the same, the result bit is 0
| This is a bitwise OR operator, it performs or operations on the corresponding bits in the operand. If one of the two corresponding bits is 1, The result bit is 1. If both digits are 0, the result is 0
Operators in Table 3-1 are arranged according to their priorities. In this set, bitwise inverse operators have the highest priority and bitwise operators have the lowest priority. In the operator priority table in appendix D, bitwise moving operators <and> have the same priority ~ Under the & operator. If you have never seen these operators before, you will ask "this is very interesting, but why ?". They are used for practice. 1. Using bitwise AND operator is generally used to select a specific bit or a group of digits 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 sizefont variable's five low positions indicate its font size, set these bits to 1 and the remaining bits to 0 so that they will be discarded (Binary Number 0000 0000 0001 1111 can be converted to hexadecimal number 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 0000 1100size_mask 0000 0001 1111 0000 font & size_mask 0000 0000 1100 it is not important to split binary values into 4-digit groups, this is only easy to represent the corresponding hexadecimal number and shows the number of digits. 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 statement: font 0000 0110 0100 1111 1100style_mask 1111 0000 0000 0000 0110 font & style_mask 0000 0000 (font & style_mask)> 8 0000 0000 0000 0110 defines the mask for the bits that represent italics and simhei, and sets the corresponding bits to 1, so 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 this Code It is described in the bitwise OR operator section. 2. You can use the bitwise OR operator to set one or more digits. To continue with the previous font variable, you need to set the italic and black positions. Use the following statement to define the mask and select these bits: Unsigned short italic = 0x40u; // seventh bit from the rightunsigned short bold = 0x20u; // sixth bit from the right use the following statement to set the black position: font | = bold; // The combination of set bold bits is as follows: font 0000 0110 0100 1100 bold 0000 0000 0010 0000 font | bold 0000 0110 0110 now, the font variable specifies that the font represents a bold and italic font. Note that this operation will set a bit, regardless of the previous status. If the previous bit status is on, it is still on. You can also perform bitwise OR operations on the mask and set multiple bitwise. The following statement sets both the bold and italic: font | = bold | italic; // set bold and italic, which makes it easy to select incorrect operators. Setting italics and simhei makes it easy to think that the & operator should be used, which is incorrect. Perform the bitwise AND operation on the two masks to get a value with 0 for all bits, which does not change any attributes of the font. As described at the end of the previous section, you can use the & operator to disable bits. That is, define a mask, set the bit to be disabled to 0, and set the other bit to 1. But how to specify such a mask? If you want to explicitly specify it, you need to know the number of bytes in the Variable. if you want Program It is not convenient to port data in any way. However, you can obtain this mask by using bitwise inverse operators on the mask that is usually used to open bits. Disable the black position on the bold mask to obtain the mask: bold 0000 0000 0010 ~ The bitwise Inverse Operator of bold 1111 1111 1101 1111 is used to reverse each bit of the original value and change 0 to 0. Whether the bold variable occupies 2 bytes, 4 bytes, or 8 bytes, this will generate the expected result. Tip: bitwise inverse operators are sometimes called not operators, because each bit of their operations will get a value different from the start. Therefore, when you disable the black position, you only need to perform bitwise AND operations on the reverse code and font variable of the mask bold. The available statements are as follows: font & = ~ Bold; // You can also use the & operator to combine several masks, and then perform the bitwise AND operation on the result and the variable to be modified, and set multiple digits to 0. Example: font & = ~ Bold &~ Italic; // turn bold and italic off this statement sets the italic and black positions in the font variable to 0. Note that parentheses are not required here, because ~ The operator has a higher priority than the & operator. However, if you do not know the operator priority, you should add parentheses to indicate the operation you want to perform. This must be harmless and can be used normally when brackets are required.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.