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 bits as a whole number from 0 to . A signed comparison reads it in two’s complement, the standard encoding of negative integers, in which the highest bit counts as , so values run from to . The two agree on which patterns are equal but not on their order: the 8-bit pattern 11111111 is 255 unsigned and 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 , and , 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 , 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 (). 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 -bit pattern from 0, the lowest, to , the highest. Unsigned, bit is worth . Two’s complement keeps these worths except for the highest bit, which is worth instead of . For the bits are worth 2 and 1 unsigned, and and 1 signed:
| pattern | unsigned | signed |
|---|---|---|
00 |
0 | 0 |
01 |
1 | 1 |
10 |
2 | |
11 |
3 |
A pattern whose highest bit is 1 is large unsigned and negative signed. Changing the worth of that bit from to lowers the value by , here 4: unsigned 3 becomes .
The bits do not say which reading applies; the operation does.
In eBPF §2.1 (extended Berkeley Packet Filter), an instruction set for small programs that run inside a host program, the jumps
JGT,JGE,JLTandJLE(greater, greater or equal, less, less or equal) compare unsigned;JSGT,JSGE,JSLTandJSLEcompare signed;JEQandJNE(equal, not equal) need no choice. ClassJMPcompares 64 bits, classJMP32the low 32.In C the operand types decide. C converts an
intcompared with anunsigned inttounsigned intfirst, so whereunsigned intis 32 bits wide, becomes and-1 < 16uis false.A processor compares by subtracting and recording condition flags §4.42. An unsigned less-than branch reads the carry flag; a signed one reads the sign and overflow flags.
A bounds check must use the reading of the later access, or exclude both ends. For an index and an array of elements, either test and as signed numbers, or make one unsigned test . 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 , which is not less than when .
Worked example
Take . Unsigned, the bits are worth 128, 64, 32, 16, 8, 4, 2 and 1; signed, the highest is worth . With , the pattern 11111111 is unsigned and signed. The pattern 10000000 is 128 unsigned and signed, and 00000101 is both ways. Against an array of 16 elements, the signed test alone accepts all three patterns. The signed pair and accepts only the pattern of 5. The unsigned test 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 unsigned and signed. An eBPF program guards an access with the signed test if r1 s> 15 goto reject. Since is false, the program goes on and adds R1 to the address of a 16-byte buffer. Adding modulo subtracts 1, so the access reads the byte just before the buffer. The unsigned test if r1 > 15 goto reject jumps, since . 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 be a pattern of bits , where is the highest bit. Its unsigned value and its two’s-complement signed value are with and . The unsigned comparison orders patterns by , the signed comparison by . For patterns and of the same width:
exactly when , which happens exactly when and are the same pattern.
If and have the same highest bit, exactly when , since 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.
For a bound with , holds exactly when holds: if , then ; if , then and , so both are false.
These facts assume one width 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, 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 becomes 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 to a signed comparison and 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 for a -bit comparison. With 8 bits and a length of 200, the pattern for reads as 128 unsigned and passes .
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].