Lecture 5 | Boolean Arithmetic

Representing and Adding Binary Numbers

How can a fixed collection of bits represent positive and negative integers?

Boolean Arithmetic

From Gate Logic to Arithmetic

Last lectureGate logiccomponents and specifications
TodayBinary arithmeticrepresentation and addition
Next lectureAdders and ALUhardware that performs operations

Before building arithmetic hardware, we must agree on what each bit pattern means.

Binary Numbers

Positional Notation: Decimal and Binary

Decimal: powers of 10

(5109)10
5109
103102101100

5 × 103 + 1 × 102 + 0 × 101 + 9 × 100 = 5109

Binary: powers of 2

(10101)2
10101
2423222120

1 × 24 + 0 × 23 + 1 × 22 + 0 × 21 + 1 × 20 = 21

The subscript identifies the base. The position of each digit determines the power used as its weight.

Binary Numbers

Bit Patterns and Their Interpretation

Integer0001010121
Character01000001the code for A
Instruction11101100...an operation and its operands
Image sample11001010one encoded intensity

The same bits can mean different things. Their interpretation depends on how the computer uses them.

Binary Numbers

Word Size, Registers, and Capacity

A register is a small, fixed-width storage location inside a processor. It holds one bit pattern while the processor works with it.

Word size is the number of bits used for such a basic chunk of information. Common word sizes are 8, 16, 32, and 64 bits.

8-bit16-bit32-bit64-bit
Example: an 8-bit register
2 choices×2 choices××2 choices
Distinct patterns28 = 256

As unsigned integers: 0 through 255.

In general, an n-bit register has 2n patterns and can represent unsigned values from 0 through 2n−1.

Binary Numbers

Most and Least Significant Bits

Most Significant Bit (MSB)largest place value
Least Significant Bit (LSB)smallest place value: 20
10101
MSB

The leftmost bit in the written representation.

LSB

The rightmost bit; it determines whether an unsigned number is even or odd.

Next: Add binary numbers one column at a time.

Binary Addition

One-Bit Addition Rules

0 + 0sum 0, carry 0
0 + 1sum 1, carry 0
1 + 0sum 1, carry 0
1 + 1sum 0, carry 1because (10)2 = 2

Begin at the LSB, write the sum bit, and carry any extra 1 into the next column.

Example: Four-Bit Binary Addition

1 1   0 1 1 0+ 0 0 1 1 1 0 0 1
  1. 0+1=1.
  2. 1+1=(10)2: write 0, carry 1.
  3. 1+0+carry 1=(10)2: write 0, carry 1.
  4. 0+0+carry 1=1.

(0110)2=6 and (0011)2=3; therefore (1001)2=9.

Binary Addition

Fixed-Width Unsigned Overflow

1 1 1 1 1 0 1 1+ 0 1 1 11 0 0 1 0

The mathematical result is (10010)2=18.

A 4-bit register has room only for 0010. The leftmost carry lies outside the register.

For now: call this unsigned overflow.

Hardware always has a fixed width; it cannot keep adding new bit positions forever.

Exercise 1

Four-Bit Binary Addition

A

0101 + 0011

Give the 4-bit result.
B

1010 + 0101

Give the 4-bit result.
C

1110 + 0111

Give the 4-bit stored result and the carry out.

A: 1000

B: 1111

C: 0101, carry out 1

Next: Use the same bit patterns to represent negative numbers.

Signed Binary Numbers

Representing Negative Integers

Available

An n-bit register has exactly 2n patterns.

Decision

Use exactly half for non-negative values and half for negative values.

Design goal

Keep addition hardware the same for positive and negative operands.

The representation used by almost all modern computers is called two's complement.

Signed Binary Numbers

Four-Bit Two's Complement

PatternValue
00000
00011
00102
......
01117
MSB = 0non-negativeMSB = 1negative
PatternValue
1000−8
1001−7
......
1110−2
1111−1

The MSB acts as a sign indicator, but two's complement is not merely “attach a sign bit.”

Signed Binary Numbers

Range of n-Bit Signed Integers

Negative integers2n−1−2n−1 through −1
Zero1the all-zero pattern
Positive integers2n−1 − 11 through 2n−1−1
Complete signed range−2n−1 to 2n−1−1

For 4 bits: 8 negative values, 7 positive values, and zero.

Exercise 2

Which Integers Fit in Four-Bit Two's Complement?

Write the four-bit representation of each integer. If it cannot be represented, say why.

A+6
B−1
C−5
D−8
E+8

A: 0110

B: 1111

C: 1011

D: 1000

E: impossible; the signed range ends at +7

Signed Binary Numbers

Converting Two's Complement to Decimal

First step: inspect the Most Significant Bit (MSB), then use the corresponding conversion below.

MSB = 0Convert normally0101

0 × 23 + 1 × 22 + 0 × 21 + 1 × 20 = 5

MSB = 1Interpret as negative1011

As unsigned, 1011 is 11. For four bits, subtract 24:

11 − 16 = −5

General rule: if an n-bit pattern has MSB 1 and unsigned value U, its two's-complement value is U − 2n.

The equivalent method for a negative pattern
1011flip bits0100add 10101 = 5value = −5
Exercise 3

Convert Two's Complement to Decimal

Interpret each pattern as a four-bit two's-complement integer. For C, verify the answer using both conversion methods.

A0110
B1111
C1010
D1000

A: 6

B: −1

C: 10−16=−6; flip and add 1 gives 0110, so −6

D: −8

Signed Binary Numbers

Negation in Two's Complement

Start with +30011
Flip all bits1100
Add 11101
Result−3

0011 + 1101 = 1 0000. Discard the carry outside four bits, leaving 0000.

The same procedure converts a negative number back to its positive counterpart.

Exercise 4

Negation Practice and Why the Method Works

For each four-bit pattern, flip every bit and add 1. State the original and resulting decimal values.

A0101
B1110
C1001
D1000

Proof question: For an n-bit value x, prove that flipping every bit and adding 1 represents −x.

A: +5 → 1011 = −5

B: −2 → 0010 = +2

C: −7 → 0111 = +7

D: −8 → 1000; +8 is outside the four-bit signed range

Flipping all n bits changes x to (2n−1)−x. Adding 1 gives 2n−x.

Keeping only n bits is arithmetic modulo 2n. Thus 2n−x ≡ −x, so the result is the n-bit representation of the negative.

Signed Binary Numbers

Addition in Two's Complement

−21110
−31101
+
stored result1011−5

The full sum is 1 1011. Discard the carry outside the 4-bit word; 1011 represents −5.

No separate “negative-number adder” is required.

Exercise 5

Add Signed Numbers and Verify the Result

Represent each operand in four bits, add the patterns, discard any carry out, and convert the stored result back to decimal.

A−2 + (−3)
B+5 + (−3)
C−4 + 6
D−5 + (−4)

A: 1110+1101=1011 = −5

B: 0101+1101=0010 = 2

C: 1100+0110=0010 = 2

D: stored 0111 = 7, but −9 is outside the range: signed overflow

Signed Binary Numbers

Subtraction in Two's Complement

To compute a − b, negate b using two's complement and add: a − b = a + (−b).

Problem5 − 3
Represent operands0101 − 0011
Negate 30011 → 1101
Add instead0101 + 1101 = 00102

The carry outside the four-bit word is discarded, exactly as in addition.

Exercise 6

Subtraction Practice

Use four-bit two's complement. Rewrite each subtraction as addition of a negated number, compute the stored result, and verify the decimal answer.

A5 − 3
B2 − 6
C−3 − 2
D−6 − (−5)

A: 0101+1101=0010 = 2

B: 0010+1010=1100 = −4

C: 1101+1110=1011 = −5

D: 1010+0101=1111 = −1

Summary of This Lecture

Binary Representation, Addition, and Subtraction

Representation

n bits provide 2n distinct patterns.

Addition

Add from the LSB and propagate carry toward the MSB.

Signed values

Two's complement gives the range −2n−1 through 2n−1−1.

Subtraction

Negate the second operand, then add.

Why two's complement?
  1. The same adder handles signed and unsigned bit patterns.
  2. There is only one representation of zero.
  3. Subtraction can be performed by adding the negated operand.
Next Lecture | Boolean Arithmetic

Lecture 6: Adders and the Hack ALU

How can gates implement the binary addition procedure developed in this lecture?