You are on page 1of 61

ASE2207: DIGITAL ELECTRONIC INSTRUMENTATION

SYSTEMS

Lecture 2: Number Systems and Data


Conversion

Jan 2024
Lecture 2 Overview
• Positional number system:
• Binary, decimal, hexadecimal
• Unsigned, signed, 1’s complement, 2’s complement
• Arithmetic operations (+, -, ×, ÷)
̶ Carry, sign extension, overflow
̶ Fractional representation and floating point
• Digital codes:
̶ ASCII, Gray code, BCD
• Parallel versus serial data
• Error detection and correction codes
̶ Parity, CRC, Hamming
• Data conversion
̶ Analog to digital conversions, DAC, ADC, and conversion limitations

• Important notes
̶ Please go thru the videos on the next page prior to the lecture and raise questions if
you have any queries.

2
Self-learning videos
• Binary, Decimal, Hexadecimal number systems
– https://www.youtube.com/watch?v=LpuPe81bc2w, 5’19”
– https://www.youtube.com/watch?v=ry1hpm1GXVI. 10’33”

• Negative, Signed, Two’s complement number


– https://www.youtube.com/watch?v=FamrrDi6RL0, 15’47”
– https://www.youtube.com/watch?v=9W67I2zzAfo, 9’11”

• Conversion of poly-radix number systems


– https://www.youtube.com/watch?v=Fpm-E5v6ddc, 7’52”
– https://www.youtube.com/watch?v=aW3qCcH6Dao&t=674s, 16”46”

• Arithmetic (+, -, /, *) operations in binary systems


– https://www.youtube.com/watch?v=sFd5bnDdB3Q, 5’11”
– https://www.youtube.com/watch?v=S9LJknZTyos, 18’09”

• Number representation in fix/floating point


– https://www.youtube.com/watch?v=PZRI1IfStY0, 9’15”
– https://www.youtube.com/watch?v=n-XozGu1viM, 4’21”

• Do your search online for other relevant topics


3
Calculator and Mobile apps

• An approved calculator (non-graphics) for coursework, lab,


quiz and exam
• Some useful Android mobile apps to aid your learning (cannot
be used for quiz and exam)
4
Introduction to Number Systems
• Numbering is one of the most important
concepts in computer system
• All computer deals with numbers in one
form or another (e.g. mp3, image,
bioinformatics, DNA, weather prediction)

• The decimal system used in our everyday


life cannot be efficiently represented in
computer
• Binary numbers is usually used
– Instructions, input data and operations carried out
in 0s and 1s
• Need to also perform operations on
real/floating numbers (+, -, ÷, × precision)

5
Positional Number Systems
• The decimal number system we use is a radix/base 10 positional system
– each digit position has an associated weight:
– e.g. 3706.5910 = 3000 + 700 + 00 + 6 + 0.5 + 0.09
= 3×103 + 7×102 + 0×101 + 6×100 + 5×10-1 + 9×10-2
Thousands hundreds tens unit

– the weight of each digit is a power of 10 for decimal number, increasing to the left and allows
for negative powers.

• In general, a decimal number of the form “d3d2d1d0.d-1d-2” in base 10 positional


system has the value:
– 𝑑𝑑3 × 103 + 𝑑𝑑2 × 102 + 𝑑𝑑1 × 101 + 𝑑𝑑0 × 100 + 𝑑𝑑−1 × 10−1 + 𝑑𝑑−2 × 10−2

• Similarly, a binary number of the form “b3b2b1b0.b-1b-2” in base 2 positional system


has the value:
– 𝑏𝑏3 × 23 + 𝑏𝑏2 × 22 + 𝑏𝑏1 × 21 + 𝑏𝑏0 × 20 + 𝑏𝑏−1 × 2−1 + 𝑏𝑏−2 × 2−2

• We usually add a subscript at the end to indicate the number representation used, for
example, a decimal number with base 10 as 21310, and a binary number with base 2 as
11012.
6
Positional Number Systems
• In general, radix r number system with p digits to the left of the
𝑝𝑝−1
radix point and n digits to the right: D = ∑𝑖𝑖=−𝑛𝑛 𝑑𝑑𝑖𝑖 . 𝑟𝑟 𝑖𝑖

• For example, D = 213.4510, then p=3 and n=2, D = ∑3−1 𝑖𝑖


𝑖𝑖=−2 𝑑𝑑𝑖𝑖 . 10 , hence d2=2,
d1=1, d0=3, d-1=4, d-2=5, D=2×102 + 1×101 + 3×100 + 4×10-1 + 5×10-2

• Digital circuits can only represent two values: on and off, high and low, or 1
and 0.

• Numbers are represented by binary digits (bits) which can have a value of
either 1 or 0.

• The leftmost bit is the most significant bit (MSB) and rightmost bit is the least
significant bit (LSB).
• A binary number: 𝑏𝑏𝑝𝑝−1 𝑏𝑏𝑝𝑝−2 … 𝑏𝑏1 𝑏𝑏0 . 𝑏𝑏−1 𝑏𝑏−2 … 𝑏𝑏−𝑛𝑛
𝑝𝑝−1
has the value: 𝐴𝐴 = ∑𝑖𝑖=−𝑛𝑛 𝑏𝑏𝑖𝑖 . 2𝑖𝑖 MSB LSB

• For example, an 8-bit binary number 1100 0101


7
Positional Number Systems: Number conversion

• Example of conversion of binary to decimal:


– 11012 = 1×23 + 1×22 + 0×21 + 1×20 = 1×8 + 1×4 + 0×2 + 1×1 = 1310

– 1001 11002 = 1×128 + 0×64 + 0×32 + 1×16 + 1×8 + 1×4 + 0×2 + 0×1 = 15610

– 1101.0112 = 1×8 + 1×4 + 0×2 + 1×1 + 0×0.5 + 1×0.25 + 1×0.125 = 13.37510

• An n-bit unsigned binary number can represent values from 0 to +(2n-1)


– e.g. an 4-bit binary number can represent values from 0 to 15 [0 to +(24-1)]

– Any larger numbers (e.g. 16, 17) cannot be represented they are termed as overflow

• Some common powers of 2 used in the digital world:

(MSB) 210 29 28 27 26 25 24 23 22 21 (LSB) 20


1024 512 256 128 64 32 16 8 4 2 1

8
8-bit
Positional Number Systems: Number conversion

• To convert from decimal to binary, use this iterative process as


outlined in the flowchart below.
• In most cases, you can use the calculator to aid with these conversions
Examples:
Start
11310 ÷ 2 = 56 remainder 1 (LSB)
Divide by 2 5610 ÷ 2 = 28 remainder 0
2810 ÷ 2 = 14 remainder 0
Record quotient (Q)
and remainder R 1410 ÷ 2 = 7 remainder 0
710 ÷2 = 3 remainder 1
Is No
Q=0? 310 ÷2 = 1 remainder 1
Yes
Collect R’s into desired
110 ÷2 = 0 remainder 1 (MSB)
binary number with first R
as LSB and last R as MSB 11310 = 111 00012
End
MSB LSB 9
Positional Number Systems: HEX representation

• Binary digits (bits) for large numbers are long and


Binary Decimal Hex
tedious to write and remember 0000 0 0
– Hexadecimal representation is usually used 0001 1 1
0010 2 2
• Hexadecimal is base 16,
0011 3 3
– Symbol used is 0, 1, 2, 3, …, 8, 9, A, B, C, D, E, F. 0100 4 4
• One hex digit equals four binary digits (since 24 = 16). 0101 5 5
0110 6 6
• For conversion, binary digits are arranged in groups of 4: 0111 7 7
– B9F16 = 1011 1001 11112 = 11×162 + 9×161 + 15×160 1000 8 8
1001 9 9
= 297510
1010 10 A
• Hex representation is widely used for memory address 1011 11 B
and data (e.g. 32-bit address as H’FE23 9AD4). 1100 12 C
1101 13 D
• Some examples:
1110 14 E
– 12 = 110 = 116, 910 = 916 1111 15 F
10
– 112 ≠ 1110 ≠ 1116, 2510 ≠ 2516
Positional Number Systems: OCT representation

• Binary digits (bits) for large numbers are long and


Binary Decimal OCT
tedious to write and remember 0 0 02 010 08
– Octal representation is an alternative where 3 binary bits 0 0 12 110 18
are group together 0 1 02 210 28
0 1 12 310 38
• Octal is radix or base 8
1 0 02 410 48
– Symbol used is 0, 1, 2, 3, …, 6, 7 1 0 12 510 58
– There is no 8 and 9 in octal 1 1 02 610 68
1 1 12 710 78
• One OCT digit equals 3 binary digits (since 23 = 8).
1 0 0 02 810 108
• For conversion, binary digits are arranged in groups of 3: 1 0 0 12 910 118

– 7258 = 111 010 0112 = 7×82 + 2×81 + 5×80


= 46910

11
Positional Number Systems: Sign-magnitude representation

• In decimal number system, a negative number is denoted by a negation symbol (-).


– But computer only understands 1’s and 0’s!
– The unsigned representation only allows positive number to be represented
– The standard binary representation is hence an unsigned representation
• With only 1’s and 0’s representation, scientist were figuring out how to incorporate a
sign to represent real numbers: numbers that are either positive or negative.
• Many representations were proposed, but we will study 3 signed representations:
– Sign-magnitude, 1’ s complement, 2’s complement (default representation)
• Do note the term “signed number” is a generic term to indicate there are both positive
and negative numbers, sign numbers does not implies sign-magnitude representation.
• Sign-magnitude representation
– Represent binary numbers by adding a sign bit to the MSB, where 0 indicates positive and 1
indicates negative.
– Given 1-bit is taken up by the sign bit, (n-1)-bit is left to represent the magnitude (value).

0 101 01112 = + 8710 1 011 00012 = – 4910 0 000 00002 = + 010


1 111 11112 = – 12710 0 011 00012 = + 4910 1 000 00002 = – 010
12
Positional Number Systems: Sign-magnitude representation

• An n-bit sign-magnitude binary number can n-bit


represent values from –(2n-1-1) to +(2n-1-1)
S Magnitude
• Given an 8-bit binary number only has 256
permutations, 7-bit is left to represent the (n-1)-bit
values, hence it can represent -127 to +127 S: sign-bit
0: Positive, 1: Negative
• Equal number of positive and negative values.
• Two representations of zero: +0 and –0.
• Adders/subtractors for sign-magnitude are
complex as they need to have different
operations based on the sign bit.
• Hence sign-magnitude is not widely used.

13
Positional Number Systems: 1’s complement representation

• The 1’s complement of a binary number is the inverse of the digits:


– Change all 0’s to 1’s and all 1’s to 0’s
– This changes the sign of the number from +ve to –ve or vice-versa
– For example, the 1’s complement of 1100 10102 (-5310) is 0011 01012 (+5310)
• It retains the same feature as sign-magnitude numbers, MSB=0 for positive numbers,
MSB=1 for negative numbers.
– +110 = 00012, -110 = 11102
– +710 = 01112, -710 = 10002
• To convert from 1’s complement binary to decimal, always look at the MSB of the binary
representation. If the MSB is 1 (meaning it is a negative number), invert the number first
before obtaining the decimal equivalent. Remember to add in the negative sign.

• Due to complexity of arithmetic operations, 1’s 1 1 0 0 1 0 1 0


complement representation is seldom used.
• 1’s complement in digital circuit is formed by using
inverters (a basic logic gate to be covered in the next
0 0 1 1 0 1 0 1
chapter):
14
Positional Number Systems: 2’s complement representation

• A complement system overcomes some of the issues associated


with sign-magnitude and 1’s complement
• The 2’s complement of a binary number is obtained by adding 1
to the LSB of the 1’s complement.
• 2’s complement numbers exhibit the same characteristics,
MSB=0 for positive numbers, MSB=1 for negative numbers.
• For binary system, the 2’s complement scheme is as follows:
– To negate a number (change from positive to negative or vice-versa), invert
every bit and add 1.
– Given a 2’s complement binary number 1101 1000, what is the decimal
value?
– From the MSB, you will note that this is a negative number. To obtain the
actual value, you will have to invert (0010 0111) and add 1 (0010 0111 + 1 =
0010 1000 = 4010). Hence, 1101 1000 = -4010 15
Positional Number Systems: 2’s complement representation

• To derive the 2’s complement number in binary for negative numbers, you have to obtain
the binary representation for the positive number, then perform invert follow by plus 1.

• Likewise, if you notice a 2’s complement number with MSB=1 (meaning this is a
negative number), you can perform invert follow by plus 1.

Recall that the 1’s complement of 0011 0110 is 1100 1001 (1’s complement)
To form the 2’s complement, add 1 +1
0 0 1 1 0 1 1 0
1100 1010 (2’s complement)

1
2’s complement
1 1 0 0 1 0 0 1
Input bits
Carry 0011 01102 = +5410
Adder
Adder
in (add 1)
Output bits (sum)
1100 10102 = -5410
1 1 0 0 1 0 1 0
16
Positional Number Systems: 2’s complement representation

• In a two’s complement representation, any


number with an MSB of 1 is negative.

• An n-bit two’s complement binary number can


represent values from –2n-1 to +(2n-1-1)

• Two’s complement is used by the vast majority


of digital systems for signed arithmetic.

• Other representations, e.g. 1’s complement and


excess representation exist but are inefficient to
implement.

17
Positional Number Systems: 2’s complement representation

• What makes two’s complement so preferred is that addition and


subtraction can be performed using the same circuit.
– E.g. 6910 – 1210 = 0100 0101 – 0000 1100 = 0011 1001 = 5710
6910 + (-1210) = 0100 0101 + 1111 0100 = 0011 1001 = 5710
– E.g. 1210 – 6910 = 0000 1100 – 0100 0101 = 1100 0111 = –5710
1210 + (-6910) = 0000 1100 + 1011 1011 = 1100 0111 = –5710
• Two’s complement also has only one zero representation (00002 is
not the same as 10002: 00002 = 010 while 10002 = -810).
• Negation is easy: invert every bits and add 1.
• This is the default number representation used in this module,
unless otherwise stated.
• Similarly, the default number system used is decimal. For
example, what is 1001?
18
Positional Number Systems: Representation Summary

• Positive numbers have the same representations while it is not


the case for negative numbers:
Number Sign-mag 1’s comp 2’s comp
+7 0000 01112 0000 01112 0000 01112
-7 1000 01112 1111 10002 1111 10002
• To find the magnitude of a Negative Number in 1’s Complement
Representation:
– Invert all the bits: e.g. 1111 1000
– After inverting becomes 0000 0111 => magnitude of the negative number is 710.
Remember to add the negative sign, hence the result is -710.
• To find the magnitude of a Negative Number in 2’s Complement
Representation:
– Invert all the bits, then +1: e.g. 1111 1001
– After inverting becomes 0000 0110
– After +1 becomes 0000 0111 => magnitude of the negative number is 710. Again,
remember to add the negative sign, hence the result is -710. 19
Summary of representation: 4-bit example
Content of memory Unsigned Sign-magnitude 1’s complement 2’s complement
00002 010
00012 110
00102 210
00112 310
01002 410
01012 510
01102 610
01112 710
10002 810 -010 -710 -810
10012 910 -110 -610 -710
10102 1010 -210 -510 -610
10112 1110 -310 -410 -510
11002 1210 -410 -310 -410
11012 1310 -510 -210 -310
11102 1410 -610 -110 -210
11112 1510 -710 -010 -110
• Notice two zeroes representations in sign-magnitude and 1’s complement. 20
Positional Number Systems

Sign-magnitude & 2’s complement


Unsigned
1’s complement Default number representation
Range 0 to 2n-1 –(2n-1-1) to +(2n-1-1) –(2n-1) to +(2n-1-1)
• Cannot represent • Two zeroes representation • Unequal positive and negative
negative numbers number range
Remarks
• Range of positive • Difficult to perform arithmetic • Easy to perform arithmetic
numbers is increased operations
8-bit 0 – 255 -127 to +127 -128 to +127
16-bit 0 – 65535 -32767 to +32767 -32768 to + 32767
Decimal to binary Decimal to binary
+ve: standard conversion +ve: standard conversion
-ve: convert the positive number: -ve: convert the positive number,
- add a 1 to MSB (sign-mag) invert all bits and then add 1.
- invert all bits (1’s comp)
Binary to decimal Binary to decimal
Standard binary MSB=0: +ve, standard conversion MSB=0: +ve, standard conversion
Summary
conversion MSB=1: -ve, MSB=1: -ve, invert all bits and add 1.
- Sign-mag: invert MSB, convert Change the sign to -ve.
the decimal, add –ve sign to the
decimal number.
- 1’s comp: invert all bits, convert
to decimal, add –ve sign to the
decimal number. 21
Positional Number Systems: Binary arithmetic

Addition Subtraction
(15) 0000 1111 (21) 0001 0101
(6) + 0000 0110 (-6) + 1111 1010
(21) 0001 0101 (15) 0000 1111

Multiplication Division
Multiplicand (13) 1101 Divisor 00001101
Multiplier (13) 1101 1101 10101001
1st partial product (PP) 1101 -1101
2nd PP 0000 10000
3rd PP 1101 -1101
4 PP
th + 1101 001101
Final product (169) 1 0 1 0 1 0 0 1 -1101
Remainder 0 22
Positional Number Systems: Binary arithmetic

• As binary are base 2 numbers, you have to remember 12+12 = 102. As 1+1=2 in
decimal, when you move the carry to the more significant column on the left, you
have to divide by 2. Likewise when you have a borrow from left to right, you have
to multiply by 2.
• When performing arithmetic operations, you may encounter one or more of the
following conditions:
– Carry: In binary arithmetic, a carry is a bit that is transferred from one column of bit to the left column
of more significant bit.
– Borrow: In binary arithmetic, when a bit becomes less than zero and the deficiency is taken from the
next bit to the left.
– An overflow occurs when the result does not have the sign that one would predict from the signs of the
operands (e.g. a negative result when adding two positive numbers). Therefore, it is useful to check the
overflow flag after adding or subtracting numbers that are represented in two's complement form (i.e.
they are considered signed numbers).
• When performing arithmetic operations, you cannot simply add or remove bits. What this
meant is adding two 8-bit numbers should produce an 8-bit results. When the results is more
than the number of bits, it is consider as overflow.
• Consider a real world use case in programming where you declare two variables as integer or
int in C program, depending on the compiler, they could take on 16 or 32-bit in memory.
However, when the results of the addition is beyond the range, an incorrect result will be
produced, but it will not produce an error! Try to relate to C programming, what is the
difference between variable data type int, long, short, unsigned, etc. 23
Positional Number Systems: Sign Extension

• Sign extension is the operation, in computer arithmetic, of increasing the number of bits of a
binary number while preserving the number's sign (positive/negative) and value. This is
done by appending digits to the most significant side of the number, following a procedure
dependent on the particular signed number representation used.
• But why??? To increase the range of numbers it can represent! An 8-bit 2’s complement
number can range from -128 to +127, if we want to represent +130, we need a number with
more bits that can accommodate a larger range of numbers (e.g. 10-bit for -512 to +511)
• If we want to widen a two’s complement number, we must sign extend, i.e. duplicate the sign
bit.
– –3510 = 101 11012 (7-bit) = 1111 1101 11012 (12-bit) = –3510

– +5210 = 011 01002 (7-bit) = 0011 01002 (8-bit) = +5210


• How do you perform sign extension for unsigned, signed magnitude and 1’s complement
numbers?
• Sometimes, you may need to reduce the number of bits used; a process known as sign
truncation. How do you reduce the number of bits?

https://en.wikipedia.org/wiki/Sign_extension
24
Overflow: Out of range error
• Overflow ≠ carry https://en.wikipedia.org/wiki/Integer_overflow

• The term arithmetic overflow or simply overflow has the following meanings:
the condition that occurs when a calculation produces a result that is greater
in magnitude than that which a given register or storage location can store or
represent (i.e. out of range).
• If you want to widen (i.e. extend the range of the numbers that can be
represented) a two’s complement number, you must sign extend, i.e. duplicate
the sign bit. The impact is more memory will be required to store the extra bits.
• An example with 4-bit addition: 01112 + 01002 = 10112
Unsigned numbers 2’s comp. numbers Sign-extended numbers
1 1 1

0 1 1 1 0 1 1 1 0 0 1 1 1
+ 0 1 0 0 + 0 1 0 0 + 0 0 1 0 0
1 0 1 1 1 0 1 1 0 1 0 1 1

Result is correct Result is incorrect After sign-extension from 4


710 + 410 = 1110 710 + 410 = -510 to 5 bit, the result is correct 25
Positional Number Systems: Unsigned binary arithmetic

• 8-Bit Addition (Unsigned numbers)


1 1 1 1 1 1 1 1 1 1 1
0111 1111 (127) 1000 0111 (135)
+ 0000 0001 (1) + 1000 0001 (129)
1000 0000 (128) C 0000 1000 (8)
For unsigned numbers, carry should be
considered to produce the correct result

• 8-Bit Subtraction (Unsigned numbers)


0 1 1 1 1 1 1 2 B 1 1 1 1 1 2
1 0 0 0 0 0 0 0 (128) 0 0 0 0 0 0 1 0 (2)
- 0 0 0 0 0 0 0 1 (1) - 0 0 0 0 0 1 0 0 (4)
0 1 1 1 1 1 1 1 (127) 1 1 1 1 1 1 1 0 (254)
Note the correct result should be
C Carry flag 2 - 4 = -2
However, unsigned magnitude
B Borrow flag cannot represent negative numbers
26
Positional Number Systems: Signed binary arithmetic

• 8-Bit Addition (2’s Complement)


Sign-extended version
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1011 1111 (-65) 0111 1111 (+127) 0 0 0 0 0 1 1 1 1 1 1 1 (+127)
+ 1100 0001 (-63) + 0000 0001 (+1) + 0 0 0 0 0 0 0 0 0 0 0 1 (+1)
C 1000 0000 (-128) (-128) 0 0 0 0 1 0 0 0 0 0 0 0 (+128)
1000 0000
While there is a carry here, O Notice the result is now correct
the result is still correct. with no overflow

• 8-Bit Subtraction (2’s Complement)


0 1 1 1 1 1 1 2 B 1 1 1 1 1 2
1 0 0 0 0 0 0 0 (-128) 0 0 0 0 0 0 1 0 (+2)
- 0 0 0 0 0 0 0 1 (+1) - 0 0 0 0 0 1 0 0 (+4)
C Carry flag 0 1 1 1 1 1 1 1 (+127) 1 1 1 1 1 1 1 0 (-2)
O Overflow flag O -ve number subtract a -ve
number should give a –ve
27
results, hence overflow
Use of calculator

Refer to the video on LMS

28
Discussion

• Which representation is this binary number: 1100 10102?


• For a 2’s complement binary number: 11 11102, what is the decimal and
hexadecimal equivalent values?
• How do you represent -2510 in hexadecimal?
• Convert this hexadecimal number F2516 to binary and decimal.
• Given a 5-bit binary number 110102, what is the decimal equivalent
values consider the following representations: unsigned, sign-magnitude,
1’s complement, 2’s complement.
• Give two 4-bit binary numbers that will result in an overflow when
added together. Try to give another group of two numbers that will
produce a carry but not an overflow.
• Add two decimal numbers (-98) + (-39), consider they are represented in
(i) 8-bit and (ii) 10-bit respectively. Discuss any observations.

29
Fractional Representation

• Binary number system can also represent fractions.

• A binary point (similar to decimal point) is inserted at the required


bit position and all positions to the right of binary point are
negative powers of two.

• In general, a fractional number can be represented as n-bit whole


number (integer) with m fractional bits, e.g.:
n m
8-bits with 4 fractional bits: 1001.10012
= 1×23 + 0×22 + 0×21 + 1×20 + 1×2-1 + 0×2-2 + 0×2-3 + 1×2-4

= 1×8 + 0×4 + 0×2 + 1×1 + 1×0.5 + 0×0.25 + 0×0.125 + 1×0.0625

= 9.562510
30
Fractional Representation - Precision
• As the value of the fractional part is the sum of negative power of two,
not all numbers can be exactly represented.
• E.g.: how to represent 0.6 with 9-bit fraction?
• 0.610 ≈ 1×2-1 + 1×2-4 + 1×2-5 + 1×2-8 + 1×2-9
= 1×0.5 + 1×0.0625 + 1×0.03125 + 1×0.00390625 + 1×0.001953125
= 0.599609375 (Binary equivalent: 0.1001100112)
• Note that 0.610 cannot be represented exactly using 9-bit, hence an error could be
present when using fractional representation.
• This error is inversely proportional to the number of bits used to represent the
fractional part: 0.10012 = 0.562510 (4-bit) gives an error of 6.25% vs 0.100112 =
0.5937510 (5-bit) that gives only 1.04% error. In general, more bit usually gives a
more accurate approximation of the fractional number.
• Floating point occurs everywhere in our daily life
– Floating point is the preferred format.
– But floating point arithmetic is costly and is used only when necessary.
– Again, there are many different proposed floating point formats, we will focus on IEEE754 that is the
most commonly used format.
31
Fractional Representation - Conversion
• A decimal fraction can be converted to binary by repeatedly
multiplying the fractional results of successive multiplications by 2. The
carries form the binary number.
E.g. Convert 0.5625 to binary E.g. Convert 0.6 to binary
0.5625 x 2 = 1.125 carry = 1 MSB 0.6 x 2 = 1.2 carry = 1 MSB
0.125 x 2 = 0.25 carry = 0 0.2 x 2 = 0.4 carry = 0
0.25 x 2 = 0.5 carry = 0 0.4 x 2 = 0.8 carry = 0
0.5 x 2 = 1.0 carry = 1 0.8 x 2 = 1.6 carry = 1
0.6 x 2 = 1.2 carry = 1
Answer = .1001
Answer = .10011 (5 significant figures)
– For this example, 0.5625 can be – For this example, 0.6 cannot be exactly
exactly represented represented. You probably notice a pattern
repeating if you continue.

– The more fraction bits used, the number gets


more accurate. But this has a trade off with
memory size.
32
Floating Point Representation
• The 32-bit IEEE-754 floating point format uses a fixed
width to represent a large range of numbers.

Bias 127 32-bit (float in C)

Bias 1023 64-bit (double in C)

• Such a number has the value:


(−1) sign × 1.mantissa × 2 Exponent −127
• Exponent is biased:
– Value stored is offset from the actual value by the exponent bias.
– Single-precision number: an exponent in the range −126 .. +127 is biased by adding 127 to
get a value in the range 1 .. 254 (0 and 255 have special meanings).
– Double-precision number: an exponent in the range −1022 .. +1023 is biased by adding
1023 to get a value in the range 1 .. 2046 (0 and 2047 have special meanings).
• Mantissa (Fraction) is normalized with the leading 1.0 not stored:
– If mantissa is 10001101, the actual number represented is 1.10001101 33
Floating Point Conversion: IEEE754 to Decimal

Sign Exponent Mantissa


1 1001 0001 1000 1110 0010 0000 0000 000
• With these 32 bits, we can represent numbers as small as ± 2-126 and as large
as ±(2 - 2-23) × 2127
• The sign bit is 1.
• The biased exponent is 1001 0001 = 14510.
⁻ Actual value after subtracting bias of 127, 145-127 = 18
• Number = (-1)1 × (1.1000111000100…2) × (2145 - 127)
= (-1)1 × (1.100011100012) × (218)
= -110 0011 1000 1000 00002 = -6388016 = -40768010
• As a quick recap: xxx.xxx2 × 2n,
• If n is +ve, move binary point n-bit to the right (e.g. 110.0112 × 22 = 11001.12)
• Else (n is –ve), move binary point n-bit to the left (e.g. 110.0112 × 2-2 = 1.100112)
34
Floating Point Conversion: IEEE754 to Decimal

Sign Exponent Mantissa


0 0111 1101 1111 0000 0000 0000 0000 000

• The sign bit is 0, hence it is a positive number


• The biased exponent is 0111 11012 = 12510.
⁻ Actual value after subtracting bias of 127, 125-127 = -2
• Number = (-1)0 × (1.111100…2) × (2125 - 127)
= (-1)0 × (1.11112) × (2-2)
= 0.0111112
= 0.48437510

35
Floating Point Conversion: Decimal to IEEE754

• How to represent 0.562510 in 32-bit IEEE754 format


• The sign bit is 0 since it is a positive number.
• Binary representation 0.562510 = 0.10012 = 1.00102 × 2-1
• Refer to earlier slide on fractional representation conversion
• Mantissa = 0010 0000 0000 0000 0000 0002
• Exponent = -1 + 127 = 126 = 0111 11102

Sign Exponent Mantissa


0 0111 1110 0010 0000 0000 0000 0000 000

https://www.h-schmidt.net/FloatConverter/IEEE754.html

36
Digital codes - ASCII Table

38
Binary Coded Decimal (BCD)
• BCD is widely used and it combines features of both decimal and binary systems.
• BCD is a decimal number with each digit encoded to its four bits binary equivalent.
• The BCD value can never be greater than 9.
• BCD is a way to present decimal numbers in binary form. It is not a number system.
• The primary advantage of BCD is the relative ease of converting to and from decimal
• When performing arithmetic using BCD, when the results in invalid, an operation
known as BCD corrections is required (simply add 610 = 01102)
• . Decimal BCD Decimal BCD
0 0000 10
1 0001 11
2 0010 12 Invalid
3 0011 13 BCD
4 0100 14
5 0101 15
6 0110
87410 = 1000 0111 0100BCD 7 0111
8 1000
https://en.wikipedia.org/wiki/Binary-coded_decimal 9 1001 39
Gray code
• Gray code is an un- Rotary encoder for angle-measuring
weighted and is not devices marked in 3-bit binary. The
inner ring corresponds to Contact 1
an arithmetic code. in the table. Black sectors are "on".
Zero degrees is on the right-hand
side, with angle increasing counter-
• The important clockwise.

feature of gray code


is that it exhibits only
a single bit change
from one code word
to the next in
sequence.
• Gray code is used to
avoid problems in
systems where an
error can occur if
more than one bit
changes at a time.
40
Serial versus parallel data
• Data can be transmitted by either serial transfer or parallel
transfer.
• Serial communication
⁻ Slower since data is transferred one-bit at a time.
⁻ It supports longer distance communication since
data is send sequentially.
⁻ E.g. USB, TCP/IP, RS232, SATA

• Parallel communication
⁻ Faster since multiple bits are transferred
simultaneously.
⁻ However, it is subjected to synchronization problems and
normally used in short distance communication.
⁻ E.g. ATAPI, computer internal address/data buses
41
Error detection codes: Parity bit
• For all computer systems (e.g. communication, storage), there is always a
need to be able to identify error.
• Error detection codes are such codes that allows error to be discovered by
adding additional bits to the original data.
• One of the simplest form of such method is call the parity method.
• Any group of bits contain either an even or an odd number of 1s.
• A parity bit is attached to a group of bits to make the total number of 1s in a
group always even (even parity) or always odd (odd parity).
Even Final data for even parity 1011 0111
Data Odd Parity
Parity Final data for odd parity 1011 0110
1011 011 1 0 https://en.wikipedia.org/wiki/Parity_bit

• Cyclic redundancy check (CRC) is another commonly used code for


detecting one- and two-bit transmission errors.
• Other more advanced codes such as Hamming code can perform both error
detection and correction. 42
Analogue and Digital Data
• Analogue data are constantly varying with respect to time, in a smooth
changing voltage curve. An example of an analogue data would be flap
position.
• Voltage from the positional sensor varies as the flap position changes.
• Digital data are also varying inputs, but they vary in steps or increments.

43
Analogue and Digital Data

Quantisation levels for a simple A bipolar analogue signal


analogue to digital quantised into voltage levels
converter using a four-bit binary by sampling at regular intervals
code (note the use of the (t1, t2, t3, etc.)
two’s complement to indicate
The process of quantising an analogue
signal into its digital equivalent negative voltage levels) 44
Digital to Analog Converters (DAC)
• Digital to Analogue converters (DAC) convert a binary number
obtained from the processing made by the computer into an analogue value.
• This is the restitution of the analogue information.
• The basic principle of a D/A converter is to divide the analogue output into a
series of small steps.

DAC

8-bit 8-bit
counter DAC

CLOCK

45
Digital to Analog Converters (DAC)
• Challenges to build a resistive ladder with precise ratio in
exponential values (R, 2R, 4R, 8R, …)

Bit Voltage gain


3 (MSB) -R/R = -1
2 -R/2R = -0.5
1 -R/4R = -0.25
Alternative DAC design with only 2 types of resistors
0 (LSB) -R/8R = -0.125

B3..0 = 10102, Vout = −1 × 5 + −0.5 × 0 + −0.25 × 5 + −0.125 × 0 = −6.25𝑉𝑉


B3..0 = 11112, Vout = −1 × 5 + −0.5 × 5 + −0.25 × 5 + −0.125 × 5 = −9.375𝑉𝑉
46
Digital to Analog Converters (DAC)
• At the output of DAC, a staircase step fashion output Binary Vout
will pass through a low pass filter (LPF) to smoothen the 0000 0V
signal. 0001 -0.625 V
• The LPF is tune to a cut-off frequency (fc) to filter out 0010 -1.250 V
higher frequency signal. This cut-off frequency is based 0011 -1.875V
on the frequency of the original signal (reminder the 0100 -2.500 V
fmax when you determine the Nyquist sampling 0101 -3.125 V
frequency?) 0110 -3.750 V
0111 -4.375 V
• For a output voltage range 0 to 9.375V, a 4-bit binary
1000 -5.000 V
value will give a step size of 0.625V.
1001 -5.625 V
1010 -6.250 V
1011 -6.875 V
1100 -7.500 V
1101 -8.125 V
1110 -8.750V
LPF to smoothen the step-size DAC output Frequency response of a LPF
1111 -9.375V
47
DAC applications
Waveform generators:
• The analogue output required
from a digital computer is not
always a steady output level.
• Sometimes a particular waveform 8-bit
Binary
8-bit
R-2R
such as a sawtooth or ramp is Counter DAC
required.
• A ramp or sawtooth wave can be
easily implemented by using a
simple R-2R ladder and a counter.
Programmable gain amplifier
• DAC converters can also be used
for gain control of an analogue
signal, for example, to control the
speed of an AC motor by varying 8-bit
the input voltage level to the R-2R
DAC
motor.

48
Analog to Digital Converters (ADC)
• Digital conversions normally convert the output of analogue electrical signals
(voltage, current, etc) into a digital format used for computation, data transfer
and display.
• There are three basic techniques normally used for the conversion process.
ADC type Speed Resolution Cost
Ramp/Integration ADC Slow High Low
Successive approximation Fast Med-High Modest
Flash type ADC Fastest Low High
• Successive approximation is the most common used.
• Both the ramp generation and successive approximation type converters
require DAC as part of the circuit but these are usually built into the ADC chip.

49
Analog to Digital Converters (ADC)
• 1. Ramp/Integration type ADC
⁻ Output of comparator is 1 or 0, depending on whether the input voltage is greater (Ai > Ri) or
less than (Ai ≤ Ri) the instantaneous value of the ramp voltage.
⁻ The output of the comparator is used to control a logic gate IC2 which passes a clock signal to
the input of a pulse counter.
⁻ The pulses are counted until the voltage from the ramp generator exceeds that of the input
signal, at which point the output of the comparator goes low and no further pulses are
passed into the counter.
Ai

Ri

Ramp Circuit
(DAC) Counter

50
Analog to Digital Converters (ADC)
• 2. Flash type ADC
⁻ The analogue input
voltage is compared with
a series of fixed reference
voltages using a number
of operational amplifiers
⁻ When the analogue input
voltage exceeds the
reference voltage present
at the inverting input of a
particular operational
amplifier stage the output
of that stage will go to
logic 1.
⁻ The priority encoder is a
logic device that produces
a binary output code that
indicates the value of the
most significant logic 1
received on one of its
inputs. 51
Analog to Digital Converters (ADC)
• 3. Successive approximation ADC
⁻ Unlike the ramp ADC, the successive approximation ADC iteratively test each
individual bit from MSB to LSB. In this manner, it will be much faster compared to
ramp ADC since it will only take n number of comparison for an n-bit ADC.
⁻ In the first comparison for MSB, the SAR will be set to 100 with the DAC producing
Vd = 4V (VREF = 8V). Given Vi=5.6V, since Vi > Vd, the MSB has to be set to 1.
⁻ The next step is to test B1 (110) followed by LSB.
111

Vi 110
Successive
Approximation
Vd Register (SAR) 101

100

011

010

001
VREF 52
Computer I/O (Inputs and Outputs)

• There are three types of inputs, which the computer may receive:
⁻ Analogue inputs are constantly varying with respect to time, for example, a smooth changing
voltage.
⁻ Digital inputs are varying inputs also, but they vary in steps or increments.
⁻ Discrete inputs are represented by an on/off condition, usually the closure of a switch or
relay.

Central
Input Processing Output
Section Unit Section

Memory Section
53
Computer I/O (Inputs and Outputs)
• The purpose of the output section is to provide interface
between the computer and the outside world.
• It provides output signal conditioning into a format used by other systems
and computers.

Input Central
Output
Processing
Section Section
Unit

Memory Section

54
Conversion limitations
• Data Conditions: there are two types of data conditions that
must be considered when converting to digital representation:
⁻ Static
⁻ Dynamic
• Accuracy for the conversion of static and dynamic conditions is affected by the
conversion method and especially by the number of bits available in the digital
representation.
• Static: the resolution of a digital system is limited by the value of the least
significant bit. The fewer the number of bits available, the greater the value of
the LSB and therefore the less resolution (and less accuracy) of the digital
representation.

55
Conversion limitations

• When converting dynamic conditions to binary form, the


sampling rate must be considered (i.e. Nyquist theorem)

56
Summary
• Positional number system:
⁻ Binary, octal, decimal, hexadecimal
⁻ Unsigned, signed, 1’s complement, 2’s complement
⁻ Arithmetic operations (+, -, ×, ÷)
⁻ Carry, borrow, sign extension, overflow
• Fractional representation and floating point
• Digital codes:
⁻ ASCII, Gray code, BCD
• Parallel versus serial data
• Error detection and correction codes
⁻ Parity, CRC, Hamming
• Data conversion
⁻ Analog to digital conversion (ADC), DAC, and conversion limitations
Self-test quiz

 1. For the binary number 1000, the weight of the column with the 1 is
 (a) 4

 (b) 6

 (c) 8

 (d) 10

 2. The 2’s complement of 1000 is


 (a) 0111

 (b) 1000

 (c) 1001

 (d) 1010

58
Self-test quiz

 3. The fractional binary number 0.11 has a decimal value of


 (a) ¼

 (b) ½

 (c) ¾

 (d) none of the above

 4. The hexadecimal number 2C has a decimal equivalent value of


 (a) 14

 (b) 44

 (c) 64

 (d) none of the above

59
Self-test quiz
 5. Assume that a floating point number is represented in binary. If the
sign bit is 1, the
 (a) number is negative

 (b) number is positive

 (c) exponent is negative

 (d) exponent is positive

 6. When two positive signed numbers are added, the result may be larger
that the size of the original numbers, creating overflow. This condition is
indicated by
 (a) a change in the sign bit

 (b) a carry out of the sign position

 (c) a zero result

 (d) smoke 60
Self-test quiz

 7. The number 1010 in BCD is(a) number is


 (a) equal to decimal eight

 (b) equal to decimal ten

 (c) equal to decimal twelve

 (d) invalid

 8. How many HEX digits are there for a 32-bit binary


number?
 (a) 4

 (b) 8

 (c) 16

 (d) 32
61
Self-test quiz

 9. What is the result for binary subtraction: 0110 1010 – 1101 0110
 ________________________

 10. When a 15-bit binary number 101 1000 0111 1001 is appended with
an odd parity bit at the MSB, what is the 16-bit result expressed in HEX
format?
 ________________________

62

You might also like