janmr blog

Bitwise Operators and Negative Numbers

When representing integers using a fixed number of bits, negative numbers are typically represented using two's complement. If using nn bit numbers, the two's complement of a number xx with 0x<2n0 \leq x < 2^n is (x)mod2n=2nx(-x) \mathbin{\text{mod}} 2^n = 2^n - x. But what do you do if you want to work with unbounded/multiple-precision integers? Fixing xx and letting the number of bits go to infinity, you will notice that increasing nn by one simply adds a 1 at the left. For instance,

  • 1975=(11110110111)21975 = (11110110111)_2
  • 1975=2121975=(100001001001)2-1975 = 2^{12} - 1975 = (100001001001)_2    (with n=12n=12)
  • 1975=2131975=(1100001001001)2-1975 = 2^{13} - 1975 = (1100001001001)_2    (with n=13n=13)
  • 1975=2201975=(11111111100001001001)2-1975 = 2^{20} - 1975 = (11111111100001001001)_2    (with n=20n=20)
  • 1975=(1111111111111100001001001)2-1975 = (\ldots 1111111111111100001001001)_2    (with n=n=\infty)

(This can be made more rigorous using 2-adic numbers). Conversely, every binary number with infinitely many 1s to the left corresponds to a negative integer.

Notice the important special case 1=(1111)2-1 = (\ldots 1111)_2. If x\overline{x} denotes bitwise not of xx, where each bit is flipped from 00 to 11 and vice versa, we observe that

x+x=1111=1,x + \overline{x} = \ldots 1111 = -1,

from which we have the important identity

x=1x.\overline{x} = -1 - x.

This makes bitwise not equivalent to a simple subtraction. Notice how bitwise not turns a non-negative integer into a negative integer and vice versa.

Let us turn to general bitwise operators. Consider a function that maps two bits to a single bit. Given such a function and two non-negative integers, we can apply the function to the zeroth bit of both numbers to obtain the zeroth bit of the result, then apply the function to the first bit of both numbers to obtain the first bit of the result, and so forth. In this way, any binary bit-operator {0,1}2{0,1}\{0,1\}^2 \mapsto \{0,1\} can be extended to work on any non-negative integer (and as we shall see, any integer). There are 16 possible binary bit-operators:

(x,y)(x, y) (0,0)(0, 0) (1,0)(1, 0) (0,1)(0, 1) (1,1)(1, 1)
0 00 00 00 00 00
1 x&yx \mathbin{\&} y 00 00 00 11
2 x&y\overline{x} \mathbin{\&} y 00 00 11 00
3 yy 00 00 11 11
4 x&yx \mathbin{\&} \overline{y} 00 11 00 00
5 xx 00 11 00 11
6 xyx \oplus y 00 11 11 00
7 xyx \mathbin{\mid} y 00 11 11 11
8 xy\overline{x \mathbin{\mid} y} 11 00 00 00
9 xy\overline{x \oplus y} 11 00 00 11
10 x\overline{x} 11 00 11 00
11 xy\overline{x} \mathbin{\mid} y 11 00 11 11
12 y\overline{y} 11 11 00 00
13 xyx \mathbin{\mid} \overline{y} 11 11 00 11
14 x&y\overline{x \mathbin{\&} y} 11 11 11 00
15 11 11 11 11 11

The first column of the table enumerates the functions from 0 to 15 (such that the binary representation of each number corresponds to the outputs). We see that exactly the functions 0–7 map (0,0)(0,0) to 00, meaning that only these functions will map two non-negative integers to a non-negative integer.

The second column shows expressions for the functions using the well-known operators bitwise and, x&yx \mathbin{\&} y, bitwise or (inclusive or), xyx \mathbin{|} y, bitwise xor (exclusive or), xyx \oplus y, and bitwise not, x\overline{x}. The table simultaneously define these operators.

We can now formulate the goal of this article: Using only the bitwise operators that map non-negative integers to non-negative integers, together with usual integer arithmetic, how can we implement all 16 functions? The approach is quite simple: Use bitwise not to transform any negative integer into a non-negative integer, apply one of the functions 0–7, and then possibly apply bitwise not again to obtain the result.

Before proceeding, we need some fundamental identities. First, symmetry:

x&y=y&x,xy=yx,xy=yx.x \mathbin{\&} y = y \mathbin{\&} x, \quad x \mathbin{|} y = y \mathbin{|} x, \quad x \oplus y = y \oplus x.

Then, De Morgan's laws:

x&y=xy,xy=x&y.\overline{x \mathbin{\&} y} = \overline{x} \mathbin{|} \overline{y}, \quad \overline{x \mathbin{|} y} = \overline{x} \mathbin{\&} \overline{y}.

Finally some useful rules for exlusive or:

xy=xy,xy=xy=xy.x \oplus y = \overline{x} \oplus \overline{y}, \quad \overline{x \oplus y} = \overline{x} \oplus y = x \oplus \overline{y}.

All of these are easily proved since they (by definition) operate bitwise. This means that you only have to consider one-bit numbers, which means only four different cases to check.

The only non-trivial operators among the functions 0–7 are x&yx \mathbin{\&} y, xyx \mathbin{|} y, xyx \oplus y, and x&yx \mathbin{\&} \overline{y}. We will use the notation x&y=x&yx \mathbin{\overline{\&}} y = x \mathbin{\&} \overline{y}. Note how &\mathbin{\overline{\&}} is not symmetric. The only non-trivial operators among the functions 8–15 are x&y\overline{x \mathbin{\&} y}, xy\overline{x \mathbin{|} y}, xyx \mathbin{|} \overline{y}, and xy\overline{x \oplus y}. Considering these eight cases, along with whether xx and yy are negative or not, we get the following table:

x0,y0x \geq 0, y \geq 0 x0,y<0x \geq 0, y < 0 x<0,y0x < 0, y \geq 0 x<0,y<0x < 0, y < 0
x&yx \mathbin{\&} y x&yx \mathbin{\&} y x&yx \mathbin{\overline{\&}} \overline{y} y&xy \mathbin{\overline{\&}} \overline{x} xy\overline{\overline{x} \mathbin{\mid} \overline{y}}
xyx \mathbin{\mid} y xyx \mathbin{\mid} y y&x\overline{\overline{y} \mathbin{\overline{\&}} x} x&y\overline{\overline{x} \mathbin{\overline{\&}} y} x&y\overline{\overline{x} \mathbin{\&} \overline{y}}
x&yx \mathbin{\overline{\&}} y x&yx \mathbin{\overline{\&}} y x&yx \mathbin{\&} \overline{y} xy\overline{\overline{x} \mathbin{\mid} y} y&x\overline{y} \mathbin{\overline{\&}} \overline{x}
xyx \oplus y xyx \oplus y xy\overline{x \oplus \overline{y}} xy\overline{\overline{x} \oplus y} xy\overline{x} \oplus \overline{y}
x&y\overline{x \mathbin{\&} y} x&y\overline{x \mathbin{\&} y} x&y\overline{x \mathbin{\overline{\&}} \overline{y}} y&x\overline{y \mathbin{\overline{\&}} \overline{x}} xy\overline{x} \mathbin{\mid} \overline{y}
xy\overline{x \mathbin{\mid} y} xy\overline{x \mathbin{\mid} y} y&x\overline{y} \mathbin{\overline{\&}} x x&y\overline{x} \mathbin{\overline{\&}} y x&y\overline{x} \mathbin{\&} \overline{y}
xyx \mathbin{\mid} \overline{y} y&x\overline{y \mathbin{\overline{\&}} x} xyx \mathbin{\mid} \overline{y} x&y\overline{\overline{x} \mathbin{\&} y} x&y\overline{\overline{x} \mathbin{\overline{\&}} \overline{y}}
xy\overline{x \oplus y} xy\overline{x \oplus y} xyx \oplus \overline{y} xy\overline{x} \oplus y xy\overline{\overline{x} \oplus \overline{y}}

Here, we have used only the identities shown earlier. Of course, we need to convert each bitwise not into a subtraction to complete the task. For instance, with x<0x < 0, y0y \geq 0 we have

xy=x&y=1((1x)&y).x \mathbin{|} \overline{y} = \overline{\overline{x} \mathbin{\&} y} = -1 - ((-1 - x) \mathbin{\&} y).

This way, the bitwise and-operation is being applied to non-negative numbers and we see that the result is always negative.

We can now, with assistance from the table above, apply any of the 16 binary bitwise operators to any pair of integers, without restricting ourselves to working with a fixed number of bits.

For further reading related to the binary representation of numbers, I recommend The Art of Computer Programming, Volume 4A, Section 7.1.3: Bitwise Tricks & Techniques by Donald E. Knuth and Hacker's Delight by Henry S. Warren, Jr.