Salta al contenuto
Note per Studenti Binary Arithmetic Report

Binary Arithmetic

In questa pagina 7
In questa pagina 4

Addition

Definition (binary addition). In columns as in base 10, with the rules 0+0=00+0=0, 0+1=10+1=1, 1+1=01+1=0 carry 11, 1+1+1=11+1+1=1 carry 11.

0101 1011+ 0011 10011001 0100(91+57=148)\begin{array}{r} 0101\,1011 \\ +\ 0011\,1001 \\ \hline 1001\,0100 \end{array} \qquad (91+57=148)

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). a−b=a+(−b)a-b=a+(-b): compute the opposite of bb (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 bb and set the adder carry in to 1, so no subtractor is needed.

Example. 4 bits, 5−35-3: 0101+1101=(1) 0010=20101+1101=(1)\,0010=2, carry discarded.

8-bit example, 25−4025-40: 25=0001 100125=0001\,1001, 40=0010 1000→−40=1101 100040=0010\,1000\to-40=1101\,1000; 0001 1001+1101 1000=1111 0001=−128+64+32+16+1=−150001\,1001+1101\,1000=1111\,0001=-128+64+32+16+1=-15.

The carry out of the most significant bit is discarded: 50−2050-20 gives 0011 0010+1110 1100=1 0001 1110→0001 1110=300011\,0010+1110\,1100=1\,0001\,1110\to0001\,1110=30.

Overflow

Property (overflow, unsigned and signed). The result does not fit in nn 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: V=V= carry into the MSB ⊕\oplus carry out of the MSB.

8-bit example: 0111 0000+0101 0000=1100 00000111\,0000+0101\,0000=1100\,0000.

  • Unsigned: 112+80=192112+80=192, no carry, correct (C = 0).
  • Two's complement: 112+80112+80 gives −64-64: 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 nn-bit numbers occupies up to 2n2n bits.

Unsigned, by shifts and additions. For each multiplier bit equal to 1, add the suitably shifted multiplicand:

1011× 110110110000010110010110001000 1111(11⋅13=143)\begin{array}{r} 1011 \\ \times\ 1101 \\ \hline 1011 \\ 0000\phantom{0} \\ 1011\phantom{00} \\ 1011\phantom{000} \\ \hline 1000\,1111 \end{array} \qquad (11\cdot13=143)

The hardware keeps an accumulator A and the multiplier Q side by side: at each step, if Q0=1Q_0=1 add the multiplicand to A, then shift the pair C, A, Q right by one bit. After nn 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 (Q0,Q−1)(Q_0,Q_{-1}) with Q−1Q_{-1} initially 0:

Q0 Q−1Q_0\,Q_{-1} Action
1 0 A←A−MA\leftarrow A-M
0 1 A←A+MA\leftarrow A+M
0 0, 1 1 nothing

then arithmetic shift right of A, Q, Q−1Q_{-1}. 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 7⋅37\cdot3 on 4 bits (M=0111M=0111, Q=0011Q=0011):

Step A Q Q−1Q_{-1} Operation
start 0000 0011 0
1 1001 0011 0 10 → A−MA-M
1100 1001 1 shift
2 1110 0100 1 11 → shift only
3 0101 0100 1 01 → A+MA+M
0010 1010 0 shift
4 0001 0101 0 00 → shift only

Result A:Q=0001 0101=21A:Q=0001\,0101=21.

Division

Definition (unsigned division). As in columns: at each step try to subtract the divisor from the partial remainder; if the result is ≥0\ge0 the quotient bit is 1, otherwise it is 0 and the remainder is restored. Example 13:313:3: 1101:11→1101:11\to quotient 100=4100=4, remainder 11.

Shifts

Definition (logical, arithmetic shifts and rotations).

Operation Effect 8-bit example
logical shift left by kk multiplies by 2k2^k (if no overflow) 0000 0101≪2=0001 01000000\,0101\ll2=0001\,0100 (5 → 20)
logical shift right by kk divides an unsigned number by 2k2^k (0 enters) 1111 0000≫1=0111 10001111\,0000\gg1=0111\,1000 (240 → 120)
arithmetic shift right by kk divides a two's complement number by 2k2^k (sign bit enters), rounding toward −∞-\infty 1111 0000≫1=1111 10001111\,0000\gg1=1111\,1000 (−16 → −8)
rotation bits leaving re-enter on the other side 1000 0001→0000 00111000\,0001\to0000\,0011 (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: −16=1111 0000-16=1111\,0000 becomes 0111 1000=1200111\,1000=120.
  • Forgetting that the product needs 2n2n bits.

Versione ripasso

Addition column by column (1+1=01+1=0 carry 1; 1+1+1=11+1+1=1 carry 1): 0101 1011+0011 1001=1001 01000101\,1011+0011\,1001=1001\,0100 (91+57=14891+57=148). 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: a−b=a+b‾+1a-b=a+\overline b+1 (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. 5−35-3: 0101+1101=(1)0010=20101+1101=(1)0010=2; 25−40=1111 0001=−1525-40=1111\,0001=-15.

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

V=V= carry into MSB ⊕\oplus carry out. Different signs never overflow. Example: 0111 0000+0101 0000=1100 00000111\,0000+0101\,0000=1100\,0000: C=0 (192 correct unsigned), V=1 (−64-64 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 2n2n bits. Shift-and-add: if Q0=1Q_0=1 add multiplicand to A, shift C, A, Q right; nn steps; result in A:Q (11⋅13=1000 111111\cdot13=1000\,1111). 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 (Q0Q−1Q_0Q_{-1}, Q−1=0Q_{-1}=0 at start): 10→A−M10\to A-M; 01→A+M01\to A+M; 00,11→00,11\to nothing; then arithmetic shift right of A, Q, Q−1Q_{-1}. Runs of 1s cost one subtraction and one addition. Example M=0111M=0111, Q=0011Q=0011: A:Q=0001 0101=21A:Q=0001\,0101=21.

Division

Unsigned: try to subtract the divisor from the partial remainder; ≥0\ge0 gives quotient bit 1, otherwise 0 and restore. 13:313:3: quotient 44, remainder 11.

Shifts

  • LSL by kk: ×2k\times2^k (5→205\to20). LSR: unsigned /2k/2^k (240→120240\to120). ASR: signed /2k/2^k, sign enters, rounds toward −∞-\infty (−16→−8-16\to-8). Rotation: bits re-enter.

Common mistakes: carry out is not signed overflow; LSR on a negative gives 120120 instead of −8-8; the product needs 2n2n bits.

Teoria collegata