|
|
HP-UX/HP C Compiler: HP C/HP-UX Reference Manual > Chapter 5 Expressions and Operators Bit Operators (<<, >>, &, ^, |, ~) |
|
The bit operators access specific bits in an object. HP C supports the usual six bit operators, which can be grouped into shift operators and logical operators. The << and >> operators shift an integer left or right respectively. The operands must have integer type, and all automatic promotions are performed for each operand. For example, the program fragment
sets left_shifted_result to 212 and right_shifted_result to 13. The results are clearer in binary:
Shifting to the left is equivalent to multiplying by powers of two:
Shifting non-negative integers to the right is equivalent to dividing by powers of 2:
The << operator always fills the vacated rightmost bits with zeros. If exp1 is unsigned, the >> operator fills the vacated leftmost bits with zeros. If exp1 is signed, then >> fills the leftmost bits with ones (if the sign bit is 1) or zeros (if the sign bit is 0). In other words, if exp1 is signed, the two bit-shift operators preserve its sign.
Make sure that the right operand is not larger than the size of the object being shifted. For example, the following produces unpredictable and nonportable results because ints have fewer than 50 bits:
You will also get nonportable results if the shift count (the second operand) is a negative value. The logical bitwise operators are similar to the Boolean operators, except that they operate on every bit in the operand(s). For instance, the bitwise AND operator (&) compares each bit of the left operand to the corresponding bit in the righthand operand. If both bits are 1, a 1 is placed at that bit position in the result. Otherwise, a 0 is placed at that bit position. The bitwise AND operator performs logical operations on a bit-by-bit level using the following truth table: Table 5-1 Truth Table for the bitwise AND operator, (&)
The following table shows an example of the bitwise AND operator: Table 5-2 The Bitwise AND Operator
The bitwise inclusive OR operator performs logical operations on a bit-by-bit level using the following truth table: Table 5-3 Truth Table for the inclusive OR operator, (|)
The bitwise inclusive OR operator (|) places a 1 in the resulting value"s bit position if either operand has a bit set at the position. Table 5-4 Example Using the Bitwise Inclusive OR Operator
The bitwise exclusive OR operator performs logical operations on a bit-by-bit level using the following truth table: Table 5-5 Truth Table for the exclusive OR, ^
The bitwise exclusive OR (XOR) operator (^) sets a bit in the resulting value"s bit position if either operand (but not both) has a bit set at the position. Table 5-6 Example Using the XOR Operator
The bitwise complement operator (~) performs logical operations on a bit-by-bit level using the following truth table:
The bitwise complement operator (~) reverses each bit in the operand: Table 5-8 Example Using the Bitwise Complement Operator
|
|