HPlogo HP C/HP-UX Reference Manual: Version A.05.55.02 > Chapter 5 Expressions and Operators

Bit Operators (<<, >>, &, ^, |, ~)

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

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 specific 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 */

Shifting to the left is equivalent to multiplying by powers of two:

x << y is equivalent to x * 2y.

Shifting non-negative integers to the right is equivalent to dividing by powers of 2:

x >> y is equivalent to x / 2y.

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

0

0

0

0

1

0

1

0

0

1

1

1

 

The following table shows an example of the bitwise AND operator:

Table 5-2 The Bitwise AND Operator

Expression

Hexadecimal Value

Binary Representation

9430

5722

0x24D6

0x165A

00100100 11010110

00010110 01011010

9430 & 5722

0x0452

00000100 01010010

 

Bitwise Inclusive (|) OR

The bitwise inclusive OR operator performs logical operations on a bit-by-bit level using the following truth table:

Table 5-3 Truth Table

bit x of op1

bit x of op2

bit x of result

0

0

0

0

1

1

1

0

1

1

1

1

 

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

Expression

Hexadecimal Value

Binary Representation

9430

5722

0x24D6

0x165A

00100100 11010110

00010110 01011010

9430 | 5722

0x36DE

00110110 11011110

 

Bitwise exclusive OR (^)

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, ^

bit x of op1

bit x of op2

bit x of result

0

0

0

0

1

1

1

0

1

1

1

0

 

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

Expression

Hexadecimal Value

Binary Representation

9430

5722

0x24D6

0x165A

00100100 11010110

00010110 01011010

9430 ^ 5722

0x328C

00110010 10001100

 

Bitwise Complement (~)

The bitwise complement operator (~) performs logical operations on a bit-by-bit level using the following truth table:

Table 5-7 Truth table for the ~, Bitwise Complement

bit x of op2

result

0

0

0

1

 

The bitwise complement operator (~) reverses each bit in the operand:

Table 5-8 Example Using the Bitwise Complement Operator

Expression

Hexadecimal Value

Binary Representation

9430

0x24d6

00100100 11010110

~9430

0xdb29

11011011 00101001

 

© Hewlett-Packard Development Company, L.P.