PadhaikoAll subjects

Introduction

Digital Logic (ENEX 152)

Open in the Padhaiko app

1.1 Digital versus analog signals

An analog signal can take any value in a continuous range and changes smoothly with time (a microphone voltage, a temperature sensor output). A digital signal takes only a few discrete values, almost always two: 0 and 1.

Analog versus Digital
FeatureAnalogDigital
ValuesContinuous, infinitely manyDiscrete, usually two levels
NoiseNoise adds to the signal and cannot be removedSmall noise is ignored by the threshold, so data is regenerated cleanly
StorageHard to store without lossEasy to store and copy exactly
AccuracyLimited by component toleranceIncreased by adding more bits
DesignComplex, hard to automateBuilt from standard gates, easy to automate
ExampleAudio amplifier, thermometerComputer, calculator, digital watch

A digital system processes information that is represented by binary numbers. To use real-world analog quantities, an ADC (analog to digital converter) turns the input into bits and a DAC turns the result back into a voltage.

1.2 Logic level diagram

In a digital circuit the two logic values are represented by two voltage ranges. With positive logic, the higher range means HIGH = 1 and the lower range means LOW = 0. The range between them is the forbidden (undefined) region: a voltage there may be read as either value, so a good circuit never leaves it except while switching.

Logic Level Diagram for 5 V TTL
Voltage rangeRegionMeaning
5 V down to 2.0 VHIGH input rangeRead as logic 1 ( min = 2.0 V)
2.0 V to 0.8 VForbidden regionUndefined, may be read as either
0.8 V down to 0 VLOW input rangeRead as logic 0 ( max = 0.8 V)

A TTL output guarantees at least 2.4 V for HIGH () and at most 0.4 V for LOW (). The 0.4 V difference between output and input limits is the noise margin: V and V. A noise spike smaller than this margin cannot flip the logic value.

1.3 Digital integrated circuits (ICs)

A digital IC packs many logic gates on a single silicon chip. ICs are grouped by the number of gates (complexity) and by the technology used.

IC Integration Scale
ScaleGates per chipTypical examples
SSI (small scale)up to 12Basic gates, flip-flops
MSI (medium scale)12 to 99Counters, decoders, multiplexers, registers
LSI (large scale)100 to 9999Small memories, 8-bit microprocessor
VLSI (very large scale)10000 to 99999Large memories, microprocessors
ULSI (ultra large scale)100000 and moreModern processors

The two main logic families are TTL (transistor-transistor logic, bipolar, 74xx series, 5 V supply) and CMOS (complementary MOSFET, 74Cxx or 74HCxx, very low static power). Chapter 8 compares them in detail.

1.4 Clock triggering systems

A clock is a periodic square wave that tells a sequential circuit when to change state. A circuit can be triggered in two ways.

Edge triggering is safer because the inputs are sampled only for a very short moment. The frequency of the clock is and the duty cycle is the ratio of HIGH time to the period, .

For example a clock of period has kHz.

1.5 Digital system applications

Digital systems are everywhere because they are accurate, cheap and programmable.

1.6 Digital codes and conversions

1.6.1 Decimal, binary, octal and hexadecimal codes

A number system with radix (base) r uses digits to , and the digit at position has weight .

Number Systems
SystemRadixDigitsUsed for
Decimal100 to 9Everyday arithmetic
Binary20, 1Digital hardware
Octal80 to 7Short form of binary (3 bits per digit)
Hexadecimal160 to 9, A to FShort form of binary (4 bits per digit)

Conversion methods

  1. Binary to decimal: add the weights of the 1 bits.
  2. Decimal to binary (integer part): divide repeatedly by 2 and read the remainders from bottom to top.
  3. Decimal to binary (fraction part): multiply repeatedly by 2 and read the integer parts from top to bottom.
  4. Binary to octal or hex: group bits in threes or fours starting at the binary point, and replace each group by its digit.

1.6.2 BCD code

BCD (binary coded decimal) writes each decimal digit as its own 4-bit binary number. So becomes . The codes 1010 to 1111 are invalid in BCD. BCD is easy to display but needs more bits than pure binary (two digits take 8 bits, while 47 needs only 6 bits in binary).

1.6.3 Excess-3 code

Excess-3 is BCD plus 3 (0011) added to each digit, so decimal 0 is 0011 and 9 is 1100. It is self-complementing: the 1's complement of the code of a digit is the code of its 9's complement (for example 2 is 0101 and 7 is 1010), which makes decimal subtraction easy.

1.6.4 Gray code

Gray code is a unit-distance code: only one bit changes between consecutive numbers. It is used in shaft encoders and K-maps to avoid glitches.

BCD, Excess-3 and Gray Code for 0 to 9
DecimalBinaryBCDExcess-3Gray (4-bit)
00000000000110000
10001000101000001
20010001001010011
30011001101100010
40100010001110110
50101010110000111
60110011010010101
70111011110100100
81000100010111100
91001100111001101

1.6.5 Examples of code conversions

1.7 Alphanumeric codes: ASCII code and EBCDIC code

Computers must also store letters, digits and symbols, so each character is given a binary code.

ASCII versus EBCDIC
FeatureASCIIEBCDIC
Full formAmerican Standard Code for Information InterchangeExtended Binary Coded Decimal Interchange Code
Bits7 bits (128 characters), 8 bits in extended form8 bits (256 characters)
Used byAlmost all PCs and the internetIBM mainframes
Digit 00110000 (30H)11110000 (F0H)
Letter A1000001 (41H)11000001 (C1H)
Letter a1100001 (61H)10000001 (81H)
Letters contiguous?Yes, A to Z are consecutive codesNo, there are gaps between I, J and R, S

In ASCII the lower-case letter code is the upper-case code plus 32 (20H), the digit has code , and codes 0 to 31 are control characters such as carriage return (0DH) and line feed (0AH).

1.8 1's complement and 2's complement

For an -bit binary number:

The 2's complement is used to represent negative numbers and to turn subtraction into addition: .

1.9 Signed number representation

The leftmost bit is the sign bit: 0 means positive and 1 means negative. Three methods exist for an -bit number.

Signed Number Formats (n bits)
MethodHow -N is formedRangeZero
Sign-magnitudeSet the sign bit, keep the magnitude to Two zeros (+0 and -0)
1's complementInvert all bits of +N to Two zeros
2's complementInvert all bits and add 1 to One zero

For 8 bits, 2's complement covers to . Overflow occurs when two numbers of the same sign give a result of the opposite sign.


Worked Examples

Example 1

1 (8 marks)

Convert the decimal number into binary, octal and hexadecimal.

Integer part by repeated division by 2. rem 0; rem 0; rem 1; rem 1; rem 1; rem 0; rem 0; rem 1. Reading the remainders from bottom to top gives .

Fraction part by repeated multiplication by 2. (digit 1); (digit 0); (digit 1). So and the full number is .

Group the bits for octal and hexadecimal.

Octal (groups of 3): . Hexadecimal (groups of 4): .

Answer:

Example 2

2 (6 marks)

For the decimal number write the BCD code and the excess-3 code, and for the binary number find the Gray code. Then convert the Gray code back to binary.

BCD of 47. Code each digit in 4 bits: and , so in BCD.

Excess-3 of 47. Add 3 to each digit first: and . Hence in excess-3.

Binary 1011 to Gray. Keep the MSB: . Then , , . The Gray code is .

Gray 1110 to binary. Keep the MSB: . Then , , . The binary number is (so are a pair).

Answer:

BCD of 47 = 0100 0111; excess-3 of 47 = 0111 1010; Gray of 1011 = 1110; binary of Gray 1110 = 1011.

Example 3

3 (8 marks)

Using 8-bit 2's complement arithmetic, evaluate and then . Give each result in binary and decimal.

Write the numbers in 8 bits. and .

25 - 37. The 2's complement of : invert to get and add 1 to get (which is ). Now with no carry out. No carry means the answer is negative and is stored in 2's complement form. Its magnitude is the 2's complement of : invert to and add 1 to get . So the result is .

37 - 25. The 2's complement of : invert to get and add 1 to get . Then . There is a carry out, so the result is positive: discard the carry and read .

Answer:

(negative, no carry); (carry discarded).


Exercises

Exercises: Introduction

Question 1

Distinguish between analog and digital signals. State three advantages of digital systems.

An analog signal varies continuously and can take infinitely many values; a digital signal has only discrete levels (normally two).

Advantages of digital systems: (i) noise immunity, because any voltage above the threshold is regenerated as a clean 1 or 0; (ii) easy and exact storage and copying of data; (iii) easy design and automation with standard gates, and higher accuracy by adding more bits.

Answer:

Analog: continuous values; digital: discrete (0 and 1). Digital systems are noise immune, easy to store, accurate and programmable.

Question 2

Convert into decimal and into hexadecimal.

Integer part: . Fraction: . So the decimal value is .

Hexadecimal: group in fours from the point: (pad the fraction with a zero) .

Answer:

Question 3

Write the BCD code for the decimal number and explain why BCD needs more bits than binary. Also write its excess-3 code.

BCD codes each digit separately: , , , so (12 bits).

In pure binary needs only 9 bits. BCD needs 4 bits per digit and wastes the six codes 1010 to 1111, hence more bits.

Excess-3: add 3 to each digit first: , and , so the code is .

Answer:

BCD of 259 = 0010 0101 1001 (12 bits against 9 bits in binary); excess-3 = 0101 1000 1100.

Question 4

Write the 4-bit Gray code sequence from decimal 6 to 10 and show that only one bit changes between neighbours.

Use . For 6 (): . For 7 (): . For 8 (): . For 9 (): . For 10 (): .

Neighbouring pairs: (bit 0), (bit 3), (bit 0), (bit 1). Exactly one bit differs in every step.

Answer:

Gray codes: 6 = 0101, 7 = 0100, 8 = 1100, 9 = 1101, 10 = 1111.

Question 5

Represent and in 8-bit sign-magnitude, 1's complement and 2's complement. State the range of an 8-bit 2's complement number.

in all three systems.

: sign-magnitude sets the sign bit, giving . The 1's complement inverts all bits, giving . The 2's complement adds 1 to that, giving .

For the 2's complement range is to , that is to .

Answer:

: sign-magnitude , 1's complement , 2's complement ; range to .

Logic Gates →