COA Notes: Number Systems and Logic Gates

Nov 19, 2026·Pranesh Mahanta·21CSS201T — Computer Organization and Architecture

Positional number systems and every conversion between them, the classical binary codes, signed arithmetic in sign-magnitude, 1’s and 2’s complement, and the logic gates that turn all those bits into hardware.

Contents
  1. Number systems and positional notation
  2. Conversions between bases
  3. Codes: BCD and Excess-3
  4. Gray code
  5. ASCII and parity
  6. Binary addition and subtraction
  7. Sign-magnitude representation
  8. 1’s complement arithmetic
  9. 2’s complement arithmetic
  10. Binary multiplication and division
  11. BCD arithmetic
  12. Basic logic gates: AND, OR, NOT
  13. NAND, NOR, EX-OR and EX-NOR
  14. Universal gates

1Number systems and positional notation

Digital computers store and manipulate everything as strings of two symbols, 0 and 1, but humans write quantities in several bases. A positional number system assigns a value to each digit according to its position: a digit d in position i contributes d × bi, where b is the base or radix. The four bases used throughout this course are binary (b = 2, the machine’s alphabet), octal (b = 8) and hexadecimal (b = 16, compact shorthands for binary), and decimal (b = 10, the human alphabet).

(dn dn−1 … d1 d0 . d−1 d−2 …)b = Σ di × bi,   with weights … b2, b1, b0, b−1, b−2 …
The radix point separates the b0 weight from the b−1 weight (called a binary point in base 2).
BaseDigit setWeights (integer part)Example
Binary (2)0, 1 (bits)8, 4, 2, 1(1011)2 = 8 + 2 + 1 = 11
Octal (8)0–7512, 64, 8, 1(234)8 = 128 + 24 + 4 = 156
Decimal (10)0–91000, 100, 10, 1(156)10
Hexadecimal (16)0–9, A–F256, 16, 1(9C)16 = 9 × 16 + 12 = 156
Hexadecimal needs sixteen digits, so the values ten to fifteen are written A, B, C, D, E, F. A single hex digit stands for exactly four bits and an octal digit for exactly three bits — that is the whole reason these two bases are used as abbreviations for long bit patterns.

2Conversions between bases

Two methods cover almost every conversion asked in the examination. To convert an integer from decimal to base b, divide repeatedly by b and read the remainders upward (the first remainder is the least significant digit). To convert a fraction, multiply repeatedly by b and read the integer parts downward. Conversions between binary and bases 2k (octal, hexadecimal) need no arithmetic at all: regroup the bits in chunks of k starting from the radix point, padding with zeros on both ends as needed.

Integer: divide by b, collect remainders → read upward (MSD last).
Fraction: multiply by b, collect integer parts → read downward (MSD first).
Grouping: 3 bits per octal digit, 4 bits per hex digit, from the radix point outward.
Worked problem · decimal to binary, octal, hexadecimal
Convert (156.25)10 into (a) binary, (b) octal and (c) hexadecimal. Verify the three results against each other.
(a) Integer part 156 by repeated division by 2: 156/2 = 78 r0; 78/2 = 39 r0; 39/2 = 19 r1; 19/2 = 9 r1; 9/2 = 4 r1; 4/2 = 2 r0; 2/2 = 1 r0; 1/2 = 0 r1. Reading remainders upward: 156 = (10011100)2.
Fraction 0.25 by repeated multiplication by 2: 0.25 × 2 = 0.5 → 0; 0.5 × 2 = 1.0 → 1, and the process terminates. So 0.25 = (0.01)2.
Result: (156.25)10 = (10011100.01)2.
(b) Octal by 3-bit grouping: 010 011 100 . 010 → (234.2)8.
(c) Hexadecimal by 4-bit grouping: 1001 1100 . 0100 → 9 C . 4 = (9C.4)16.
Verification: (234.2)8 = 2 × 64 + 3 × 8 + 4 + 2/8 = 128 + 24 + 4 + 0.25 = 156.25 and (9C.4)16 = 9 × 16 + 12 + 4/16 = 156.25. All three agree.
A decimal fraction terminates in binary only when it is a sum of negative powers of two. 0.110 has no finite binary expansion — its bits repeat 0011 forever — which is why decimal fractions are usually converted to a stated number of places.

3Codes: BCD and Excess-3

The binary-coded decimal (BCD, or 8421) code stores each decimal digit as its own 4-bit group, so 549 becomes 0101 0100 1001 — not the pure binary of 549. Only the groups 0000 through 1001 are legal; 1010 to 1111 are unused, which makes BCD wasteful (about 20% more bits than binary) but convenient wherever decimal input and output dominate, as in calculators and meters. The Excess-3 (XS-3) code is BCD shifted: each digit is represented by its BCD pattern plus 0011 (three). It is self-complementing: complementing all bits of the code for digit d yields the code for 9 − d, which makes subtraction by complement addition easy.

DigitBCD (8421)Excess-3DigitBCD (8421)Excess-3
000000011501011000
100010100601101001
200100101701111010
300110110810001011
401000111910011100
Worked problem · encoding in BCD and Excess-3
Write the decimal number 549 in (a) BCD (8421) code and (b) Excess-3 code. (c) Verify that XS-3 is self-complementing for the digit 4.
(a) BCD: encode each digit separately: 5 → 0101, 4 → 0100, 9 → 1001. So 549 = 0101 0100 1001 (BCD).
(b) Excess-3: add 3 to each digit, then encode: 5+3 = 8 → 1000, 4+3 = 7 → 0111, 9+3 = 12 → 1100. So 549 = 1000 0111 1100 (XS-3).
(c) Self-complement check: 4 is coded 0111 (since 4 + 3 = 7). Complementing every bit gives 1000 = 8, which is the XS-3 code of 5 = 9 − 4. The complement of the code therefore represents the 9’s complement of the digit, as required.
Do not confuse BCD with binary: (549)10 in pure binary is 1000100101, a 10-bit pattern, whereas BCD spends twelve bits. The two agree only for numbers 0–9.

4Gray code

The Gray code (reflected binary code) is a binary ordering in which successive values differ in exactly one bit. This property matters in real hardware: a mechanical shaft encoder reading Gray code can never misread a value near a boundary, because only one sensor bit changes at a time. Gray code is also used to order the rows and columns of Karnaugh maps, where single-bit adjacency makes loop-grouping of minterms possible.

Binary → Gray: gn−1 = bn−1,   gi = bi+1 ⊕ bi   (exclusive-OR each bit with the one to its left).
Gray → Binary: bn−1 = gn−1,   bi = bi+1 ⊕ gi   (accumulate the XORs from the left).
Worked problem · Gray code conversions
(a) Convert the binary number 1011 to Gray code. (b) Convert the Gray code word 1101 back to binary. Check the pair of results for consistency.
(a) Binary 1011 → Gray: g3 = b3 = 1; g2 = b3 ⊕ b2 = 1 ⊕ 0 = 1; g1 = b2 ⊕ b1 = 0 ⊕ 1 = 1; g0 = b1 ⊕ b0 = 1 ⊕ 1 = 0. Result: 1110 (Gray).
(b) Gray 1101 → binary: b3 = g3 = 1; b2 = b3 ⊕ g2 = 1 ⊕ 1 = 0; b1 = b2 ⊕ g1 = 0 ⊕ 0 = 0; b0 = b1 ⊕ g0 = 0 ⊕ 1 = 1. Result: 1001 (binary).
Check: convert 1001 forward again: g3 = 1, g2 = 1 ⊕ 0 = 1, g1 = 0 ⊕ 0 = 0, g0 = 0 ⊕ 1 = 1 → 1101, which is exactly the given Gray word. Consistent.
In binary → Gray you XOR neighbours of the binary word; in Gray → binary you XOR the running binary result with each Gray bit. Mixing up the two reference words is the most common examination mistake.

5ASCII and parity

ASCII represents text with 7-bit codes, giving 128 characters: digits, letters, punctuation and control codes. Useful anchors: ‘0’ = (48)10 = 0110000, ‘A’ = 65 = 1000001 and ‘a’ = 97 = 1100001; every digit or letter follows in sequence, so a machine converts a character digit to its numeric value simply by subtracting 0110000.

A parity bit is an extra bit appended to a word for simple error detection. For even parity the bit is chosen so that the total number of 1s in the word (including the parity bit) is even; for odd parity, odd. It is the XOR of all data bits.

even parity bit = bn−1 ⊕ … ⊕ b1 ⊕ b0,   so the whole word always carries an even count of 1s.
Worked problem · ASCII word with even parity
The letter ‘C’ is transmitted as ASCII with one even-parity bit prepended. Write the complete 8-bit transmitted word and give it in hexadecimal.
ASCII code of ‘C’: 67 = 64 + 2 + 1 = 1000011 (7 bits).
Count the 1s: 1000011 contains three 1s — an odd count.
Even parity bit: must make the total even, so parity = 1.
Transmitted word: parity first, then the character: 11000011, which is (C3)16.
At the receiver: the XOR of all eight received bits is 0 for a correct word; if any single bit flips in transit the XOR becomes 1 and the error is flagged.
Parity detects any odd number of flipped bits but cannot detect an even number of flips, and it cannot say which bit was wrong. It locates nothing — correcting needs codes such as Hamming code, and even there parity is the underlying ingredient.

6Binary addition and subtraction

Unsigned binary addition follows the same column method as decimal, with a very small carry table: 0 + 0 = 0, 0 + 1 = 1, 1 + 1 = 0 with carry 1, and 1 + 1 + 1 = 1 with carry 1. Subtraction borrows instead: 0 − 1 forces a borrow from the next higher column, turning that bit into 10 − 1 = 1. These are the only rules the hardware needs — a full adder per column implements the addition row of the table.

Addition example (with carries):   1011 + 0110 = 10001   (11 + 6 = 17).
Subtraction example (with borrows):   10101 − 01011 = 01010   (21 − 11 = 10).

Every signed-arithmetic method in the next three sections is really unsigned addition in disguise: the operand is encoded so that adding the encodings produces the correct encoded result, and only the interpretation of the leading bit changes. That is why the addition circuit above is reused everywhere.

When subtracting by hand, a final borrow out of the most significant column means the minuend was smaller than the subtrahend — the true result is negative and must be recomputed as B − A (or, in hardware, produced through complement arithmetic).

7Sign-magnitude representation

In sign-magnitude the leftmost bit is a pure sign (0 for positive, 1 for negative) and the remaining n − 1 bits hold the magnitude. Thus +13 in eight bits is 00001101 and −13 is 10001101. The notation matches human intuition and the sign of the result is easy to inspect, but arithmetic is awkward: to add two numbers of opposite sign the hardware must compare magnitudes, subtract the smaller from the larger, and attach the sign of the larger — a separate subtract circuit alongside the adder.

Range of n-bit sign-magnitude: ±(2n−1 − 1),   i.e. ±127 for n = 8.
Two zeros exist: +0 = 0000 0000 and −0 = 1000 0000.
The doubled zero and the add/subtract-then-choose-sign logic are the two standard criticisms of sign-magnitude. It survives in the mantissa of floating-point formats, where a separate sign bit is natural, but integer arithmetic moved to 2’s complement long ago.

81’s complement arithmetic

In 1’s complement a negative number is formed by complementing every bit of the positive value: −13 = 11110010. Addition then works with an ordinary adder plus one rule: if the most significant addition produces an end carry, the result is positive and the carry is added back into the least significant bit; if there is no end carry, the result is negative and its magnitude is read by complementing it.

Negation: X̄ → every bit flipped.
End carry present: result positive; add carry back (end-around carry).
End carry absent: result negative; magnitude = complement of the sum.
Worked problem · subtraction with 1’s complement
Using 8-bit 1’s complement arithmetic, compute 9 − 25. Show the encodings, the addition and how the sign of the result is determined.
Encode: 9 = 00001001. To subtract 25, add the 1’s complement of 25: 25 = 00011001, so −25 = 11100110.
Add: 00001001 + 11100110 = 11101111. There is no carry out of the most significant bit.
Interpret: no end carry → result is negative. Its magnitude is the complement of 11101111, which is 00010000 = 16.
Answer: 9 − 25 = −16, represented as 11101111 in 1’s complement. Correct, since 9 − 25 = −16.
1’s complement still has two zeros (00000000 and 11111111) and the end-around carry creates a second pass through the adder, which slows hardware. These defects are exactly what 2’s complement removes.

92’s complement arithmetic

2’s complement is the standard of every modern CPU. A negative value is encoded as 2n − |value|, formed by complementing all bits and adding 1; the leading bit still acts as a sign, but the encoding is a true weighted sum in which the sign bit carries weight −2n−1. Addition, subtraction and multiplication all reduce to ordinary unsigned binary operations, with one check: overflow occurs only when the carry into the sign bit differs from the carry out of it, i.e. when the true result falls outside the representable range.

Negation: 2’s complement = complement every bit, then add 1.
Value: −bn−1 · 2n−1 + Σi=0n−2 bi · 2i.
Range (n = 8): −128 … +127; a single zero (0000 0000).
Overflow: carry-in to MSB ≠ carry-out of MSB.
Worked problem · adding two negative numbers
Using 8-bit 2’s complement, compute (−13) + (−25). Give the encodings, the binary sum and the decimal check, and state whether overflow occurs.
Encode −13: 13 = 00001101 → complement 11110010 → add 1 → 11110011.
Encode −25: 25 = 00011001 → complement 11100110 → add 1 → 11100111.
Add: 11110011 + 11100111 = 1 11011010; the carry out of the MSB is discarded (modular arithmetic), leaving 11011010.
Decode: MSB = 1 → negative. Magnitude = complement 11011010 → 00100101 → +1 → 00100110 = 38. Result = −38.
Check: −13 + (−25) = −38. Correct.
Overflow? Two negatives produced a negative sum — the sign is consistent and no overflow occurred.
Worked problem · subtraction via 2’s complement
Using 8-bit 2’s complement, compute 42 − 58 by adding the 2’s complement of 58.
Encode 42: 42 = 32 + 8 + 2 = 00101010.
Encode −58: 58 = 00111010 → complement 11000101 → add 1 → 11000110.
Add: 00101010 + 11000110 = 11110000 (no carry out of the MSB).
Decode: MSB = 1 → negative. Magnitude: complement 11110000 → 00001111 → +1 → 00010000 = 16.
Answer: 42 − 58 = −16. Correct.
The pattern 1000 0000 encodes −128 and has no positive counterpart: negating it overflows back to itself. A quick sanity example of overflow: 100 + 50 = 150 > 127, and indeed the 8-bit sum 10010110 decodes to −106 — the sign flip is the tell-tale of overflow.

10Binary multiplication and division

Binary multiplication uses the shift-and-add algorithm, identical to long multiplication: for each 1 in the multiplier, add the multiplicand shifted left by that bit’s position. Division mirrors it as shift-and-subtract: starting from the most significant end, if the shifted divisor fits into the running remainder, the quotient bit is 1 and the divisor is subtracted; otherwise the quotient bit is 0. Signed versions sign-extend the operands and either correct the product’s sign or pre-complement both operands so the magnitude arithmetic is unsigned.

Worked problem · multiplication by shift-and-add
Multiply the unsigned 4-bit numbers 1101 and 1011 using the shift-and-add method, and verify the result in decimal.
Setup: multiplicand 1101 (13), multiplier 1011 (11). Scan the multiplier from the LSB.
Partial products: bit 0 = 1 → 1101 shifted 0 places; bit 1 = 1 → 1101 shifted 1 place; bit 2 = 0 → 0000 shifted 2 places; bit 3 = 1 → 1101 shifted 3 places.
Multiplier bitPartial productShiftDecimal value
1 (LSB)11010 places13
111011 place26
000002 places0
1 (MSB)11013 places104
Add the partial products: 1101 + 11010 = 100111 (39); 100111 + 000000 = 100111; 100111 + 1101000 = 10001111.
Verify: 10001111 = 128 + 8 + 4 + 2 + 1 = 143 = 13 × 11. Correct. (The product of two 4-bit numbers needs 8 bits — the double-width result register is why hardware multipliers use a widened accumulator.)
Worked problem · division by shift-and-subtract
Divide 11001 by 101 in binary using restoring long division, giving quotient and remainder, and verify in decimal.
Setup: dividend 11001 (25), divisor 101 (5). Process the dividend one bit at a time, left to right.
Step 1: remainder = 1. Is 1 ≥ 101? No → quotient bit 0.
Step 2: bring down 1 → 11. Is 11 ≥ 101? No → quotient bit 0.
Step 3: bring down 0 → 110. Is 110 ≥ 101? Yes → quotient bit 1; subtract: 110 − 101 = 1.
Step 4: bring down 0 → 10. Is 10 ≥ 101? No → quotient bit 0.
Step 5: bring down 1 → 101. Is 101 ≥ 101? Yes → quotient bit 1; subtract: 101 − 101 = 0.
Result: quotient bits 00101 → 101, remainder 0.
Verify: 25 ÷ 5 = 5 exactly. Correct.

11BCD arithmetic

BCD addition proceeds nibble by nibble with a correction rule, because four bits can hold values up to 15 while a decimal digit may only reach 9. Each nibble pair is added as ordinary binary; if a nibble result exceeds 9 (1001) or a carry leaves the nibble, that nibble is corrected by adding 0110 (six) — the “add 6” rule, which skips the six illegal codes and propagates the carry properly. BCD subtraction similarly adds the 9’s or 10’s complement in BCD.

Per nibble: add as binary. If sum > 1001 or carry out of nibble → add 0110 to that nibble.
Worked problem · BCD addition with correction
Add 59 and 48 in BCD, showing the nibble arithmetic and every correction applied.
Encode: 59 = 0101 1001; 48 = 0100 1000.
Units nibble: 1001 (9) + 1000 (8) = 1 0111 — a carry out of the nibble occurs. Apply “add 6”: 0111 + 0110 = 1101, keeping the nibble carry: units digit = 0111 (7), carry 1 to the tens.
Tens nibble: 0101 (5) + 0100 (4) + 1 (carry) = 1010 — greater than 1001, invalid. Apply “add 6”: 1010 + 0110 = 1 0000: tens digit = 0000 (0), carry 1 to the hundreds.
Result: carry 1 followed by 0000 0111 = 0001 0000 0111 (BCD) = 107.
Check: 59 + 48 = 107. Correct.
BCD arithmetic is slower and needs more circuitry than binary, but it is exact for decimal fractions: 0.30 stored in BCD is exactly three tenths, while the same value in binary repeats forever. Money-handling systems still prefer BCD for this reason.

12Basic logic gates: AND, OR, NOT

Logic gates implement Boolean functions on bits. The AND gate outputs 1 only when every input is 1 (Y = A·B); the OR gate outputs 1 when at least one input is 1 (Y = A + B); the NOT gate (inverter) flips its single input (Y = A′). From these three, and from their derived cousins in the next section, any Boolean function can be built — which is why truth tables are the specification language of combinational logic.

ABA·B (AND)A + B (OR)A′ (NOT)
00001
01011
10010
11110
AND Y = A·B OR Y = A+B NOT Y = A′ NAND Y = (A·B)′ NOR Y = (A+B)′ EX-OR Y = A⊕B EX-NOR Y = (A⊕B)′ ′ denotes inversion; the small circle on the output marks inversion.
Figure — symbols of the seven gates: AND, OR and NOT (top row) and the derived NAND, NOR, EX-OR and EX-NOR, covered in the next section.

13NAND, NOR, EX-OR and EX-NOR

The NAND and NOR gates are the inverted outputs of AND and OR: Y = (A·B)′ and Y = (A + B)′. The EX-OR gate outputs 1 whenever its inputs differ, and EX-NOR (equivalence) outputs 1 whenever they agree. EX-OR is the arithmetic gate — it computes the sum bit of a half adder and underlies every adder, parity generator and comparator in the processor.

A ⊕ B = A′B + AB′,   (A ⊕ B)′ = A′B′ + AB
A ⊕ 0 = A,   A ⊕ 1 = A′,   A ⊕ A = 0,   A ⊕ A′ = 1
De Morgan: (A·B)′ = A′ + B′,   (A + B)′ = A′·B′
ABNANDNOREX-OREX-NOR
001101
011010
101010
110001
Read the EX-OR column as an odd-ones detector: it outputs 1 when the number of 1-inputs is odd, and cascading this property gives multi-input parity circuits. EX-NOR is the equality test — the output of A ⊕ B inverted is 1 exactly when A and B match.

14Universal gates

A gate is universal if any Boolean function can be implemented using only copies of it. NAND and NOR are universal; AND, OR and NOT alone are not, because no combination of them can ever produce an inverted function. Universality has practical value: a technology that manufactures one gate type well (historically TTL NAND) can build any circuit from repeated copies of it.

From NAND only:   NOT A = (A·A)′    A·B = ((A·B)′)′    A + B = (A′·B′)′
From NOR only:   NOT A = (A + A)′    A + B = ((A + B)′)′    A·B = (A′ + B′)′

The OR construction is De Morgan in disguise: NOR the two inverted inputs, giving (A′ + B′)′ = A·B — careful, that builds an AND from NOR; the OR from NOR is simply the NOR output inverted twice.

Worked problem · EX-OR from four NAND gates
Realise the two-input EX-OR function Y = A⊕B using only four 2-input NAND gates, and verify the circuit for all four input combinations.
Circuit: N1 = A NAND B. N2 = A NAND N1. N3 = B NAND N1. Output Y = N2 NAND N3. (Four gates.)
Algebra: N1 = (AB)′, so N2 = (A·(AB)′)′ and N3 = (B·(AB)′)′. Then Y = (N2·N3)′, which simplifies to A′B + AB′ = A ⊕ B.
Truth-table verification:
ABN1 = (AB)′N2N3YA⊕B
0011100
0111011
1010111
1101100
The output column Y matches A⊕B for every input row, so the four-NAND circuit realises EX-OR exactly.
Count gates carefully in such answers: EX-OR needs four NANDs, not three. Also note the same trick works with four NOR gates — swap every NAND for a NOR and the structure mirrors it.