Bitwise Operators


Bitwise operators operate on the individual bits of integer operands. They are available only for integers and are not available for the floating point data type. The available bitwise operators are shown in the table below.

Operator Description
& bitwise and
| bitwise or
^ bitwise exclusive or (xor)
>> bitwise right shift
<< bitwise left shift
~ bitwise not (unary)

The bitwise and, or, xor and not operators

The bitwise and, or, xor and not operators are (respectively) &, |, ^ and ~. They perform a similar function to their logical equivalents but on the bit level rather than on a macroscopic level. The truth tables for these operators are shown below.

p q p&q p|q p^q ~p
0 0 0 0 0 1
0 1 0 1 1 1
1 0 0 1 1 0
1 1 1 1 0 0

Each truth table is applied to individual bits or bit pairs in the operand or operands.

One use for and is to turn bits off. For example, consider the following operatioin, where the 3rd bit is turned off.

1 1 1 1  1 0 1 1 &      // bit patern to turn 3rd bit off   0xfb &
1 0 1 0  1 1 0 1        // arbitrary  bit patern            0xad =
----------------                                            ----
1 0 1 0  1 0 0 1        // 3rd bit switched odd             0xa9
----------------                                            ----

Exclusive or (^) has the interesting property that if it is applied twice, the original pattern is returned. This fact is used in a variety of situations.

The next program demonstrates the above facts.

// bitwise_operators.txt - bitwise operators

generic bitwise_operators
{
    bitwise_operators()
    {
        i = 0xfb     // 1 1 1 1  1 0 1 1 &
        l = 0xad     // 1 0 1 0  1 1 0 1 = 
        n = i & l    // 1 0 1 0  1 0 0 1  (0xa9)

        s = i.to_string() + " & " + l.to_string() + " = " + n.to_string()
        s.println()

        o = i ^ l
        s = i.to_string() + " ^ " + l.to_string() + " = " + o.to_string()
        s.println()

        p = o ^ l
        s = i.to_string() + " ^ " + l.to_string()  + " ^ " + l.to_string() + " = " + p.to_string()
        s.println()
    }
}

The output of the program is shown below.

251 & 173 = 169
251 ^ 173 = 86
251 ^ 173 ^ 173 = 251

The Shift Operators

The shift operators shift the individual bits of which an integer consists left or right by a specified amount. The two bit shift operators are shown below.

operator description
>> bitwise right shift
<< bitwise left shift

The general form of these operators is shown below:

value << number-of-bits
value >> number-of-bits

where value is the integer value being shifted by the specified number of bits number-of-bits. When shifting to the left, zero bits are brought in from the right to fill vacant positions. When an integer is being right shifted, the sign bit is preserved and vacant positions are therefore filled with the sign bit. For both left and right shifts, the bits shifted out of the number are lost.

Left shifting a number by one position doubles the value of that number. Therefore, left shifts are a rapid nneans of multipiling numbers by powers of 2.