Section 15.15

Bit-vector logic

Definition

Bit-vector logic is a logic for formulas about bit-vectors: values that are sequences of a fixed number ww of bits, where ww is called the width. Its operations are those a processor performs on ww-bit words: addition, subtraction and multiplication that keep only the low ww bits of the result, that is, modulo 2w2^w; signed and unsigned comparison §2.12, division and remainder; bitwise operations §4.50; shifts; and extracting or joining ranges of bits. Formulas in this logic describe machine arithmetic exactly. SMT solvers §3.28 (satisfiability modulo theories solvers) decide them.

The idea

An analysis must prove that incrementing a counter makes it larger: after c=c+1c = c + 1, the new cc exceeds the old one. Over the integers of mathematics this is always true, so a solver that reads ++ as integer addition proves it at once. A processor, however, stores cc in a fixed number of bits, say 8. When the old cc is 255, the sum 256 needs a ninth bit; the processor drops that bit, and the new cc is 0, which is smaller. The integer proof is a proof about a different program. Bit-vector logic makes the width part of every value and every operation, so a formula means what the machine computes, and the solver finds the counterexample c=255c = 255.

A car’s odometer with six digits is a fair picture: after 999,999 km it shows 000,000. Bit-vector addition is the same wrap-around in binary. The picture stops at one point. The bits of a bit-vector can be read in two ways, as an unsigned number or as a signed number, and bit-vector logic states for each comparison, division and shift which reading it uses. An odometer has only one reading.

How it works

A bit-vector of width ww is a string of bits bw−1…b1b0b_{w-1} \ldots b_1 b_0, where b0b_0 is the lowest bit. It has two standard readings.

The bit-vector itself carries no sign. Each operation whose result depends on the reading exists in two versions.

The SMT-LIB standard (Satisfiability Modulo Theories Library), the common input language of SMT solvers, defines the operations; their SMT-LIB names are given here in code font.

Every operation has a defined result for every input. In SMT-LIB, unsigned division by zero gives the bit-vector of all ones, and the unsigned remainder by zero gives the dividend. A shift by ww or more positions gives all zero bits, except that bvashr of a value whose top bit is 1 gives all one bits. These are conventions of the logic. They differ from C, in which division by zero and a shift by the width or more are undefined behavior §4.12, and from some processors: x86 processors, for example, use only the low 5 bits of the shift count of a 32-bit shift. An encoding of a program must add whatever its language or processor requires.

Solvers usually decide bit-vector formulas by bit-blasting. They replace each ww-bit variable by ww true-or-false variables and each operation by a circuit of logic gates, such as an adder circuit for bvadd, and pass the result to a SAT solver (Boolean satisfiability solver), which handles only true-or-false variables. An adder needs a number of gates proportional to ww, and a multiplier a number proportional to w2w^2, so formulas with many wide multiplications or divisions can be slow to decide.

Worked example

Take width w=8w = 8, aa = 11001000 and bb = 01100100, which are 0xC8 and 0x64 in hexadecimal.

One bvadd gives the right bits for both readings. Here the unsigned sum wrapped and the signed sum did not.

Now ask a solver whether c+1>cc + 1 > c can fail for an 8-bit unsigned cc. In SMT-LIB the query is:

(declare-const c (_ BitVec 8))
(assert (not (bvugt (bvadd c #x01) c)))
(check-sat)
(get-model)

Here (_ BitVec 8) declares an 8-bit bit-vector, #x01 is the 8-bit constant 1 in hexadecimal, and bvugt is unsigned greater-than. The solver answers sat with cc = #xff, which is 255: 255+1=256255 + 1 = 256, 256mod256=0256 \bmod 256 = 0, and 0>2550 > 255 is false. With signed greater-than, bvsgt, the answer is cc = #x7f, which is 127: the sum is #x80, whose signed reading is −128-128, and −128>127-128 > 127 is false. Over the integers the same query is unsatisfiable, because no integer cc has c+1≤cc + 1 \le c.

The C language adds a trap of its own. With uint8_t a = 200, b = 100; the C expression a + b is not an 8-bit addition. Integer promotion §4.18 first converts both operands to int, which has at least 16 bits and usually 32, so the result is 300. Only an assignment back to a uint8_t variable keeps the low 8 bits, giving 44. An encoding that writes a + b as an 8-bit bvadd models a different program: the C test a + b > 255 is true, and the 8-bit encoding of it is false.

Stated precisely

Let w≥1w \ge 1, and let b=bw−1…b0b = b_{w-1} \ldots b_0 be a bit-vector of width ww. Its unsigned value is u(b)=∑i=0w−1bi2iu(b) = \sum_{i=0}^{w-1} b_i 2^i, and its signed value is s(b)=u(b)−bw−12ws(b) = u(b) - b_{w-1} 2^w. For an integer mm, write mmod2wm \bmod 2^w for the remainder of mm after division by 2w2^w, a number from 0 to 2w−12^w - 1. The bit-vector sum r=a+wbr = a +_w b is the bit-vector with u(r)=(u(a)+u(b))mod2w.u(r) = (u(a) + u(b)) \bmod 2^w. Its signed value s(r)s(r) is the one number from −2w−1-2^{w-1} to 2w−1−12^{w-1} - 1 that differs from s(a)+s(b)s(a) + s(b) by a multiple of 2w2^w. Integer addition has no width: x+yx + y is the exact sum.

The two additions agree in the unsigned reading, u(r)=u(a)+u(b)u(r) = u(a) + u(b), exactly when u(a)+u(b)≤2w−1u(a) + u(b) \le 2^w - 1, that is, when no carry leaves the top bit. They agree in the signed reading, s(r)=s(a)+s(b)s(r) = s(a) + s(b), exactly when s(a)+s(b)s(a) + s(b) lies from −2w−1-2^{w-1} to 2w−1−12^{w-1} - 1. Bit-vector addition keeps the commutative and associative laws of integer addition, because arithmetic modulo 2w2^w keeps them. It does not keep order: a<ba < b does not imply a+c<b+ca + c < b + c, and x+1>xx + 1 > x fails for exactly one value of xx in each reading, 2w−12^w - 1 unsigned and 2w−1−12^{w-1} - 1 signed.

A solver’s unsat answer proves that the submitted formula, under these definitions, has no satisfying values. The proof carries over to a program under three assumptions:

  1. every width in the formula equals the width of the matching value in the program;

  2. every comparison, division, right shift and extension uses the reading, signed or unsigned, that the program uses;

  3. the formula includes the rules of the source language, such as integer promotion in C and the undefined behavior of C signed overflow §4.15.

Limits and common mistakes

A proof covers one width. A property proved for 8-bit values says nothing about 32-bit values, so the width in the formula must be the width in the program.

Bit-vector logic is the arithmetic of the machine, not of the source language. In C, signed integer overflow, a shift by the width or more, and division by zero are undefined behavior, and integer promotion widens small operands before the arithmetic. A wrapping bvadd is therefore not a model of C signed addition. A correct encoding also checks that the exact sum fits: apply sign extension §4.19 to both operands to w+1w + 1 bits, add, and test that the result lies from −2w−1-2^{w-1} to 2w−1−12^{w-1} - 1. The sum of two ww-bit signed values always fits in w+1w + 1 bits, so this addition cannot wrap.

Signed and unsigned comparison of the same bits can disagree. With w=8w = 8, 0x80 is 128 unsigned and −128-128 signed, so 0x80 is greater than 0x01 as unsigned numbers and less than 0x01 as signed numbers. Choosing bvult where the program makes a signed comparison proves a property of another program.

A correct solver proves exactly the formula it receives. A wrong width, a wrong reading or a wrongly modeled shift makes the proof irrelevant to the program, however reliable the solver is.

An answer of unknown, or a time limit reached, is not a proof. Formulas with many wide multiplications and divisions are the usual cause.

Questions answered

How does an SMT bit-vector addition differ from mathematical integer addition?

An SMT (satisfiability modulo theories) bit-vector addition adds two values of the same fixed width of ww bits and keeps only the low ww bits of the sum, so its result is the sum modulo 2w2^w; mathematical integer addition has no width and always gives the exact sum. The two agree only when the exact sum fits in ww bits. With w=8w = 8, 200+100200 + 100 is 300 for the integers but 300−256=44300 - 256 = 44 for bit-vectors. As a result, laws of order that hold for the integers fail for bit-vectors: x+1>xx + 1 > x fails at x=255x = 255 in the unsigned reading and at x=127x = 127 in the signed reading.

Bit-vector addition is one operation for both readings of the bits, unsigned and two’s-complement signed; comparison, division, right shift and extension are the operations that come in separate signed and unsigned versions. Before a program is translated into a bit-vector formula, the width and signedness of every value must be fixed, together with the rules of the source language, such as integer promotion and undefined signed overflow in C, because the solver proves only the formula it receives.

Related entries: Signed versus unsigned comparison (§2.12), Signed integer overflow (§4.15), Unsigned modular arithmetic (§4.16), Integer promotion and conversion (§4.18), SMT solver (§3.28), Undefined behavior (UB) (§4.12), Bit operations: truth tables, masks, shifts, and rotations (§4.50), Truncation and extension (§4.19). Further reading: [181].