0% found this document useful (0 votes)
50 views22 pages

Lecture 2: Number Systems and Codes: Youssef Jaffal

This lecture discusses number systems and codes used in digital systems. It covers positional number systems like binary, octal, hexadecimal and their conversions. It also describes how signed and two's complement representations are used to represent negative numbers in binary. Addition and subtraction operations in binary are demonstrated.

Uploaded by

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

Lecture 2: Number Systems and Codes: Youssef Jaffal

This lecture discusses number systems and codes used in digital systems. It covers positional number systems like binary, octal, hexadecimal and their conversions. It also describes how signed and two's complement representations are used to represent negative numbers in binary. Addition and subtraction operations in binary are demonstrated.

Uploaded by

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

Lecture 2: Number Systems and Codes

Youssef Jaffal

Phoenicia University
Electrical & Communication Engineering
EENG 212: Digital System Design

References: [Wakerly] Chapter: 2

The figures in this presentation are taken from “Wakerly, Digital Design: Principles and Practices, 4th
edition.”
1/22
Youssef Jaffal Lecture 2: Number Systems and Codes
Introduction

Digital systems are built from circuits that process binary


digits (0s and 1s).

A digital designer must establish a correspondence between


binary digits and real-life numbers, events and conditions.

2/22
Youssef Jaffal Lecture 2: Number Systems and Codes
Positional Number Systems
A number is represented by a string of digits
Each digit position has an associated weight
The value of a number is a weighted sum of the digits
Ex1: 1734 = 1 × 1000 + 7 × 100 + 3 × 10 + 4 × 1
The weights are power of 10: 1000, 100, 10, 1
Ex2: 85.68 = 8 × 10 + 5 × 1 + 6 × 0.1 + 8 × 0.01
We have positive and negative powers
This system had a base (radix, r) of 10
General form of a number is:

d p−1 d p−2 ...d1 d0 .d−1 d−2 ...d−n

The value of the number is:


p−1
D= ∑ di ri where r≥2
i=−n
d p−1 = Most significant digit and d−n = Least significant digit

3/22
Youssef Jaffal Lecture 2: Number Systems and Codes
Positional Number Systems
Binary: radix = 2
Digital circuits have signals that are normally in one of only
two conditions; low or high, charged or discharged, off or on.
Binary is used to represent numbers in a digital system
Ex: 110.012 = 1 × 4 + 1 × 2 + 0 × 1 + 0 × 0.5 + 1 × 0.25 = 6.2510
In general the value is given by:
p−1
B= ∑ bi 2i
i=−n

Octal and Hexadecimal represent a shorthand notation for the


binary system
Octal (Base 8): {0, 1, 2, 3, 4, 5, 6, 7}
Ex: 5768 = 5 × 82 + 7 × 8 + 6 × 1 = 38210
Hexadecimal (Base 16): {0, ..., 9, A, B, C, D, E, F}
Ex: A2F16 = A × 162 + 2 × 16 + F × 1 =
10 × 162 + 2 × 16 + 15 × 1 = 260710
4/22
Youssef Jaffal Lecture 2: Number Systems and Codes
General Positional Number System Conversions
To convert a number D to decimal, we convert each digit to
radix-10 equivalent and expand the weighted-sum formula
using radix-10 arithmetic

Ex1: 1CE8.116 =
1 × 163 + 12 × 162 + 14 × 16 + 8 × 1 + 1 × 16−1 = 7400.062510

Ex2: F1AC16 = 12 + 16 × [10 + 16 × (1 + 15 × 16)] = 6186810

5/22
Youssef Jaffal Lecture 2: Number Systems and Codes
General Positional Number System Conversions

Decimal to Binary: It is done by successive divisions until a


zero quotient is obtained. The remainders taken in reverse
order constitute the binary representation

6/22
Youssef Jaffal Lecture 2: Number Systems and Codes
General Positional Number System Conversions
Decimal fraction to binary: It is done by successive
multiplications taking out each time the integer part of the
result.
Example: convert 0.3125 to binary

0.3125 × 2 = 0.625 →0
0.625 × 2 = 1.25 →1
0.25 × 2 = 0.5 →0
0.5 × 2 = 1 →1 (LSB)

Then 0.312510 = 0.01012

7/22
Youssef Jaffal Lecture 2: Number Systems and Codes
General Positional Number System Conversions
Binary to Octal: Bits are grouped in “Threes” from right to
left, and the binary value of every group represents the
corresponding Octal Digit
Example

Result: 100111001012 = 23458


Binary to Hexadecimal: Similar, but in groups of four.
Example

Result: 110111001011102 = 372E16

8/22
Youssef Jaffal Lecture 2: Number Systems and Codes
Addition of Binary Numbers

Example

Using the Carry vector C:

9/22
Youssef Jaffal Lecture 2: Number Systems and Codes
Subtraction of Binary Numbers

Example

Using the Borrow vector B:

10/22
Youssef Jaffal Lecture 2: Number Systems and Codes
Representation of Negative Numbers

Negative integers can be represented using signed-magnitude,


one’s complement or two’s complement notations

Sign and Magnitude: Leftmost bit is used to indicate the sign


0 for positive and 1 for negative
Example using 4 bits: +5 = 0101 and -5 = 1101
Range: from -7 to +7 (from 1111 to 0111). i.e. (−(2N−1 − 1)
to 2N−1 − 1)
Disadvantages:
Two representations of zero: (0000, 1000).
The process of addition needs a relatively complex adder.

11/22
Youssef Jaffal Lecture 2: Number Systems and Codes
Complements

Complements are used in digital systems to negate a number


and to simplify the subtraction operation.
Two types of complements for any base-r system:
Radix complement: called r’s complement
Diminished radix complement: called (r − 1)’s complement
In base ten: 10’s complement, 9’s complement
In base 2: 2’s complement, 1’s complement
Diminished radix: Given a number N in base r having n digits,
the (r − 1)’s complement is defined as follows:

(rn − 1) − N

12/22
Youssef Jaffal Lecture 2: Number Systems and Codes
Complements
(r − 1)’s complement = (rn − 1) − N
Ex: r = 10, what is 10n ?
It is a single ‘1’ followed by n 0s. Ex: 104 = 1000010
Ex: r = 10, what is 10n − 1?
It is n 9s. 104 − 1 = 9999
What is 9’s complement of 546700?
106 − 1 − 546700 = 999999 − 546700 = 453299


Ex: r = 2, what is 24 ? 100002


Ex: r = 2, what is 24 − 1? 11112
What is 1’s complement of 11010?
25 − 1 − 11010 = 11111 − 11010 = 00101


1’s complement: subtract each digit from 1. But 1 − 0 = 1,


1 − 1 = 0 then to get 1’s complement of a binary number, flip
all its digits. 13/22
Youssef Jaffal Lecture 2: Number Systems and Codes
Complements

Radix complement: Given a number N in base r having n


digits, the r’s complement is defined as follows:
 n
r − N N 6= 0
0 N=0

Since rn − N = [(rn − 1) − N] + 1
r’s complement is obtained by taking (r − 1)’s complement and
adding 1.
Ex1: 2’s complement of 101100 is
010011 + 1 = 010100

Ex2: 10’s complement of 012398 is


987601 + 1 = 987602

14/22
Youssef Jaffal Lecture 2: Number Systems and Codes
Complements
Short cuts:
10’s complement: leave all least significant 0s unchanged,
subtract first nonzero least significant digit from 10, and
subtract all higher significant digits from 9.
Ex: 10’s complement of 012398 is 987602

2’s complement: leave all least significant 0s unchanged, leave


the first 1 unchanged, flip all other higher significant digits.
Ex: 2’s complement of 101100 is 010100

What is the r’s complement of the r’s complement of a


number?

15/22
Youssef Jaffal Lecture 2: Number Systems and Codes
Representation of Negative Numbers

Two’s complement: A negative number is represented by


complementing each bit of the corresponding positive number
and adding 1.
MSB has a weight of −2N−1 instead of 2N−1 .
A negative number has MSB = 1
Example using 8 bits:

000010012 = +910 = 20 + 23
111101112 = −910 = 20 + 21 + 22 + 24 + 25 + 26 −27

Range is from -128 to +127 (10000000 to 01111111)


For N bits, the range is: −2N−1 to +2N−1 − 1
Advantages:
1 representation of 0. Ex: Using 4 bits: 0000
Addition can be easily carried out.

16/22
Youssef Jaffal Lecture 2: Number Systems and Codes
Representation of Negative Numbers

17/22
Youssef Jaffal Lecture 2: Number Systems and Codes
2’s Complement Addition: Overflow

An overflow is said to occur if the result of the addition


exceeds the range.
Addition of 2 numbers with different signs cannot produce an
overflow.
Examples:

A simple rule to detect overflow is to check if the sign of the


result is different from the sign of the addends.
18/22
Youssef Jaffal Lecture 2: Number Systems and Codes
Subtraction Rules
Most subtraction circuits for 2’s complement numbers do not
perform subtraction. Instead, we take the two’s complement
of the subtrahend and add the 2 numbers: A − B = A + (−B)
We can complement bit by bit the subtrahend, added to the
minuend and add a carry of 1
Examples:

We can use the same rules of addition to detect overflow.


19/22
Youssef Jaffal Lecture 2: Number Systems and Codes
Binary Codes for decimal Numbers
Some digital devices process decimal numbers
The interface of a digital system may read or display decimal
numbers
A decimal number is represented by a string of at least 4 bits

20/22
Youssef Jaffal Lecture 2: Number Systems and Codes
Binary Codes for decimal Numbers
Gray Code
Only 1 bit changes between each pair of consecutive codewords
It is a reflective code
For a 1-bit code: 0 and 1
For (N + 1) bit code, the first 2N are the same of N-bit code
with a leading 0
The last 2N are the reverse of N-bit code with a leading 1

21/22
Youssef Jaffal Lecture 2: Number Systems and Codes
Binary Codes for decimal Numbers
Gray Code, Example
Figure taken from mathworks.com

22/22
Youssef Jaffal Lecture 2: Number Systems and Codes

You might also like