0% found this document useful (0 votes)
7 views

CE162-lecture-notes-part1

The document outlines the learning outcomes and topics covered in the CE162 Digital Electronic Systems course, including number systems, digital circuit design, data transmission, and digital representation of analog signals. It emphasizes the importance of understanding binary, octal, and hexadecimal systems, as well as conversion methods between these bases. Additionally, it provides exercises and reading materials to support the learning process.

Uploaded by

selvonmosoti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

CE162-lecture-notes-part1

The document outlines the learning outcomes and topics covered in the CE162 Digital Electronic Systems course, including number systems, digital circuit design, data transmission, and digital representation of analog signals. It emphasizes the importance of understanding binary, octal, and hexadecimal systems, as well as conversion methods between these bases. Additionally, it provides exercises and reading materials to support the learning process.

Uploaded by

selvonmosoti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

School of Computer Science and Electronic Engineering

CE162 - Digital Electronic Systems

1
Learning outcomes
 Demonstrate an understanding of number systems, and
conversion methods between number bases, including fixed and
floating-point binary.
 Design digital circuits incorporating higher-level logic elements
such as counters, registers and multiplexers.
 Perform forensic byte-level interpretation of data file contents using
standard tools.
 Describe and implement a serial data transmission system.
 Select appropriate system parameters for digital representation of
analogue signals.
 Be aware of methods to perform simple manipulations of digital
image and audio data.

2
Topics to be covered
 Revision of binary number representation and binary arithmetic.
 Number conversions between arbitrary bases.
 Representation of floating-point binary numbers and applications in
digital systems; accuracy of numerical calculations.
 Use of Karnaugh map techniques in logic design, including variable-
entered map logic design.
 Sequential logic: Asynchronous and synchronous counters; finite
state machines and their design; sequence detectors.
 Asynchronous serial data transmission.
 Analogue-to-digital and digital-to-analogue conversion techniques.
 Sampling in theory (Nyquist's theorem) and in practice (sample and
hold circuits). Quantization and quantization accuracy.
 Frequency spectra and frequency domain representation of
sampled data. Fourier series.
 Data compression concepts.
3
Reading material

FLOYD, T.L., Digital Fundamentals (Prentice Hall, 2015, 11th edition).

CLEMENTS, A., The Principles of Computer Hardware (Oxford


University Press, 2006, 4th edition).

4
N.Thomos CE162 Digital Electronic Systems 1.2

Number Representation - Revision


One of the most important human inventions of all time is the 'weighted positional number system' that we now use
almost universally. It evolved in India between 200 BC and 500 AD, and started to be used in the Arabian peninsula
around 750 AD, but only became fully accepted in Europe in the 1500s.

Definition:

where the number system base is r, and the {di} are the individual digits, which can take values 0,1,..., (r-1).

Examples:
258.6310 = 2 x 102 + 5 x 101 + 8 x 100 + 6 x 10-1 + 3 x 10-2
hundreds tens units tenths hundredths
Integer + Fraction

Two other commonly-used bases:

1001.101 = 1 x 23 + 0 x 22 + 0 x 21 + 1 x 20 + 1 x 2-1 + 0 x 2-2 + 1 x 2-3 = ? (binary)


2 10

317.518 = 3 x 82 + 1 x 81 + 7 x 80 + 5 x 8-1 + 1 x 8-2 = ?


(octal) 10

Exercise: Convert the binary and octal examples to their decimal integer + fraction representation

School of Computer Science and Electronic Engineering University of Essex


N. Thomos CE162 Digital Electronic Systems 1.3

Conversion of integers from decimal to other number bases


(Where it matters, the number base is represented as a subscript, e.g. 568 is a base-8 or octal number)

The process involves repeated integer division by the base, collecting remainders as you proceed. To see how and why
this works, consider the (pointless!) 'conversion' of a number from base 10 to base 10:
e.g. 1023:

1023/10 = 102 rem 3


102/10 = 10 rem 2
10/10 = 1 rem 0
1/10 = 0 rem 1

Conversion to Binary:

e.g. 4210:

42/2 = 21 rem 0 Check by reverse conversion:


21/2 = 10 rem 1
10/2 = 5 rem 0 101010 = 1 x 25 + 0 x 24 + 1x23 + 0 x 22 + 1x21 + 0x20
5/2 = 2 rem 1 = 32 + 8 +2 = 42
2/2 = 1 rem 0
1/2 = 0 rem 1
Hence the result is 101010

Exercises:
Convert the following to Binary: 47 ,1023 , 32769
10
10
School of Computer Science and Electronic Engineering University of Essex
10
N. Thomos CE162 Digital Electronic Systems 1.4

Other useful number bases


It's well-known that digital electronic systems, including all computers, are easiest to implement when the signals they
process are allowed to have only two levels*. This leads naturally to a binary representation of all information within
the digital system. The individual elements of a binary number are called bits, from binary digits.

Counting in Binary
(IMPORTANT: You should memorise the bit patterns for the numbers representing 0 to 15. The same applies to the
integer powers of two, at least up to 216 = 65536)

Decimal Binary Decimal Binary Powers of Two


0 0 16 10000
1 1 17 10001 N 2N N 2N
2 10 18 10010 0 1 9 512
3 11 19 10011 1 2 10 1024
4 100 20 10100 2 4 11 2048
5 101 21 10101 3 8 12 4096
6 110 22 10110 4 16 13 8192
7 111 23 10111 5 32 14 16384
8 1000 24 11000 6 64 15 32768
9 1001 25 11001 7 128 16 65536
10 1010 26 11010 8 256 17 131072 etc
11 1011 27 11011
12 1100 28 11100
13 1101 29 11101 *In electronic circuits, it doesn't matter what two levels, and they do
14 1110 30 11110 not have to be accurate: the only requirement is that the circuits
11111 handling the signals can reliably distinguish between them.
15 1111 31 Hence
32 in practice, the 'low' level can be anything in the range 0 to +1.5V, and
etc. the 'high' level +3.5 to +5V
School of Computer Science and Electronic Engineering University of Essex
N. Thomos CE162 Digital Electronic Systems 1.5

Octal and Hexadecimal


While electronic circuits work most easily in binary, humans do not find bit patterns meaningful, and the typically large
number of bits involved makes errors likely. Therefore two intermediate number bases are often used in connection
with computer systems, Octal, using a base of eight, and more commonly Hexadecimal, base sixteen.

Decimal Binary Octal Hexadecimal Decimal Binary Octal Hexadecimal


0 0 0 0 16 10000 20 10
1 1 1 1 17 10001 21 11
2 10 2 2 18 10010 22 12
3 11 3 3 19 10011 23 13
4 100 4 4 20 10100 24 14
5 101 5 5 21 10101 25 15
6 110 6 6 22 10110 26 16
7 111 7 7 23 10111 27 17
8 1000 10 8 24 11000 30 18
9 1001 11 9 25 11001 31 19
10 1010 12 A 26 11010 32 1A
11 1011 13 B 27 11011 33 1B
12 1100 14 C 28 11100 34 1C
13 1101 15 D 29 11101 35 1D
14 1110 16 E 30 11110 36 1E
15 1111 17 F 31 11111 37 1F
32 100000 40 20
etc.
The reason for using octal and hexadecimal is that converting between these to and from binary is very straightforward,
but only if you can remember the binary for 0-1510. For octal, take bits in groups of 3; hexadecimal uses groups of 4.

School of Computer Science and Electronic Engineering University of Essex


N. Thomos CE162 Digital Electronic Systems 1.6

Octal is particularly useful as an intermediate for manually converting decimal to binary, as it involves fewer divisions
and there is less chance for error. (Using hexadecimal is more difficult, unless you also know the sixteen times table!)

Example

Convert 1234567810 to binary, via octal, without using a calculator.

12345678/8 = 1543209 rem 6


1543209/8 = 192901 rem 1
192901/8 = 24112 rem 5
24112/8 = 3014 rem 0
3014/8 = 376 rem 6
376/8 = 47 rem 0
47/8 = 5 rem 7
5/8 = 0 rem 5

Hence the octal conversion is 570605168 . Note that the final division is redundant - stop when the value of the fraction
is less than 1, i.e., the numerator is less than the denominator (base).

Converting each octal digit to three bits gives:

101 111 000 110 000 101 001 110


2

If you now want the number in hexadecimal, rearrange the bits in groups of four:

1011 1100 0110 0001 0100 11102

From memory, or looking up from the first table on the previous page, gives
BC614E16

School of Computer Science and Electronic Engineering University of Essex


N. Thomos CE162 Digital Electronic Systems 1.7

Exercises

Convert to Decimal (the convention in the C programming language is for Octal numbers to start with '0' and hexadecimal '0x'):

0377, 0xFF, 02003, 0x5AFE

Convert base ten numbers to Octal, then Binary and Hexadecimal:

2003, 4096, 12648430

Conversion of Decimal Fractions to Binary


What is 0.625 as a binary fraction? Anyone familiar with Imperial measurement units, where 'power of two' fractions of
an inch are commonly used, knows that this is 5/8.

It can also be represented as 4/8 + 1/8 = 1/2 + 1/8 = 2-1 + 2 -3 = 0.5 + 0.125.

The equivalent binary fraction is thus: 0.101

The general rule for converting a fractional number to its binary equivalent can again be seen by doing the redundant
process in decimal. We take the original number, and multiply by the base. The value of the integer that is obtained
becomes a digit of the fraction, starting with the most significant. The remaining fractional part is multiplied by the base
again, and so on until we are left with zero, but this does not always happen.

0.625 x 10 = 6.25 Repeat in base 2:


0.25 x 10 = 2.5 0.625 x 2 = 1.25
0.5 x 10 = 5.0 0.25 x 2 = 0.5
0.5 x 2 = 1.0
Stop. Result is 0.62510
Stop. Result is 0.101 2
School of Computer Science and Electronic Engineering University of Essex
N. Thomos CE162 Digital Electronic Systems 1.8

Other examples Suppose we decide to limit the fraction to 9 bits of


precision.
What is the binary equivalent of 0.110?
An easy way to find the decimal equivalent of a fractional
0.1 x 2 = 0.2 binary number is to note that
0.2 x 2 = 0.4
0.4 x 2 = 0.8 0.000110011 2 = 110011 x 2-9 .
0.8 x 2 = 1.6
0.6 x 2 = 1.2 (Looks like a problem here...!) 0011 00112 = 3316 = 3 x 16 + 3 = 5110
0.2 x 2 = 0.4
0.4 x 2 = 0.8 Hence the decimal equivalent of the truncated binary for
0.110 is:
0.8 x 2 = 1.6
0.6 x 2 = 1.2
0.2 x 2 = 0.4 51 x 2-9 = 51/512 = 0.099609375 10
...
The error is about 0.4%.
and so on for ever, which means that 0.1 cannot be
Exercises
exactly represented in binary. This is an unfortunate side-
effect of having ten fingers*, in turn almost certainly the Convert the following to binary. Where appropriate limit
reason why human number systems like using base ten. precision to 10 bits after the binary point.
0.984375, , 0.7.
i.e. 0.110  0.000110011001100...2
What is the decimal back-conversion of the binary
approximation to 0.1 if the precision is extended to 13
bits?

*It's interesting to note that measurement or unit systems that predate the 'decimal' generally do not have ten as one of their factors: most
Imperial measurement in inches uses divisions of 1/2, 1/4, 1/8 and so on, as already mentioned. Twelve is a favourite: 12 inches to the foot,
12 (old) pence to a shilling, 240 old pence to a pound sterling. Twelve has the advantage of having more integer divisors than ten: two,
three (most useful) four and six. If evolution had given us eight, twelve or sixteen fingers, things might have been different.

School of Computer Science and Electronic Engineering University of Essex


N. Thomos CE162 Digital Electronic Systems 1.9

Binary Arithmetic Rule for Addition:


The basic rules are identical to decimal. c s
0 + 0 = 0 0
Addition: 0 + 1 = 0 1
1 + 0 = 0 1
Add bit by bit. Each pair of bits generates a Sum and 1 + 1 = 1 0
potentially a Carry that is taken to the next column,
e.g. (c = carry, s = sum)

1010 + 0011 = 1101 (10 + 3 = 13)


Rule for Subtraction:
1010+
bb d
0011 0 - 0 =
1101 00 0
0 1 0 carry 0 - 1 = 10 1
bits 1 - 0 = 00 1
1 - 1 = 00 0
Subtraction: (d = difference, bb = borrow: a '1' borrowed from the
Each pair of bits generates a difference, and may adjacent column becomes '10' (=2))
potentially require a Borrow from the next column left:

e.g. 1010 - 0011 = 0111 (10 - 3 = 7)

In practice, the 'direct' method is not used in computers.


Addition with Two's Complement negative numbers is a
more efficient method - see later.

School of Computer Science and Electronic Engineering University of Essex


N. Thomos CE162 Digital Electronic Systems 1.10

Binary arithmetic

Multiplication
Repeated addition can be used: to compute AxB add A to itself B times (or B to itself A times, whichever needs fewer
operations.)

Either way, this is inefficient, so usually the 'shift and add' principle of conventional long multiplication is adapted for
binary; the implementation is simpler than for non-binary and can be done in physical hardware or 'algorithmically' in
software.

e.g. 1010 x 0111 (10 x 7 = 70)

1010 x
0111
1010 replicate the multiplicand if there is a '1' in the multiplier, otherwise 0
10100 shift left one place
101000 shift left two places
0000000
1000110 check: 0100 01102 = 4616 = 4x16 + 6 = 70

Division
Division is more complicated, and nearly always done algorithmically. The simple (but inefficient) version uses
repeated subtraction of the divisor from the dividend, counting the number of subtractions needed until the result
becomes  0.

School of Computer Science and Electronic Engineering University of Essex


N. Thomos CE162 Digital Electronic Systems 1.11

Representing Negative Integers Two's Complement


There are two main methods. Although it seems more complicated, this method has
significant advantages. It is universal in computer
'Sign and magnitude' hardware.

In a fixed-length word, one bit is reserved for the sign. For For N-bit word length, -A is represented as 2N - A.
example in an 8-bit system, using the MSB for the sign:
The most significant bit (MSB) becomes 1 for a -ve
0111 1111 = +127 number, 0 for +ve.
1111 1111 = -127
Consider word length 8 bits:
The first problem here is that there are two ways of
representing zero: 1000 0000 and 0000 0000. +62 = 0011 1110
-62 = 28-62 = 256-62 = 194 = 1100 0010
Another awkwardness is that simple arithmetic addition of
the representations of the positive and negative forms of a In practice, the two's complement can be obtained by
number does not generate zero, e.g. complementing each bit and adding 1.

0000 0001 +1 0011 1110 +62


1000 0001+ -1 1100 0001 complement each bit (1 to 0 and 0 to 1)
1000 0010 -2? 0000 0001+ add 1
1100 0010
This system is sometimes used in specialised digital
hardware, but never in modern computers. Avoid! Use the same procedure to convert -ve to +ve:

1100 0010 -62


0011 1101 complement
0011 1110 add 1: +62

School of Computer Science and Electronic Engineering University of Essex


N. Thomos CE162 Digital Electronic Systems 1.12

Number Range in Two's Complement Addition with two's complement


e.g. using 4 bits: Examples assume 8 bit representation.
0111 = +7 The possibilities are:
0110 = +6
0101 = +5 Both numbers positive 0000 0011
0100 = +4 • Sum is +ve +0000 0110
0011 = +3 • Warning: overflow could ?
0010 = +2 occur
0001 = +1
0000 = 0
1111 = -1 +ve number with magnitude > -ve number 0110 0000
1110 = -2 • Disregard final carry. Sum is +ve +1111
1101 = -3 1100
1100 = -4
1011 = -5
1010 = -6
1001 = -7 -ve number with magnitude > +ve number
1000 = -8
1000 1100
• Sum is -ve

+0000 1000
From this, work out a general rule to find the range of Both numbers negative 1111 1101
Exercise
two's complement numbers using N bits. • Disregard final carry. Sum is - +1111 1111
ve
What is the range that can be represented using 8 bits?
• Warning: overflow could occur

School of Computer Science and Electronic Engineering University of Essex


N. Thomos CE162 Digital Electronic Systems 1.13

Two's Complement Arithmetic Exercise

Devise a general rule that could be used to detect an


Addition - overflow conditions overflow condition.

The problem arises when the result of a calculation lies


outside the range of numbers that can be represented in
the word length being used. Assuming 8 bits, this means Subtraction in 2's complement arithmetic
that any result greater than +127 or less than -128 will
cause overflow. Overflow cannot occur if the sign bits of Subtraction does not need specialised hardware, since
the numbers are different. (e.g.)

Two positive numbers: +9 - 4 = +5

0111 1110 7E16 = 126 can also be done by addition by using


0000 0111+ +716 = 7
1000 0101 8516 = 133 +9 + (-4) = +5

But, if we're working in 8 bit 2's complement, 8516 will be 2's complement processing, provided overflow does not
interpreted as - what? occur, correctly computes the addition of all possible
combinations of +ve and -ve numbers, so specialised
Two negative numbers: hardware to perform subtraction is not needed in a
computer system. An adder and means to complement
1000 1001 8916 = -119 (2's C. is 77 =16119) binary numbers - both easy operations - are sufficient.
1011 0001 +B116 = -79 (2's C is 4F16 = 79)
(1)0011 1010 3A = +58 Wrong! (ignore carry)
16

The reason is that -119 + (-79) = -198, which is outside the


8 bit range.

School of Computer Science and Electronic Engineering University of Essex


N. Thomos CE162 Digital Electronic Systems 1.14

2's Complement Arithmetic Division

Multiplication (repeated subtraction method)

P=AX Q=A/ B
B
'Shift and add' multiplication cannot be done directly Check sign bits of A and B
with signed numbers. The algorithm is: if different, set a 'flag' bit to indicate result will be -ve.
if same, result will be +ve. Flag is not set.
Check sign bits of A and B
if different, set a 'flag' bit to indicate product will be - Make A positive and B negative (2's complement)
ve. if same, product will be +ve. Flag bit is not set.
Initialise quotient (Q) to 0
Make A and B both
positive Initialise partial remainder to A
Perform shift-and-add multiplication as before.
loop:
If flag bit set, make product negative by taking 1. Subtract B from partial remainder using
2's complement 2's complement addition of (-B).

Done. 2. Add 1 to Q

Note that overflow may occur: what is the potential length 3. if partial remainder is zero or negative exit loop
in bits of the result if you multiply two N bit binary otherwise repeat from loop.
numbers?

If flag bit is set, take 2's complement of quotient to


make it negative.

School of Computer Science and Electronic Engineering University of Essex


N. Thomos CE162 Digital Electronic Systems 1.15

Signed and Unsigned variables We want the sign of the longer version of the variable to
be the same as that of the original; this is done by a
In the C programming language, integers can be of five process called sign extension.
basic types which vary in size, depending on the
processor, in units of 1 byte (or 8 bits). This program and Suppose we have the numbers +63 and -120 in 8-bit 2's
its output list the sizes on a PC running Linux: complement char variables. In binary:

[tim2@james tim]$ cat sizeof.c 63 = 3F16 = 0011 1111


+120 = 7816 = 0111 1000
main(){
printf (“\nVariable sizes (bytes):\n”);
printf (“char\t\t%d\n”, sizeof(char)); 2's complement:
printf (“short\t\t%d\n”, sizeof(short)); -120 = 1000 1000
printf (“int\t\t%d\n”, sizeof(int));
printf (“long\t\t%d\n”, sizeof(long));
If we now assign these to short (2 byte/16 bit) variables
printf (“long long\t%d\n\n”, sizeof(long long));
exit(0); the bit patterns need to be:
}
0000 0000 0011 1111 (+63)
[tim2@james tim]$ gcc sizeof.c -o sizeof 1111 1111 1000 1000 (-120) (check
[tim2@james tim]$ sizeof
it!)
Variable sizes (bytes): ...and so on for the longer word lengths. The process is
char 1 achieved very simply by replicating the sign bit of the char
short 2
int 4
into the 'blank' bits of the longer variable.
long 4
long 8 Byte-sized quantities are rarely signed in practice, being
long
treated as positive integers in the range 0 to 255.
In addition, integer variables (which are normally Unexpected and unwanted effects can occur unless the
processed as two's complement numbers) can be declared variable concerned is declared as unsigned char, in
to be of type unsigned. This mainly affects what happens which case sign extension does not occur.
when an expression assigns the value of a variable to
another type that is longer (occupies more bytes).

School of Computer Science and Electronic Engineering University of Essex


N. Thomos CE162 Digital Electronic Systems 1.16

The ASCII Character Code


The principal means of communication between humans and computers is
through alphanumeric display and keyboard in typewriter layout, with the
addition of mouse or other pointing device in the last 30 years or so. Data
are transmitted to the processor usually in serial form, reflecting the origin of
the keyboard in the mechanical teletype (right, as used in computer rooms
until the 1980s. Picture from http://www.kekatos.com/teletype/gil/M33.htm).
ASCII stands for American Standard Code for Information Interchange.
Derived from pre-computer age teleprinter codes, it defines the principal
basic keyboard and display character set employed in computer systems and
uses 7 bits per character (8 bits are actually available, but the spare one is
normally set to 0 or used as a 'parity' bit for error checking.)
Codes 0 - 0x1f are so-called non-printing 'control' codes, that have specific
names, and originally performed mechanical control functions on the
teleprinter; hence 0xa (octal 012, decimal 10) is generated by the 'line feed'
(LF) key, and 0xd (015, 13) is 'carriage return' (CR), commonly the 'Enter'
command key today. Control codes are generated when the CTRL and an
Ordinary key are pressed together: hence CTRL-C produces binary 0000 0011 (3).
ASCII is also the basis for the ISO-8859 series of character sets which extends the range to include values 0x80 to 0xff
and represents characters that do not appear in English: for example, accented letters (é,ü, ç), currency symbols other
than '$' (£, ¥) and some Greek characters (, ). To see a full listing of ASCII and the ISO-7 series, try 'man 7 ascii'
and 'man 7 iso_8859-15' on a Unix/Linux workstation.
Unicode (16-bit code) has been designed to represent characters from most of the world's written language such as
Japanese and Chinese. The fist 256 characters of Unicode map onto the ASCII character set, making ASCII to Unicode
conversion easy.
The listing of the 7-bit ASCII character set, in various formats, follows from page 20 onwards.
School of Computer Science and Electronic Engineering University of Essex
N. Thomos CE162 Digital Electronic Systems 1.17

Teletype close-up
The units at left are paper tape punch (top) and paper tape reader.
Punched paper tape was sometimes the only backup medium for programs
and data. The TTY printed at 10 characters/second, and could read tapes
at the same speed. 'High speed' (300 characters/second) paper tape
readers and punches were also available, but as separate units.

As an exercise in detective work, try and decode what is 'written' on


the visible section of the paper tape, based on the ASCII codes listed
on the next pages.

Note that each character occupies a row of 7 bits plus a parity bit. EVEN
parity is being used here. The large holes are data, a hole representing
binary '1', and no hole '0'. The small holes engage with the teeth of the drive
sprocket.

For a bit of computing nostalgia, see:


http://www.youtube.com/watch?v=E4IztV7M3jI

School of Computer Science and Electronic Engineering University of Essex


N. Thomos CE162 Digital Electronic Systems 1.18

Error checking - parity This system fails, of course, if more than one bit is in error,
but if the error probability is moderately low, then it is quite
The 7-bit ASCII code sometimes incorporates a simple effective.
error checking process which uses the spare bit of each
byte. Error checking is needed on 'noisy' data channels Assuming the errors are random, and that they occur with
(e.g. radio), when bits may randomly change from 1 to 0 probability p, then the probability of a bit being received
or 0 to 1. Using the 8th bit as a parity bit on serial correctly is (1-p).
communication ports is an option that can be selected.
In an n-bit word, the probability of exactly one error is:
Parity can be 'Odd' or 'Even', meaning that the number
of 1s in each byte, including the parity bit, is odd or even. np(1-p)n-1 (1)
If the receiver detects the wrong parity, then an error is
likely. For example, consider 4 bit data words: The error could occur in n places, while the probability of
the remaining (n-1) bits being correct is (1-p)n-1.
Data Odd parity Even parity
0000 1 0 The probability of exactly 2 errors is:
0001 0 1
0010 0 1
0011 1 0 n
C 2.p (1-p)
2 n-2
(2)
0100 0 1
0101 1 0 As an example, let p = 10-4, a very high error rate in
0110 1 0 practice, with 8 bit data.
0111 0 1
1000 0 1
From eqn (1), P(exactly 1 error) = 7.9944x10-4 ( 8 x 10-4).
1001 1 0
1010 1 0
1011 0 1 From eqn (2), P(exactly 2 errors) =
1100 1 0
8
C 2.10-8 (0.9999) 6 = 28x10 -8 x 0.999400 = 2.798 x 10-7
1101 0 1
1110 0 1
1111 1 0 This is a huge reduction, so it's likely that the parity
system will work most of the time.

School of Computer Science and Electronic Engineering University of Essex


N. Thomos CE162 Digital Electronic Systems 1.19

Hamming codes Example:

In the late 1940’s Richard Hamming recognized that the Let us consider the bit sequence (I4, I3, I2, I1)= (1, 1, 0, 1).
further evolution of computers required greater reliability, in We compute the parity bits as follows:
particular the ability to not only detect errors, but correct
them. His search for error-correcting codes led to the C3 = 0 + 1 + 1 = 0
Hamming Codes, perfect 1-error correcting codes, and the
C2 = 1 + 1 + 1 = 1
extended Hamming Codes, 1-error correcting and 2-error
C1 = 1 + 0 + 1 = 0
detecting codes.

Hamming Codes are still widely used in computing, Thus, the Hamming encoded code word is
telecommunication, and other applications. Hamming (I4, I3, I2, C3, I1, C2, C1) = (1, 1, 0, 0, 1, 1, 0)
Codes are also applied in
• Data compression Suppose that the code word is corrupted during storage
• Some solutions to the popular puzzle ‘The Hat Game’ and I3 value is switched from 1 to 0. The resulting word is
• Block Turbo Codes (I4, I3, I2, C3, I1, C2, C1) = (1, 0, 0, 0, 1, 1, 0)
Let’s consider a sequence of bits (I 4, I3, I2, I1). The binary
If we calculate the parity symbols again we have
(7,4) Hamming code adds three parity symbols for error
detection and correction. The parity bits are placed at the
C3 ’ = 0 + 0 + 1 = 1
20 = 1, 21 = 2, and 22 = 4 bit positions. It encodes the
original sequence of bits to a new (I 4, I3, I2, C3, I1, C2, C1), C2 ’ = 1 + 0 + 1 = 0
where C1, C2, C3 correspond to the parity bits. This can be C1 ’ = 1 + 0 + 1 = 0
computed as
We can see that C3’, C2’, C1’ is different from C3, C2, C1.
C3 = I2 + I3 + I4, modulo-2 addition Thus, there is an error. We can find its position!!!
C2 = I 1 + I 3 + I 4 This is done by performing XOR of the old and the new
C1 = I 1 + I 2 + I 4 bits, i.e., ( C3’ + C3, C2’ + C2, C1’ + C1 ) = (1, 1, 0) (modulo-2
addition)
The (7,4) Hamming code is able to correct a single bit error
and detect up to two bit errors. Thus, the error happened in the sixth bit, which is correct!
School of Computer Science and Electronic Engineering University of Essex
N. Thomos CE162 Digital Electronic Systems 1.20

Non-printing ASCII characters Printing characters


Binary Octal Dec Hex Char C escape Binary Octal Dec Hex Char

0000 0000 000 0 00 NUL ‘\0’ 0010 0000 040 32 20 SPACE


0000 0001 001 1 01 SOH 0010 0001 041 33 21 !
0000 0010 002 2 02 STX 0010 0010 042 34 22 “
0000 0011 003 3 03 ETX 0010 0011 043 35 23 #
0000 0100 004 4 04 EOT 0010 0100 044 36 24 $
0000 0101 005 5 05 ENQ 0010 0101 045 37 25 %
0000 0110 006 6 06 ACK 0010 0110 046 38 26 &
0000 0111 007 7 07 BEL ‘\a’ 0010 0111 047 39 27 ‘
0000 1000 010 8 08 BS ‘\b’ 0010 1000 050 40 28 (
0000 1001 011 9 09 HT ‘\t’ 0010 1001 051 41 29 )
0000 1010 012 10 0A LF ‘\n’ 0010 1010 052 42 2a *
0000 1011 013 11 0B VT ‘\v’ 0010 1011 053 43 2b +
0000 1100 014 12 0C FF ‘\f’ 0010 1100 054 44 2c ,
0000 1101 015 13 0D CR ‘\r’ 0010 1101 055 45 2d -
0000 1110 016 14 0E SO 0010 1110 056 46 2e .
0000 1111 017 15 0F SI 0010 1111 057 47 2f /
0001 0000 020 16 10 DLE 0011 0000 060 48 30 0
0001 0001 021 17 11 DC1 0011 0001 061 49 31 1
0001 0010 022 18 12 DC2 0011 0010 062 50 32 2
0001 0011 023 19 13 DC3 0011 0011 063 51 33 3
0001 0100 024 20 14 DC4 0011 0100 064 52 34 4
0001 0101 025 21 15 NAK 0011 0101 065 53 35 5
0001 0110 026 22 16 SYN 0011 0110 066 54 36 6
0001 0111 027 23 17 ETB 0011 0111 067 55 37 7
0001 1000 030 24 18 CAN 0011 1000 070 56 38 8
0001 1001 031 25 19 EM 0011 1001 071 57 39 9
0001 1010 032 26 1A SUB 0011 1010 072 58 3a :
0001 1011 033 27 1B ESC 0011 1011 073 59 3b ;
0001 1100 034 28 1C FS 0011 1100 074 60 3c <
0001 1101 035 29 1D GS 0011 1101 075 61 3d =
0001 1110 036 30 1E RS 0011 1110 076 62 3e >
0001 1111 037 31 1F US 0011 1111 077 63 3f ?

School of Computer Science and Electronic Engineering University of Essex


N. Thomos CE162 Digital Electronic Systems 1.21

Binary Octal Dec Hex Character Binary Octal Dec Hex Character

0100 0000 100 64 40 @ 0110 0000 140 96 60 ‘


0100 0001 101 65 41 A 0110 0001 141 97 61 a
0100 0010 102 66 42 B 0110 0010 142 98 62 b
0100 0011 103 67 43 C 0110 0011 143 99 63 c
0100 0100 104 68 44 D 0110 0100 144 100 64 d
0100 0101 105 69 45 E 0110 0101 145 101 65 e
0100 0110 106 70 46 F 0110 0110 146 102 66 f
0100 0111 107 71 47 G 0110 0111 147 103 67 g
0100 1000 110 72 48 H 0110 1000 150 104 68 h
0100 1001 111 73 49 I 0110 1001 151 105 69 i
0100 1010 112 74 4a J 0110 1010 152 106 6a j
0100 1011 113 75 4b K 0110 1011 153 107 6b k
0100 1100 114 76 4c L 0110 1100 154 108 6c l
0100 1101 115 77 4d M 0110 1101 155 109 6d m
0100 1110 116 78 4e N 0110 1110 156 110 6e n
0100 1111 117 79 4f O 0110 1111 157 111 6f o
0101 0000 120 80 50 P 0111 0000 160 112 70 p
0101 0001 121 81 51 Q 0111 0001 161 113 71 q
0101 0010 122 82 52 R 0111 0010 162 114 72 r
0101 0011 123 83 53 S 0111 0011 163 115 73 s
0101 0100 124 84 54 T 0111 0100 164 116 74 t
0101 0101 125 85 55 U 0111 0101 165 117 75 u
0101 0110 126 86 56 V 0111 0110 166 118 76 v
0101 0111 127 87 57 W 0111 0111 167 119 77 w
0101 1000 130 88 58 X 0111 1000 170 120 78 x
0101 1001 131 89 59 Y 0111 1001 171 121 79 y
0101 1010 132 90 5a Z 0111 1010 172 122 7a z
0101 1011 133 91 5b [ 0111 1011 173 123 7b {
0101 1100 134 92 5c \ 0111 1100 174 124 7c |
0101 1101 135 93 5d ] 0111 1101 175 125 7d }
0101 1110 136 94 5e ^ 0111 1110 176 126 7e ~
0101 1111 137 95 5f _ 0111 1111 177 127 7f DELETE

School of Computer Science and Electronic Engineering University of Essex


N. Thomos CE162 Digital Electronic Systems 1.22

Exercises “0110”,
“0111”,
1 For completeness, and as an example of simple C “1000”,
language programming, here is the program that “1001”,
generated most of the ASCII code listing on the previous “1010”,
“1011”,
pages. How does it work?
“1100”,
“1101”,
2 By examining the binary for a character and its shifted “1110”,
version (e.g. a and A, 1 and !), work out what functions “1111”};
are performed by the SHIFT key. Do the same for CTRL.
main(){
3 What letter keys in combination with CTRL produce CR int cnt;
('carriage return', from teleprinter usage) and LF (line unsigned char code;
feed)? Because they are used frequently, on a keyboard
these have their own dedicated keys as well, as do HT
(horizontal tab) and DELETE. code = ‘ ‘;

cnt = 0x7f - ‘ ‘;
/*
printf (“\n\nBinary\t\tOctal\tDec\tHex\
Program to list printable ASCII characters in binary,
tCharacter\n”);
octal, decimal and hexadecimal
*/
while (cnt--){
/* printf (“%s %s\t%03o\t%3d\t%2x\t%c\n”,
binstrings[(code & 0xf0) >> 4],
Array containing character strings representing the
binstrings[code & 0xf],
binary conversions of 0 - 0xF.
code, code, code, code);
(The printf() procedure doesn't have a 'binary' output
code++;
format!)
}
*/
exit(0);
char *binstrings[] = {
}
“0000”,
“0001”,
“0010”,
“0011”,
“0100”,
“0101”,

School of Computer Science and Electronic Engineering University of Essex


N. Thomos CE162 Digital Electronic Systems 1.23

Serial and Parallel Data Transmission


Within a digital system, parallel processing of data is nearly universal;
this means that all the bits of a byte, or combinations of bytes up to the
maximum word length of the system, are available within the hardware
simultaneously. The disadvantage of parallel data handling is that it
requires as many connections as the number of bits to link one element
of the system to the next; when there are 64 or 128 bits involved, this
becomes a major factor in system construction and complexity.

Rather than provide a separate group of wires between each


component in a system, the interconnections will usually be shared on
a set of parallel connections called a bus (from the Latin word omnibus
meaning 'all'). This greatly reduces complexity, but has some
restrictions in that only one subsystem can insert information onto the
bus at any one time, but any number of subsystems can read it off.

This slide shows the front (top) and rear (bottom) of a small three-layer
backplane unit for the VME Bus, commonly used in industrial
applications. This version uses 3x64 pin connectors. Thin lines carry
signals, thick ones power and ground.

School of Computer Science and Electronic Engineering University of Essex


N. Thomos CE162 Digital Electronic Systems 1.24

Closeup of a databus on a PC
backplane

Why do so many of the signal lines take a


convoluted path?

School of Computer Science and Electronic Engineering University of Essex


N. Thomos CE162 Digital Electronic Systems 1.25

Interfacing to databuses
The standard way of driving a databus uses tristate logic
devices (right). As the name implies, these have three
output conditions:

Enabled:

• Outputs can be driven to Logic 'HIGH' or ' LOW'.


Typical bus interface device is the 74LS244. It contains 8
non-inverting buffers with high-power 'tristate' outputs and
Disabled: high impedance Schmitt-Trigger inputs: together these
enable long bus lines to be driven and read from with low
• Outputs are disconnected, or in practice set to an likelihood of signal loss or interference. The buffers are in
electrically high impedance state, so they do not insert two groups that can be separately enabled by LOW
an unwanted electrical load onto the bus when inactive. signals on inputs 1G or 2G.

The system designer has to ensure that only one tristate


device is enabled at any one time onto a databus signal
line. Why is this?

A data register has a dual role: to 'retime' data so that all


bits change at the same time, and to act as a buffer. By
storing data temporarily (while the bits are picked-up by
another device connected to the bus, for example) the
source is able to carry on with something else. 8 bit data register with built-in tristate output bus drivers.

School of Computer Science and Electronic Engineering University of Essex


N. Thomos CE162 Digital Electronic Systems 1.26

Data Register
The function of a data register is to act as a temporary store for information. It does this by capturing the state (logic
high or low) of each input line in parallel and simultaneously onto individual binary storage elements called 'flip-flops'.

Most registers are 'edge triggered' (positive or negative), which means that the capture takes place on a transition
(that is, a change from low-to-high or high-to-low) of the strobe or clock pulse input. The state of the input line the
instant before the transition is the one that matters; at other times the data inputs are ignored.

The example device block diagram (previous page) shows the 74LS374 octal register. This device does not have a
'clear' input, which forces all outputs to zero when activated, but many do.

To get word lengths longer than 8 bits, additional registers are added as required, but driven from the same clock/
enable/clear inputs.

Exercise
Draw the output waveform from O0 if the input, D0 , and clock waveform are as shown. The positive edge (arrows) is the active one

Data
Input, D0

Clock/
strobe

Output,
O0?
Time

School of Computer Science and Electronic Engineering University of Essex


N. Thomos CE162 Digital Electronic Systems 1.27

Asynchronous Serial Data Transmission


Transmitting data in parallel form is only practical over short
distances, up to a metre or so, otherwise the cabling
requirements become impractical. Instead, bits can be
loaded into a parallel-in, serial-out register, and transmitted
on a single channel, one bit at a time. At the receiver, the
bits are loaded into a serial-in parallel-out register for use
by the destination in the normal parallel form.
The original main computer input/output device was a
keyboard and display connected to the processor via a
serial port, using the ASCII code and the RS232
asynchronous data transmission standard. RS232 is very
simple to implement, especially at the 'transmitter' end. An
RS232 transmitter outputs a waveform like the illustration,
which is a real example. The device that does this is called
a Universal Asynchronous Receiver/Transmitter, or
UART. It contains both the transmit and receive parts Signal waveforms for RS232 transmission of the ASCII
Characters 'A' (4116 = 0100 0001)
which operate independently.
and 'U' (55 16 = 0101 0101).
Each character or byte is transmitted independently, and
comprises a sequence of around 10 bits (the exact number Modern high-speed versions of serial data transmission
can vary with the application). The resting state of the systems include USB and FireWire. Serial data
RS232 output is -4 V, or logic '1' level. An active data byte transmission at very high rates* (150 Mb.s-1) now
is commonly replaces parallel data even within a standalone
preceded by a START bit, a transition to +4 V, logic '0'. The PC box.
active data bits then follow in sequence, starting with the
least significant bit. The end of the transmission is signalled *SATA = Serial Advanced Technology Attachment format.
by one or more STOP bits, rests at -4 V, prior to the next start
bit, if any.
School of Computer Science and Electronic Engineering University of Essex
N. Thomos CE162 Digital Electronic Systems 1.28

Receiving RS232 serial data To receive serial data reliably also requires a clock or
The RS232 standard defines a number of 'Baud rates', strobe waveform, ideally synchronised to the middle of the
formally defined as 'symbols per second'*, but here it is the input bit period. Why is this?
same as bits per second. Some standard Baud rates are:
Because the receive clock is reset by each start bit, so
75 as long as the transmitter and the receiver agree on the
110 Baud rate, and number of bits per character, then its
300 accuracy does not have to be very great: provided drift is
1200 less than, say, 25% of the bit period in 10 bits, then
2400 performance will be satisfactory. In practice, a very stable
9600 crystal oscillator-driven Baud rate generator will be used.
19200
38400 Why 'asynchronous'?

and so on. Baud rates 75 and 110 were used with The reason is, as above, that the clocks at transmitter and
receiver do not have to be precisely locked in frequency
mechanical Teletypes and similar devices.
and phase, as would be the case in a synchronous
system. In synchronous systems, it is usually not
Converting to serial form can be done with a 10- bit
convenient to provide a separate path for data and clock,
parallel-in, serial-out (PISO) shift register, assuming 1
so the receive clock has to be recovered in some way from
start bit, 1 stop bit and 8 data bits. The bits corresponding
the data sequence itself. How this is done cannot be
to the start and stop bits would be hardwired to logic 0 and
covered in detail here, but it involves knowing the precise
1 respectively. Similarly, receiving serial data uses a serial-
clock frequency and that the signal transitions on the
in, parallel-out (SIPO) shift register to reassemble each
incoming data stream happen at integer multiples of the
byte.
clock period. By ensuring that there is a regular 'supply' of
such transitions (i.e., you need to avoid transmitting long
*Serial digital data transmission often uses more than two levels for strings of 0’s like 00000000...!), then clock recovery can be
signalling. For example, with 4 discrete levels, each symbol carries two
bits of information. If the Baud rate is N symbols per second, then the done quite easily. The next problem is to detect the
data rate for 4 levels would be 2N bits per second. 'framing' of the incoming data, i.e., knowing where is the
start of each byte.
What is the bit rate for 8 level signalling?
School of Computer Science and Electronic Engineering University of Essex
N. Thomos CE162 Digital Electronic Systems 1.29

Text and Binary Files

Data on computer systems are organised in 'files'.

A 'file' comprises an ordered sequence of (usually)


bytes, the interpretation of which is entirely
dependent on the application that generates or
reads back the data.

'Binary' files contain bytes that occupy the full range


of values, 0x00 to 0xFF. 'Text' files, of course, also
contain data in binary, but the values lie within the
ASCII code range. A text file if fed directly to a dumb
printer or file listing program will (or should!) produce
coherent, human-readable text. Binary files will
generally produce garbage.

Most operating systems provide an application to


view the 'raw' byte values; this can be very useful in
identifying the content of an unknown file. Here we
present two examples from Linux, showing the
source for the list_ascii program on page 1.22, and
part of the executable binary file generated by the C
compiler from the same source (next page).

The Linux utility application KHexEdit gives the raw data in hex on the left, and an attempted ASCII interpretation on the
right. 'Unprintable' characters, including such as CR and LF, come out as '.'. (You can also use this to edit files at the
binary level; for example it would be possible to change the character strings in the executable file.)

School of Computer Science and Electronic Engineering University of Essex


N. Thomos CE162 Digital Electronic Systems 1.30

Part of the Linux executable binary file for the


list_ascii program. The section shown includes
the text string containing the format statement for
the printf call. Here the ASCII interpretation is
mostly unsuccessful, but unencrypted binary files,
especially executable programs, often contain
snatches of readable ASCII text; these can be
useful for forensic analysis.

The application that generates this display can


give other commonly-used interpretations of the
binary data, for example as short (2 byte) or long
(4 byte) decimal integers. It also knows about
varying byte order: on Intel series processors, this
is 'least significant byte at lowest (byte) address',
or 'Little Endian'. Other processors use the
reverse, 'Big Endian', which is actually the more
sensible option, as bytes listed in address order
are then also correctly oriented in the longer word
lengths.

Not being aware of the possibility of different byte


ordering conventions can cause problems when
multi-byte binary data are transported between
different processor types. See later.

Sophisticated versions of applications like this can


'disassemble' executable files back to assembly
language statements, an even more powerful (and
possibly dangerous!) facility.
School of Computer Science and Electronic Engineering University of Essex
N. Thomos CE162 Digital Electronic Systems 1.31

OD(1) BSD General Commands Manual


Other file examination tools NAME
OD(1)

od — octal, decimal, hex, ASCII dump

Unix or Linux-based operating systems also have various SYNOPSIS


command-line tools to allow examination of a file at the od [-aBbcDdeFfHhIiLlOosvXx] [-A base] [-j skip] [-N length] [-t type]
[[+]offset[.][Bb]] [file ...]
byte level, and to test various interpretations of the data it DESCRIPTION
contains. One of these is 'od' (for 'octal debug'). Part of The od utility is a filter which displays the specified files, or stan-
dard input if no files are specified, in a user specified format.
its manual entry is shown on the right.
The options are as follows:

Here is the dialogue when 'saw.wav', an audio file, is listed -A base Specify the input address base. Base may be one of d, o, x
or n, which specify decimal, octal, hexadecimal addresses or
with od set to display ASCII characters (when it can): no address, respectively.

eseimac1:~ timjdennis$ od -A d -c saw.wav | more -a Output named characters. Equivalent to -t a.

-B, -o Output octal shorts. Equivalent to -t o2.


0000000 R \0 \0 \0 W A V E m f
t
0000016 I \0 002 \0 D 254 \0 \0 020 261 002 \0 -b Output octal bytes. Equivalent to -t o1.
0000032 \0 d a t a 240 352 032 \0 \0 \0 \0 \0
0000048 \0F \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 -c Output C-style escaped characters. Equivalent to -t c.
*
0004096 \0 \0F \0 \0 \0 \0 \0 \0 \0 \0 001 \0 001 \0 -D Output unsigned decimal ints. Equivalent to -t u4.
0004112 004 020 \0\0001 \0 001 \0 001 \0 001 \0 001 \0 001 \0
* \0 \0 -e, -F Output double-precision floating point numbers. Equivalent
0004144 001 \0 001 \0\0001 \0 001 \0 \0 \0 \0 \0 \0 \0 \0 \0 to -t fD.
0004160 \0\0 001\0
001 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0 \0
\0 020
002\0 -f Output single-precision floating point numbers. Equivalent
\0\0 \0
\0 001 to -t fF.
The parts in bold contain some readable characters and
-H, -X Output hexadecimal ints. Equivalent to -t x4.
confirm that it is what the filename implies. It is nearly
-h, -x Output hexadecimal shorts. Equivalent to -t x2.
always the case that the first part of a file contains
information in its format. An application that wants to -I, -L, -l Output signed decimal longs. Equivalent to -t dL.

play back an audio file needs to know, for example, what -i Output signed decimal ints. Equivalent to -t dI.

the sampling rate is, how many channels there are, and -j skip Skip skip bytes of the combined input before dumping. The
so on. Examining the 'official' format description would number may be followed by one of b, k or m which specify the
units of the number as blocks (512 bytes), kilobytes and
enable anyone to write a program to interpret the data megabytes, respectively.

correctly, and for example do some signal processing, -N length Dump at most length bytes of input.

then output the modified file using the same format. -O Output octal ints. Equivalent to -t o4.

-s Output signed decimal shorts. Equivalent to -t d2.

School of Computer Science and Electronic Engineering University of Essex


N. Thomos CE162 Digital Electronic Systems 1.32

Here is another listing, this time 8192 bytes into the saw.wav file, and well inside the 'sound' part, now interpreting the
bytes as 'signed short integers' (i.e. 16 bits).

The data (the sequence runs left to right along each row) seem to be periodic in that the sign changes at regular
intervals, but the values still look suspicious. Why? (The leftmost column gives the byte number within the file.)
eseimac1:~ timjdennis$ od -A d -j 8k -N 512 -s GEOPHYSICS/SOUND/saw.wav
0008192 256 256 256 256 0 0 0 0
0008208 -1 -1 -1 -1 -257 -257 -257 -257
0008224 -257 -257 -513 -513 -513 -513 -769 -769
0008240 -769 -769 -1025 -1025 -1025 -1025 -1025 -1025
0008256 -1281 -1281 -1281 -1281 -1281 -1281 -1537 -1537
0008272 -1537 -1537 -1537 -1537 -1793 -1793 -1793 -1793
0008288 -1793 -1793 -1793 -1793 -1793 -1793 -1793 -1793
0008304 -2049 -2049 -2049 -2049 -2049 -2049 -2049 -2049
0008320 -2049 -2049 -2049 -2049 -1793 -1793 -1793 -1793
0008336 -1793 -1793 -1793 -1793 -1793 -1793 -1793 -1793
0008352 -1537 -1537 -1537 -1537 -1537 -1537 -1537 -1537
0008368 -1281 -1281 -1281 -1281 -1281 -1281 -1025 -1025
0008384 -1025 -1025 -769 -769 -769 -769 -513 -513
0008400 -513 -513 -257 -257 -257 -257 -1 -1
0008416 -1 -1 0 0 0 0 256 256
0008432 256 256 512 512 512 512 768 768
0008448 768 768 1024 1024 1024 1024 1280 1280
0008464 1280 1280 1536 1536 1536 1536 1536 1536
0008480 1792 1792 1792 1792 1792 1792 2048 2048
0008496 2048 2048 2048 2048 2304 2304 2304 2304
0008512 2304 2304 2304 2304 2304 2304 2304 2304
0008528 2560 2560 2560 2560 2560 2560 2560 2560
0008544 2560 2560 2304 2304 2304 2304 2304 2304
0008560 2304 2304 2304 2304 2304 2304 2048 2048
0008576 2048 2048 2048 2048 1792 1792 1792 1792
0008592 1792 1792 1536 1536 1536 1536 1280 1280
0008608 1280 1280 1024 1024 1024 1024 768 768
0008624 768 768 512 512 512 512 256 256
0008640 256 256 0 0 -1 -1 -1 -1
0008656 -257 -257 -257 -257 -513 -513 -513 -513
0008672 -769 -769 -1025 -1025 -1025 -1025 -1281 -1281
0008688 -1281 -1281 -1537 -1537 -1537 -1537 -1537 -1537

School of Computer Science and Electronic Engineering University of Essex


N. Thomos CE162 Digital Electronic Systems 1.33

The answer depends on the actual (hardware) processor Here is the same part of the file, but interpreted by another
that is running the machine’s operating system. In this listing program that swaps pairs of bytes. Now the values
case, used to construct this example, it is an Apple iMac make more sense.
running OS X 10.4, but more crucially, the processor is a 8192 1 8244 -5 8296 -8 8348 -8
Motorola Power PC G5. This uses the 'Big Endian' internal 8194 1 8246 -5 8298 -8 8350 -8
byte order, whereas Intel processors used by Microsoft 8196 1 8248 -5 8300 -8 8352 -7
Windows machines (and later versions of the iMac) use 8198 1 8250 -5 8302 -8 8354 -7
Little Endian, as mentioned previously. 8200 0 8252 -5 8304 -9 8356 -7
8202 0 8254 -5 8306 -9 8358 -7
On the PPC, the byte order in a file or memory for a 32-bit 8204 0 8256 -6 8308 -9 8360 -7
integer is the most significant byte first and the least 8206 0 8258 -6 8310 -9 8362 -7
significant byte last. Intel machines are the other way 8208 -1 8260 -6 8312 -9 8364 -7
around, i.e., the least significant byte comes first. 8210 -1 8262 -6 8314 -9 8366 -7
8212 -1 8264 -6 8316 -9 8368 -6
Hence, the 32-bit hexadecimal number 0102030416 would 8214 -1 8266 -6 8318 -9 8370 -6
appear in the file system of a PPC-based machine as the 8216 -2 8268 -7 8320 -9 8372 -6
sequence of four hex bytes: 8218 -2 8270 -7 8322 -9 8374 -6
8220 -2 8272 -7 8324 -9 8376 -6
01 02 03 04 8222 -2 8274 -7 8326 -9 8378 -6
8224 -2 8276 -7 8328 -8 8380 -5
On Intel-based machines (Windows, and Macs using the 8226 -2 8278 -7 8330 -8 8382 -5
Intel Core Duo processor) the storage format would be: 8228 -3 8280 -8 8332 -8 8384 -5
8230 -3 8282 -8 8334 -8 8386 -5
04 03 02 01
8232 -3 8284 -8 8336 -8 8388 -4
8234 -3 8286 -8 8338 -8 8390 -4
Clearly, if the byte order of data from a file is wrongly
8236 -4 8288 -8 8340 -8 8392 -4
interpreted, it potentially makes a very big difference to the
8238 -4 8290 -8 8342 -8 8394 -4
numerical values obtained.
8240 -4 8292 -8 8344 -8 8396 -3
8242 -4 8294 -8 8346 -8 8398 -3
School of Computer Science and Electronic Engineering University of Essex
N. Thomos CE162 Digital Electronic Systems 1.34

Conversion routines
main(argc, argv)
int argc;
1. ASCII to Binary char **argv;
{
unsigned char c, *cp;
Within a processor, numerical data are represented in int value;
binary but have to be entered or displayed using the ASCII int base, digit_value,
code. This C program for Unix takes its arguments, cnt, index;
/*
converts them from ASCII strings to binary, and then Convert argument list
outputs them again in octal, decimal, and hexadecimal. It from ascii to integer.
recognises the standard '0...' and '0x...' for octal and Accept octal (start with 0, 0-7), hex
hexadecimal and assumes decimal otherwise. (start with 0x, 0-9, A-F) or decimal, 0-9.
*/
cnt = argc - 1;
Conversion stops when a character is read that is NOT a index = 1; while
valid digit in the current base. (cnt—){
cp = *++argv; /* pointer to arg. string */ if
(cp[0] == ‘0’){
The central part of the algorithm, in 'pseudocode', is: if (cp[1] == ‘x’) {
base = 16;
value = 0; cp += 2;
}
loop { else base = 8;
get_character_f }
rom_input; else value
base == 10;
0;
if (character is valid in current base){ while(1){
c =
*cp++;
value = value * base; if
convert_character_to_digit_value; (base
== 8){
value = value + digit_value; i
} f
else exit_loop; (
} !
(
(
School of Computer Science and Electronic Engineering University of Essex
c
>
N. Thomos CE162 Digital Electronic Systems 1.35

if (!(((c >= ‘0’) && (c <= ‘9’)) ||


((c >= ‘A’) && (c <= ‘F’)))) break;
}

if (base == 16){
if ((c >= ‘A’) && (c <= ‘F’)) digit_value = c - ‘A’ + 10;
else digit_value = c - ‘0’;
}
else digit_value = c - ‘0’;
value = value * base digit_value;
+
printf
} (“\nArgument %d value: 0%o, %d, 0x%x\n”, index, value, value, value);
index++;
}
exit(0);
}

Run the program:

[tim2@james DEMO_PROGS]$ asc_conv 077777 12345678 0x3FFFFF

Argument 1 value: 077777, 32767, 0x7fff

Argument 2 value: 057060516, 12345678, 0xbc614e

Argument 3 value: 017777777, 4194303, 0x3fffff

[tim2@james DEMO_PROGS]$

School of Computer Science and Electronic Engineering University of Essex


N. Thomos CE162 Digital Electronic Systems 1.36

Conversion routines Note that in C '%' is the 'remainder' or 'modulo' operator,


i.e. the operation
2. Binary to ASCII rem = a % b;
The simplest method to convert binary numbers to ASCII
makes rem equal to the reminder when a is divided by b
is to adapt the base conversion algorithm discussed
using integer division.
previously (without loss of generality we consider base
10). A slight disadvantage of this approach is that the
digits are generated in the reverse order from which they Exercises
are likely to be printed or displayed.
1. How can the printing order reversal in the binary to
In C, the relevant part of the algorithm might be the one ASCII conversion process opposite be overcome?
illustrated below, where at the start, the integer number is
in variable to_convert and the ASCII code for the 2. How would you deal with negative integers?
corresponding characters will be in asc_char. 3. Integer division is potentially a source of considerable
int to_convert; rounding error. For example, 59/10 produces 5, when the
more accurate integer approximation to the exact value
char asc_digit;
5.9 would be 6. How can this be overcome for both positive
and negative integers in programs using integer
while (to_convert > 0){ division?
digit = to_convert % 10;
to_convert = to_convert /
10;
asc_digit = digit + '0'; /* Why does this
work ? */
print_asc_digit(asc_digit);
}

School of Computer Science and Electronic Engineering University of Essex


N. Thomos CE162 Digital Electronic Systems 1.37

Floating Point Numbers We could write this:

Computer word lengths are finite, which means that the 299792500 m.s-1,
range of numerical quantities that can be represented
exactly is also finite and makes handling very large or very but because of the measurement error, the last digit is
small numbers difficult. It is possible to do arithmetic on effectively 'noise' and hence adds no useful information;
words of any length by combining the units provided thus, it is best omitted. For most practical purposes, this
(e.g., the 'long long' integer type in C), but is the precision many digits of precision are unnecessary, and it's common
this gives always necessary? to see the speed of light given as 'approximately
3 x 108 m.s-1'.
We already have a way of dealing with this problem in
everyday applications, using floating point, also known as A form of this notation often used in scientific programming
'scientific' representation, which is to specify a small would give the number representing the speed of light as:
decimal number (integer part ideally in the range 1 to 9)
followed by a multiplier in the form of a positive or negative 2.9979250E8
integer power of ten. For example, the rest mass of the
electron from a table of physical constants is listed as: and the mass of the electron:

9.109558 x 10-31 kg. 9.109558E-31

The likely error in this measurement is given as 6 parts per


million (ppm), which would affect the last two digits of the The two parts of a floating point number are the mantissa
fraction. It, therefore, makes no sense to provide more than (from Latin, meaning 'make weight') and exponent.
this number of digits of precision.
Scientific programming languages allow users to specify
Similarly, the speed of light is given as: exactly how a number will appear on output listings, e.g.
integer, fixed or floating point, and how many digits (the
2.9979250 x 108 m.s-1, error 0.33 ppm. precision) are to be used.

School of Computer Science and Electronic Engineering University of Essex


N. Thomos CE162 Digital Electronic Systems 1.38

Decimal Floating Point arithmetic Addition and subtraction are less straightforward
because the numbers have to be denormalised so that
both have the same exponent before the mantissas can
Multiplication
be processed. This is also called 'aligning the decimal
point'.
Example:
E.g. for addition:
(9x105) x (2x108)
(9x105) + (2x108)
= (9 x 2) x (10 x 10 ) 5 8

= 0.009 x 108 + 2 x 108


= 18 x 10(5+8) = 18 x 1013 = 1.8 x 1014
= 2.009 x 108.
i.e., the rule is to add the exponents and multiply the
mantissas. Renormalise as necessary to get one digit to
This process gives a useful insight into precision and
the left of the decimal point in the mantissa.
accuracy: when the numbers are so different that they
can no longer be aligned within the available precision,
Division then the calculation has no effect, the result being equal
to the component with the largest magnitude.
(9x105) / (2x108)

= (9/2) x (105/108)
= 4.5 x 105 x 10-8
= 4.5 x 10(5-8)
= 4.5 x 10-3

School of Computer Science and Electronic Engineering University of Essex


N. Thomos CE162 Digital Electronic Systems 1.39

Binary Floating Point representation

There is no 'natural' way of doing this; as a result a number of methods exist. A well-known recommended standard is
that defined by ANSI/IEEE 754-1985.

(details taken from http://www.psc.edu/general/software/packages/ieee/ieee.html)

This has two main versions, single in 32 bits = 4 bytes, and double precision, which uses 64 bits = 8 bytes.

The bits in the IEEE single precision 32-bit floating point standard representation may be numbered from 0 to 31, left to
right*. The first bit is the sign bit, S, the next eight bits are the exponent bits, 'E', and the final 23 bits are the fraction 'F':

S EEEEEEEE FFFFFFFFFFFFFFFFFFFFFFF
0 1 8 9
31

The value V represented by the word may be determined as follows:

If E=255 and F is nonzero, then V = NaN ("Not a number")


If E=255 and F is zero and S is 1, then V = -Infinity
If E=255 and F is zero and S is 0, then V = Infinity

If 0<E<255 then V= (-1)S x 2(E-127) x (1.F) where "1.F" is intended to represent the binary number created by
prefixing F with an implicit leading 1 and a binary point.

If E=0 and F is nonzero, then V = (-1)S x 2(-126) x (0.F). These are "unnormalized" values.
If E=0 and F is zero and S is 1, then V = -0
If E=0 and F is zero and S is 0, then V = 0

*This
School of can beScience
Computer confusing,
and Electronicgiven thatUniversity
Engineering it's moreof Essex usual to number bits right to left, reflecting their 'weight'

within a number.
N. Thomos CE162 Digital Electronic Systems 1.40

Examples
0 00000000 00000000000000000000000 = 0
1 00000000 00000000000000000000000 = -0
0 11111111 00000000000000000000000 = Infinity
1 11111111 00000000000000000000000 = -Infinity

0 11111111 00000100000000000000000 = NaN


1 11111111 00100010001001010101010 = NaN
0 10000000 00000000000000000000000 = +1 x x 1.02 = 2
2(128-127)
0 10000001 10100000000000000000000 = +1 x (129-127) x 1.1012 = 6.5
2
1 10000001 10100000000000000000000 = -1 x (129-127) x 1.101 = -6.5
2 2

0 00000001 00000000000000000000000 = +1 x x 1.0 2 = 2(-126)


2(1-127)
0 00000000 10000000000000000000000 = +1 x x 0.12 =
0 00000000 00000000000000000000001 = +1 x 2(-126) x 0.00000000000000000000001
2(-127) 2
=
2(-149) (Smallest positive value)
2(-126)
Except for the special case of zero exponent, E is encoded as 'excess 127', that is 127 is assumed added to its value,
and must be subtracted before the exponent value is used in any calculation.

Note that for most of the range, the precision of the mantissa is actually 24 bits, even though it is allocated 23 bits in the
standard. This is because the fractional part is always of the form 1.xxxxx..., so the leading '1' becomes redundant and
can be left out, but must be restored when calculations are actually performed.

Floating point arithmetic can be done in software, in which case it is very much slower than integer. More usually,
specialised hardware is provided and the speed disadvantage may be slight or non-existent.

School of Computer Science and Electronic Engineering University of Essex


N. Thomos CE162 Digital Electronic Systems 1.41

Examples 2. Convert
0 1000 0000 1001 0010 0001 1111 1011 011
1. Convert -1.5 to single precision floating point to decimal.
representation.
The exponent is 128 - 127 = +1.
1.5 = 1 + 2-1 = 1.1
2
The mantissa is:
Hence the exponent is 0. Excess 127 representation is 1.1001 0010 0001 1111 1011 0112
used in IEEE 754, so we add 127, hence E = 0111 1111
= 1100 1001 0000 1111 1101 1011 x -23
2
The mantissa is 1.1 2, but the integer part is 'hidden', so F = C90FDB16 x 2 -23
becomes: = 131769510 x 2 -23= 1.57079637110

1000 0000 0000 0000 0000 000 The exponent is +1, so the number is finally:

The sign bit, S, is 1 since the number is negative, so the 1.57079637110 x 21 = 3.141592741
full result is:
Looks a bit like , but the last 3 digits are wrong!
1 0111 1111 1000 0000 0000 0000 0000
000

Exercises

1 Convert to 32 bit binary floating point:


10.1 10

2 Convert to decimal fixed point:


1 1000 0101 1001 0011 1111 0000 0000 000

School of Computer Science and Electronic Engineering University of Essex


N. Thomos CE162 Digital Electronic Systems 1.42

A warning on Accuracy getbits(dst, src, size)


void *dst, *src;
int size;
{
This trivial C program computes the value 1000000 using int cnt;
single precision floating point variables in two different unsigned char *sp, *dp; sp
= src; dp = dst;
ways, the first by adding 0.1 to itself 107 times, the second while(cnt--) *dp++ = *sp++;
by multiplying 0.1 by 107. The results are very different. }

Why?
Run the program:
#include <stdio.h>
main() eseimac1:~/DEMO_PROGS timjdennis$ rounding_error
{ sizeof float: 4, double: 8, long long: 8
float sum, inc; Calculating 10^6, 'float' variables
int cnt;
unsigned long Bit pattern, float: 0x3dcccccd00000000
long ltmp = 0;
Repeated addition. sum = 1087937.000000000
printf ("sizeof float: %d, double: %d, long long: %d\n",
sizeof(float), sizeof(double), sizeof(long long)); Multiplication. sum = 1000000.000000000

printf ("Calculating 10^6, 'float' variables\n\n");


Note how the program represents 0.1. Decoding the IEEE
sum = 0.0; format 32 bit value 0x3dcccccd generates:
cnt = 10000000;
inc = 0.1;
-4
1.100 1100 1100 1100 1100 11012 x 2
getbits(&ltmp, &inc, sizeof(float));
printf ("Bit pattern, float: 0x%llx\n", ltmp);
(check this yourself, and compare with page 1.8). The
while (cnt--) sum = sum + inc;
printf ("Repeated addition. sum = %15.9f\n", sum); final (least significant) '1' is a 'rounding-up' of the next two
bits which should be a continuation of the 1100... pattern.
inc = 10000000.0; However, this does not seem sufficient to account for the
sum = 0.1;
very large error which most likely comes from the
sum = sum * inc; denormalisation process: remember that the numbers
printf ("Multiplication. sum = %15.9f\n", sum);
effectively both have to be represented in fixed point
exit(0); format to do addition, and the binary points aligned, which
}
may not be possible when their magnitudes are very
different.
School of Computer Science and Electronic Engineering University of Essex
N. Thomos CE162 Digital Electronic Systems 1.43

The error does occur with double precision floating point variables, but would with proportionately smaller/larger
values.

(In 'double' variables, 0.1 becomes: 0x3fb999999999999a


which is: 0 011 1111 1011 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 10102
Note the same fundamental bit pattern, with rounding-up at the end.)

General advice in programs where a variable is needed that changes by a large number of very small steps is NOT to
use code like this:

float inc, stepping_value;


int loop_control;

inc = very_small_number;
loop_control = 0;
stepping_value = 0.0;
while (loop_control < large_number)
{ stepping_value = stepping_value +
inc; loop_control = loop_control + 1;
...(rest of loop)
}

Instead do it this way:


inc = very_small_number;
loop_control = 0;
while (loop_control < large_number)
{ stepping_value = (float)loop_control *
inc; loop_control = loop_control + 1;
...(rest of loop)
}

This version avoids the rounding error


accumulation of the example above.

Single precision numbers should be avoided; they are really only useful for storing raw data, or processed final data.
Use double precision format (which uses 8 bytes/64 bits) within programs.
School of Computer Science and Electronic Engineering University of Essex
N. Thomos CE162 Digital Electronic Systems 1.44

Summary
In this section we have covered:

• Number systems, and conversions between different bases: binary, octal, decimal
and hexadecimal.
• Binary fractions
• Binary arithmetic
• Use of Two's Complement for negative numbers; two's complement arithmetic.
• Data representation in computer systems: 'text' and 'data' files.
• The ASCII code. Serial transmission of binary data on the RS232 standard.
• Floating point binary number representation and floating point arithmetic.

School of Computer Science and Electronic Engineering University of Essex

You might also like