Binary Arithmetic
In questa pagina 7
In questa pagina 4
Addition
Definition (binary addition). In columns as in base 10, with the rules , , carry , carry .
The circuit that does it is the ripple-carry adder (AddersHalf adder and full adder, generate-propagate-delete decomposition, the ripple-carry adder and its linear worst-case delay, the adder-subtractor, and the fast structures carry-lookahead, carry-bypass, carry-select and square-root carry-select with their delay formulas and optimal block sizes.Adders →).
Subtraction in two's complement
Formula (subtraction). : compute the opposite of (invert the bits, add 1; see Signed Integer RepresentationsSigned integers on n bits: sign-magnitude, one's complement, two's complement and excess K, with representable ranges, computing the opposite, sign extension and a 4-bit comparison table.Signed Integer Representations →) and add. In hardware: invert the bits of and set the adder carry in to 1, so no subtractor is needed.
Example. 4 bits, : , carry discarded.
8-bit example, : , ; .
The carry out of the most significant bit is discarded: gives .
Overflow
Property (overflow, unsigned and signed). The result does not fit in bits. The rule depends on how the bits are interpreted:
Interpretation Overflow when Flag unsigned a carry leaves the MSB in addition (in subtraction: no carry out, i.e. a borrow occurred) C (carry) two's complement two operands of the same sign give a result of opposite sign V (overflow) Adding numbers of different signs never overflows in two's complement. Equivalent: carry into the MSB carry out of the MSB.
8-bit example: .
- Unsigned: , no carry, correct (C = 0).
- Two's complement: gives : two positives, negative result, so overflow (V = 1).
Processors always compute both flags; the program chooses which to look at (in ARM different conditions for signed and unsigned comparisons). The four typical flags are N (negative, i.e. the MSB), Z (zero), C, V (Arithmetic Logic UnitRole of the ALU, its inputs and the N Z C V flags, the 1-bit ALU built from AND, OR, a full adder and a multiplexer, the N-bit ALU with subtraction, NOR, set-less-than, zero and overflow detection, comparators, bus sharing with tri-state outputs, and the other computation units.Arithmetic Logic Unit →).
Multiplication
The product of two -bit numbers occupies up to bits.
Unsigned, by shifts and additions. For each multiplier bit equal to 1, add the suitably shifted multiplicand:
The hardware keeps an accumulator A and the multiplier Q side by side: at each step, if add the multiplicand to A, then shift the pair C, A, Q right by one bit. After steps the product is in A:Q. Combinational and array versions are in Shift Registers, Shifters and MultipliersShift registers (serial-parallel conversion, hold constraint), combinational shifters and the barrel shifter with log2 N multiplexer levels, and multipliers: partial products, array multiplier with N squared AND gates and N(N-1) full adders, Wallace tree, Booth, sequential shift-and-add, and dedicated FPGA DSP blocks.Shift Registers, Shifters and Multipliers →.
Property (Booth's algorithm). Examine the pairs with initially 0:
Action 1 0 0 1 0 0, 1 1 nothing then arithmetic shift right of A, Q, . A run of consecutive 1s in the multiplier costs one subtraction at the start and one addition at the end instead of one addition per bit. It works directly on two's complement numbers.
Example on 4 bits (, ):
| Step | A | Q | Operation | |
|---|---|---|---|---|
| start | 0000 | 0011 | 0 | |
| 1 | 1001 | 0011 | 0 | 10 → |
| 1100 | 1001 | 1 | shift | |
| 2 | 1110 | 0100 | 1 | 11 → shift only |
| 3 | 0101 | 0100 | 1 | 01 → |
| 0010 | 1010 | 0 | shift | |
| 4 | 0001 | 0101 | 0 | 00 → shift only |
Result .
Division
Definition (unsigned division). As in columns: at each step try to subtract the divisor from the partial remainder; if the result is the quotient bit is 1, otherwise it is 0 and the remainder is restored. Example : quotient , remainder .
Shifts
Definition (logical, arithmetic shifts and rotations).
Operation Effect 8-bit example logical shift left by multiplies by (if no overflow) (5 → 20) logical shift right by divides an unsigned number by (0 enters) (240 → 120) arithmetic shift right by divides a two's complement number by (sign bit enters), rounding toward (−16 → −8) rotation bits leaving re-enter on the other side (left)
ARM has shifts as a modifier of the second operand of almost every instruction (LSL, LSR, ASR, ROR).
Common mistakes
- Treating the carry out of a two's complement addition as overflow: only the sign change counts.
- Using logical right shift to divide a negative number: becomes .
- Forgetting that the product needs bits.
Versione ripasso
Addition column by column ( carry 1; carry 1): (). Circuit: AddersHalf adder and full adder, generate-propagate-delete decomposition, the ripple-carry adder and its linear worst-case delay, the adder-subtractor, and the fast structures carry-lookahead, carry-bypass, carry-select and square-root carry-select with their delay formulas and optimal block sizes.Adders →.
Subtraction: (Signed Integer RepresentationsSigned integers on n bits: sign-magnitude, one's complement, two's complement and excess K, with representable ranges, computing the opposite, sign extension and a 4-bit comparison table.Signed Integer Representations →); carry out discarded. : ; .
Overflow
| Interpretation | Overflow when | Flag |
|---|---|---|
| unsigned | carry out of the MSB (subtraction: no carry out, i.e. a borrow occurred) | C |
| two's complement | same-sign operands, opposite-sign result | V |
carry into MSB carry out. Different signs never overflow. Example: : C=0 (192 correct unsigned), V=1 ( signed). Flags N, Z, C, V (Arithmetic Logic UnitRole of the ALU, its inputs and the N Z C V flags, the 1-bit ALU built from AND, OR, a full adder and a multiplexer, the N-bit ALU with subtraction, NOR, set-less-than, zero and overflow detection, comparators, bus sharing with tri-state outputs, and the other computation units.Arithmetic Logic Unit →).
Multiplication
Product has bits. Shift-and-add: if add multiplicand to A, shift C, A, Q right; steps; result in A:Q (). Hardware: Shift Registers, Shifters and MultipliersShift registers (serial-parallel conversion, hold constraint), combinational shifters and the barrel shifter with log2 N multiplexer levels, and multipliers: partial products, array multiplier with N squared AND gates and N(N-1) full adders, Wallace tree, Booth, sequential shift-and-add, and dedicated FPGA DSP blocks.Shift Registers, Shifters and Multipliers →. Booth (, at start): ; ; nothing; then arithmetic shift right of A, Q, . Runs of 1s cost one subtraction and one addition. Example , : .
Division
Unsigned: try to subtract the divisor from the partial remainder; gives quotient bit 1, otherwise 0 and restore. : quotient , remainder .
Shifts
- LSL by : (). LSR: unsigned (). ASR: signed , sign enters, rounds toward (). Rotation: bits re-enter.
Common mistakes: carry out is not signed overflow; LSR on a negative gives instead of ; the product needs bits.