Section 2.12

Signed versus unsigned comparison

Definition

A signed comparison and an unsigned comparison are two ways to order the same fixed-width bit patterns. An unsigned comparison reads a pattern of ww bits as a whole number from 0 to 2w−12^w - 1. A signed comparison reads it in two’s complement, the standard encoding of negative integers, in which the highest bit counts as −2w−1-2^{w-1}, so values run from −2w−1-2^{w-1} to 2w−1−12^{w-1} - 1. The two agree on which patterns are equal but not on their order: the 8-bit pattern 11111111 is 255 unsigned and −1-1 signed.

The idea

A program checks a number from outside before using it as an index, the position of an element in an array §14.5. With 8-bit numbers and 16 elements, the valid indices are 0 to 15. The pattern 11111111 arrives. Read as signed it is −1-1, and −1<16-1 < 16, so a signed test “index less than 16” accepts it. The access then uses index 255 if it reads the bits as unsigned, or index −1-1, the element just before the array, if it reads them as signed. No bit changed; only the rule for reading them did.

Picture the 256 patterns of 8 bits around a clock face in counting order, with 11111111 next to 00000000. Ordering them means cutting the circle open into a line. Unsigned comparison cuts between 11111111 (255) and 00000000 (0); signed comparison cuts between 01111111 (127) and 10000000 (−128-128). Patterns with the same highest bit come out in the same order either way, and patterns with different highest bits in opposite orders. The picture stops at the cut: equality, addition and subtraction do not depend on it.

How it works

Number the bits of a ww-bit pattern from 0, the lowest, to w−1w - 1, the highest. Unsigned, bit ii is worth 2i2^i. Two’s complement keeps these worths except for the highest bit, which is worth −2w−1-2^{w-1} instead of +2w−1+2^{w-1}. For w=2w = 2 the bits are worth 2 and 1 unsigned, and −2-2 and 1 signed:

pattern unsigned signed
00 0 0
01 1 1
10 2 −2-2
11 3 −1-1

A pattern whose highest bit is 1 is large unsigned and negative signed. Changing the worth of that bit from +2w−1+2^{w-1} to −2w−1-2^{w-1} lowers the value by 2w2^w, here 4: unsigned 3 becomes 3−4=−13 - 4 = -1.

The bits do not say which reading applies; the operation does.

A bounds check must use the reading of the later access, or exclude both ends. For an index xx and an array of nn elements, either test 0≤x0 \le x and x<nx < n as signed numbers, or make one unsigned test x<nx < n. The single unsigned test suffices because a pattern that is negative when signed has its highest bit set, so its unsigned value is at least 2w−12^{w-1}, which is not less than nn when n≤2w−1n \le 2^{w-1}.

Worked example

Take w=8w = 8. Unsigned, the bits are worth 128, 64, 32, 16, 8, 4, 2 and 1; signed, the highest is worth −128-128. With 64+32+16+8+4+2+1=12764 + 32 + 16 + 8 + 4 + 2 + 1 = 127, the pattern 11111111 is 128+127=255128 + 127 = 255 unsigned and −128+127=−1-128 + 127 = -1 signed. The pattern 10000000 is 128 unsigned and −128-128 signed, and 00000101 is 4+1=54 + 1 = 5 both ways. Against an array of 16 elements, the signed test x<16x < 16 alone accepts all three patterns. The signed pair 0≤x0 \le x and x<16x < 16 accepts only the pattern of 5. The unsigned test x<16x < 16 rejects 255 and 128, so it too accepts only the pattern of 5.

With 64-bit registers the same happens. Register R1 holds 0xffffffffffffffff, which is 264−1=18,446,744,073,709,551,6152^{64} - 1 = 18{,}446{,}744{,}073{,}709{,}551{,}615 unsigned and −1-1 signed. An eBPF program guards an access with the signed test if r1 s> 15 goto reject. Since −1>15-1 > 15 is false, the program goes on and adds R1 to the address of a 16-byte buffer. Adding 264−12^{64} - 1 modulo 2642^{64} subtracts 1, so the access reads the byte just before the buffer. The unsigned test if r1 > 15 goto reject jumps, since 264−1>152^{64} - 1 > 15. With R1 equal to 5 or 16 the two tests agree, so only a value with the highest bit set reveals the difference.

Stated precisely

Let xx be a pattern of ww bits bw−1…b1b0b_{w-1} \dots b_1 b_0, where bw−1b_{w-1} is the highest bit. Its unsigned value and its two’s-complement signed value are u(x)=∑i=0w−1bi2i,s(x)=u(x)−bw−12w,u(x) = \sum_{i=0}^{w-1} b_i \, 2^i, \qquad s(x) = u(x) - b_{w-1} \, 2^w, with 0≤u(x)≤2w−10 \le u(x) \le 2^w - 1 and −2w−1≤s(x)≤2w−1−1-2^{w-1} \le s(x) \le 2^{w-1} - 1. The unsigned comparison orders patterns by uu, the signed comparison by ss. For patterns xx and yy of the same width:

  1. u(x)=u(y)u(x) = u(y) exactly when s(x)=s(y)s(x) = s(y), which happens exactly when xx and yy are the same pattern.

  2. If xx and yy have the same highest bit, u(x)<u(y)u(x) < u(y) exactly when s(x)<s(y)s(x) < s(y), since ss subtracts the same amount from both. If their highest bits differ, the pattern whose highest bit is 1 is the larger one unsigned and the smaller one signed.

  3. For a bound nn with 0≤n≤2w−10 \le n \le 2^{w-1}, u(x)<nu(x) < n holds exactly when 0≤s(x)<n0 \le s(x) < n holds: if bw−1=0b_{w-1} = 0, then s(x)=u(x)s(x) = u(x); if bw−1=1b_{w-1} = 1, then s(x)<0s(x) < 0 and u(x)≥2w−1≥nu(x) \ge 2^{w-1} \ge n, so both are false.

These facts assume one width ww for both readings and two’s complement for signed numbers. Every common processor uses two’s complement, eBPF does, and the C standard requires it from C23 on. If the value is widened or narrowed between the comparison and the use, the check must be made on the value after that conversion.

Limits and common mistakes

Addition, subtraction and multiplication give the same bit pattern under both readings, which is why two’s complement is used; comparison, division, right shift and widening do not. A mixed-up reading therefore stays invisible until one of these operations runs.

Widening keeps one reading. Zero extension §4.19, which fills the new high bits with zeros, keeps the unsigned value: 11111111 becomes 0x00ff, 255. Sign extension, which copies the highest bit into the new bits, keeps the signed value: 0xffff, −1-1 signed but 65,535 unsigned. A check on the low 32 bits of a 64-bit register says nothing about the high 32; the entry on 32-bit and 64-bit eBPF arithmetic §2.11 shows how eBPF sets them.

A classic mistake in C: a length of type int passes the test if (len < 64) and is then handed to memcpy, whose length parameter has the unsigned type size_t. A length of −1-1 becomes 264−12^{64} - 1 bytes where size_t is 64 bits wide. The integer conversion §4.18 entry gives the C rules.

A checker that tracks ranges must keep both readings. The Linux eBPF verifier §3.3, the Linux kernel’s checker for eBPF programs, keeps a signed and an unsigned minimum and maximum for every register, for 64 bits and for the low 32. A bound from a signed test gives an unsigned bound only for a value known to be non-negative.

Questions answered

Why can the same bits be negative in one branch and very large in another?

The same bits can be negative in one branch and very large in another because bits carry no sign of their own: each comparison instruction chooses how to read them. A signed comparison uses two’s complement, the standard encoding of negative integers, where the highest bit counts negative; an unsigned comparison counts every bit as positive. The 64-bit pattern of all ones is therefore −1-1 to a signed comparison and 264−1=18,446,744,073,709,551,6152^{64} - 1 = 18{,}446{,}744{,}073{,}709{,}551{,}615 to an unsigned one. In eBPF, an instruction set for small programs, the jump if r1 s< 16 (signed) is taken for that value, while if r1 < 16 (unsigned) is not.

The two readings give the same value when the highest bit is 0 and different values when it is 1. For a signed index, first require a nonnegative value and then compare it with the array length. A single unsigned comparison also excludes every negative signed input when the length is at most 2w−12^{w-1} for a ww-bit comparison. With 8 bits and a length of 200, the pattern for −128-128 reads as 128 unsigned and passes 128<200128 < 200.

Related entries: Integer promotion and conversion (§4.18), ALU32 versus ALU64 (§2.11), Truncation and extension (§4.19), Overflow-safe range check (§4.17). Further reading: [1, 78, 160].