Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

1Learning Outcomes

So far, we have learned about arithmetic operations involving binary numbers. In this section, we will learn about bitwise operations.

2Bitwise Operations

We denote the bitwise operations AND, OR, XOR, and NOT as the operators &, |, ^, and ~, respectively. Supposing a and b are single-bit values, the following truth tables specify the result y of each operation.[1]

Table 1 is bitwise AND. a & b is 1 only if both a and b are 1. Otherwise, it is 0.

Table 1:AND: y = a & b

aby
000
010
100
111

Table 2 is bitwise OR. a | b is 1 only if either a or b are 1. Otherwise, it is 0.

Table 2:OR: y = a | b

aby
000
011
101
111

Table 3 is bitwise NOT. It is a unary operator because it only takes one operand. ~a is 1 only if a is 0. If a is 1, then ~a is 0.

Table 3:NOT: y = ~a

ay
01
10

Table 4 is bitwise XOR (“exclusive OR”). a ^ b is 1 only if one of a and b is 1. Otherwise if both a and b are 0 or 1, a^b is 0.

Table 4:XOR: y = a ^ b

aby
000
011
101
110

2.1Properties of Bitwise Operations

The below properties in Table 5 hold for a single-bit value x. We leave the proofs to you.

Table 5:Properties of bitwise operations AND, OR, and XOR.

Bitwise OperationExampleResultColloquially
ANDx & 00set to 0
ANDx & 1xkeep/pass-through
ORx & 0xkeep/pass-through
ORx & 11set to 1
XORx & 0~xflip/invert
XORx & 1xkeep/pass-through

Because of its behavior, we also call XOR a “conditional inverter”. We discuss this more when we design logic gates.

3C: Bitwise Operations vs. Logical Operations

The bitwise operators &, |, and ~ are used in C. With n-bit operands, bitwise operations are performed on the binary numeral(s) one bit at a time; the result’s bitwidth depends on the input operands. See Table 6 for examples on 8-bit char values.

Table 6:Bitwise operation examples with 8-bit char values.

Bitwise OperationC exampleResult
AND0b00001001 & 0b000000110b00000001
OR0b00001001 | 0b000000110b00001011
XOR0b00001001 ^ 0b000000110b00001010
NOT~0b000010010b11110110

However, note that C bitwise operators should not be confused with the C logical operators &&, ||, and !. By contrast, Logical operations translate bit values to truthy and and falsy values. These types of operations are also known as boolean compound operators[2].

4Bitmasks

This is our first foray into bitwise operations. Why do we care?

Remember that n bits can represent 2n2^n things, and we often want to use bits to represent many more things than just numbers. Suppose we wanted to use bit patterns to represent whether n things were present. We could then use each of our n bits to represent whether each thing was present (1) or not 0.

Using bitwise operations (and the properties of such bitwise operations) will be very convenient for updating state using bit patterns called bitmasks.

Bitwise operations will be very useful for boolean logic later on when we introduce logic gates.

5More C Bitwise Operators: Left and Right Shift

Two additional operations describe bit shifts. We will revisit bit shifting operations later in the course. For now, we define them for the sake of being comprehensive.

Suppose you have the 8-bit bit patterns (where we put spaces between nibbles for readability):

Left shift x << n shifts the bits of x left, filling the lower bits coming in from the left with 0’s. Mathematically, this is equivalent to multiplying x by 2n2^{\texttt{n}}. For example, x << 3 gives the bit pattern 0000 1000, where the leftmost 1 gets “shifted out.”

Right shift, x >> n shifts the bits of x right. Mathematically, this is equivalent to taking the floor of a division by $2^{\texttt{n}}4 We will still need to fill in the top bits coming in from the right somehow, but the precise operation in C depends on x’s type.

Footnotes
  1. The name “truth table” comes from boolean logic itself. We discuss this later in logic design, when it is useful to represent signals as “high” or “low”.

  2. In Python, the boolean operators are and, or, and not. Python also supports bitwise operators &, |, ~, and ^.