Definition
Bit-vector logic is a logic for formulas about bit-vectors: values that are sequences of a fixed number of bits, where is called the width. Its operations are those a processor performs on -bit words: addition, subtraction and multiplication that keep only the low bits of the result, that is, modulo ; 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 , the new 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 in a fixed number of bits, say 8. When the old is 255, the sum 256 needs a ninth bit; the processor drops that bit, and the new 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 .
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 is a string of bits , where is the lowest bit. It has two standard readings.
Unsigned: the value , a number from 0 to .
Signed, in two’s complement, the encoding that almost every processor uses: the same sum, except that the top bit counts as instead of . The value is a number from to .
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.
The same for both readings. Addition
bvadd, subtractionbvsub, multiplicationbvmul, negationbvnegand left shiftbvshlkeep the low bits of the exact result. The signed and the unsigned value of the same bits differ by 0 or by , and a difference that is a multiple of changes only bits at position and above, which the operation discards. So one rule serves both readings. The bitwise operationsbvand,bvor,bvxorandbvnotalso ignore the reading.Different for the two readings. Comparison:
bvultis unsigned less-than andbvsltis signed less-than, with matching forms for the other comparisons. Division and remainder:bvudivandbvuremfor unsigned values,bvsdivandbvsremfor signed ones. Right shift:bvlshrfills the vacated top bits with zeros, a logical shift, andbvashrfills them with copies of the top bit, an arithmetic shift. Widening:zero_extendadds zero bits on top, andsign_extendadds copies of the top bit.Width changes.
extracttakes a range of bits out of a bit-vector, andconcatjoins two bit-vectors into a longer one. The operands of arithmetic and comparison must have equal widths, so every change of width is written explicitly.
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 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 -bit variable by 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 , and a multiplier a number proportional to , so formulas with many wide multiplications or divisions can be slow to decide.
Worked example
Take width , = 11001000 and = 01100100, which are 0xC8 and 0x64 in hexadecimal.
Unsigned: and . The integer sum is 300, which needs 9 bits: , binary
100101100.bvaddkeeps the low 8 bits,00101100, which is , or0x2C. In modular form, .Signed: the top bit of is 1, so , and . The integer sum is , which fits in the signed range from to 127. The result bits
00101100read as signed are 44.
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 can fail for an 8-bit unsigned . 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 = #xff, which is 255: , , and is false. With signed greater-than, bvsgt, the answer is = #x7f, which is 127: the sum is #x80, whose signed reading is , and is false. Over the integers the same query is unsatisfiable, because no integer has .
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 , and let be a bit-vector of width . Its unsigned value is , and its signed value is . For an integer , write for the remainder of after division by , a number from 0 to . The bit-vector sum is the bit-vector with Its signed value is the one number from to that differs from by a multiple of . Integer addition has no width: is the exact sum.
The two additions agree in the unsigned reading, , exactly when , that is, when no carry leaves the top bit. They agree in the signed reading, , exactly when lies from to . Bit-vector addition keeps the commutative and associative laws of integer addition, because arithmetic modulo keeps them. It does not keep order: does not imply , and fails for exactly one value of in each reading, unsigned and 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:
every width in the formula equals the width of the matching value in the program;
every comparison, division, right shift and extension uses the reading, signed or unsigned, that the program uses;
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 bits, add, and test that the result lies from to . The sum of two -bit signed values always fits in bits, so this addition cannot wrap.
Signed and unsigned comparison of the same bits can disagree. With , 0x80 is 128 unsigned and 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 bits and keeps only the low bits of the sum, so its result is the sum modulo ; mathematical integer addition has no width and always gives the exact sum. The two agree only when the exact sum fits in bits. With , is 300 for the integers but for bit-vectors. As a result, laws of order that hold for the integers fail for bit-vectors: fails at in the unsigned reading and at 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].