HP C A.06.05 Reference Manual
Expressions and Operators
Bit Operators (<<, >>, &, ^, |, ~)
Chapter 5 93
Bit Operators (<<, >>, &, ^, |, ~)
Syntax
exp1
<<
exp2
Left shifts (logical shift) the bits in
exp1
by
exp2
positions.
exp1
>>
exp2
Right shifts (logical or arithmetic shift) the bits in
exp1
by
exp2
positions.
exp1
&
exp2
Performs a bitwise AND operation.
exp1
^
exp2
Performs a bitwise OR operation.
exp1
|
exp2
Performs a bitwise inclusive OR operation.
~exp1
Performs a bitwise negation (one's complement) operation.
Arguments
exp1
and
exp2
Any integer expression.
Description
The bit operators access speciļ¬c bits in an object. HP C supports the usual six bit operators,
which can be grouped into shift operators and logical operators.
Bit-Shift 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
short int to_the_left = 53, to_the_right = 53;
short int left_shifted_result, right_shifted_result;
left_shifted_result = to_the_left << 2;
right_shifted_result = to_the_right >> 2;
sets left_shifted_result to 212 and right_shifted_result to 13. The results are clearer
in binary:
base 2 base 10
0000000000110101 53
0000000011010100 212 /* 53 shifted left 2 bits */
0000000000001101 13 /* 53 shifted right 2 bits */