HP C A.06.05 Reference Manual

Expressions and Operators
Bit Operators (<<, >>, &, ^, |, ~)
Chapter 594
Shifting to the left is equivalent to multiplying by powers of two:
x << y is equivalent to x * 2
y
.
Shifting non-negative integers to the right is equivalent to dividing by powers of 2:
x >> y is equivalent to x / 2
y
.
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.
NOTE Not all compilers preserve the sign bit when doing bit-shift operations on
signed integers. The K&R language definition and the ANSI standard make
this behavior implementation-defined.
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:
10 >> 50
You will also get nonportable results if the shift count (the second operand) is a negative
value.
Bit Logical Operators
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.
Bitwise AND (&) Operator 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, (&)
bit x of op1 bit x of op2 bit x of result
000
010
100