In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
What this article shares with you is about the specific use of the bit operator in C language. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.
Catalogue
Boolean bit operator
Shift operator
For more compact data, C programs can store information in separate bits or multiple combined bits. File access license is a common application case. The bit operator allows processing of independent bits in a byte or larger unit of data: you can clear, set, or invert any bit or more bits. You can also move the bit pattern (bit pattern) of an integer to the right or left.
The bit pattern of the integer type consists of a sequence of bits numbered from right to left by position, starting with 0, which is the least significant bit (least significant bit). For example, consider the character value'*', which has an ASCII code of 42, which is equivalent to 101010 of binary:
Bit mode 0 0 1 0 1 0 1 0
Bit position 7 6 5 4 3 2 1 0
In this case, the value 101010 is represented as an 8-bit byte, so there are two more zeros in front of it.
Boolean bit operator
The operators listed in Table 1 can perform Boolean operations on each bit of the Operand. This binary operator associates bits in the same position in two different operands. The bits that are set (that is, bits with a value of 1) are interpreted as true, and the bits that are cleared (that is, bits with a value of 0) are interpreted as false.
In addition to the Boolean operators AND, OR, and NOT, there are also bit XOR operators (exclusive-OR,XOR). These are listed in Table 1.
Operator meaning example for the result of each bit position (1 = set, 0 = clear) & bit ANDx&y if both x and y are 1; if either x or y is 0, or both 0, it gets 0 | bit ORx | y if x or y is 1, or both are 1; if x and y are both 0, you get 0 ^ bit XORx^ y if the values of x or y are different, you get 1 If the two values are the same, then 0 ~ bit NOT (complement of I) ~ x if x is 0, 1 is obtained, and if x is 1, 0 is obtained
Table 1 Boolean bit operator
The operands of bit operators must be of integer type and follow normal arithmetic transformations (usualarithmetic conversion). The common type of the Operand obtained after conversion is the type of the entire calculation result. Table 2 shows the effect of these operators.
Expression (or declaration) bit pattern int aq6; 0 ·00110int bau11; 0 01011a & b0 00010a | b0 01111a ^ b0 01101 ~ a1 11001
Table 2 effects of bit operators
You can clear the special location of an integer a by performing a bit AND operation on the integer an and another integer, where the other integer is 0 in the bit that needs to be cleared, and the other bit is 1, and the bit AND operation, where the other integer is 0 in the bit that needs to be cleared, and the other bit is 1, and assigns the result of the AND operation to the integer a.
The other integer, the second Operand of the bit AND operation, is set to a position of 1 (called a bitmask), which is operated by a bit AND and does not change the value of the corresponding position of the first Operand. For example, when an integer performs a bit AND operation with a bitmask 0xFF, the 8 bits of the lowest position are retained, while the values of all other bits are cleared:
A & = OxFF; / / is equivalent to: a = a & OxFF
In this example, the compound assignment operator & = also performs the & operation. The compound assignment operator performs in a similar manner to other binary operators, which I will not repeat here.
The bit operator can also be used to generate a bit mask for later bit operations. For example, in the in-place mode 0x20, only bit 5 is set. So the expression ~ 0x20 generates a bitmask with only bit 5 not set:
A & = ~ 0x20; / / clear bit 5 in a
The bitmask ~ 0x20 is more popular than 0xFFFFFFDF because it is more portable: the result is not affected by machine word size (and it is also easier for people to read).
You can also use the operators | (OR) and ^ (XOR) to set or clear special positioning. Here is an example:
Int mask = OxC;a | = mask; / / set bit 2 and bit 3a of a ^ = mask; / / invert bit 2 and bit 3 of a
The second transformation uses the same bitmask, which reverses the results of the first conversion again. In other words, b ^ mask gets the original value of b. This operation can be used to exchange the values of two integers without using a third temporary variable:
A ^ = b; / equivalent to a = a ^ b / b ^ = a; / assign the original value of a to ba ^ = b; / / assign the original value of b to a
The first two expressions in this example are equivalent to b = b ^ (a ^ b) or b = (a ^ b) ^ b. The result is equivalent to bicona, and the side effect is that the value of an is also modified to a ^ b. At this point, the third expression has the following side effects: a = (a ^ b) ^ an or aqb (using the original values of an and b).
Shift operator
The shift operator shifts the bit pattern of the left Operand by several positions, and several positions are specified by the right Operand. They are listed in Table 3.
Operator meaning sample results each bit of yx moves y bits to the right
Table 3 shift operator
The Operand of the shift operator must be an integer. Before the actual shift operation, both operands are promotion. The right Operand cannot be negative and must be less than the bit length of the left Operand after integer promotion. If these conditions are not met, the running result of the program cannot be determined.
The type of the result of the shift operation is equal to the type of the left Operand after the integer is raised. The shift expression for the following example has the type unsigned long.
Unsigned long n = 0xB, / / bit pattern: 0... 0 0 0 1 1 1 result = 0 position result = n > 2; / 0. 0 000 0 0 0
In the left shift operation, the extra bits on the right are filled with 0. Moving bits beyond the left boundary is discarded directly. Moving y positions to the left is equivalent to multiplying the left Operand by 2 ^ {y}: if the left Operand x is unsigned, then the expression x
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.