Week 1 - Module 1 Number Systems

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

CPE 6204 – Logic Circuits and Switching Theory

1
Number Systems

Module 1: Number Systems

Course Learning Outcomes:


1. Convert between different number systems.
2. Perform addition and subtraction of signed and unsigned binary numbers.
3. Represent decimal and binary numbers in binary coded decimal.

Introduction
As computer engineers, we must cultivate genuine interest in what is going on under the
hood of any digital system. The beauty of understanding concepts behind the design of such
systems is that it is possible for us to design (and eventually create!) our own computing
machines from the ground up.
It is in this course that we begin exploring how digital systems are built. Fortunately, the
building blocks of digital systems are simple: 1’s and 0’s. The challenge comes in combining
these simple bocks to form complicated systems.
The topics in this course are divided into three broad categories:
 Number Systems in Digital Systems, with focus on binary numbers
 Combinational Logic Circuit Design
 Sequential Logic Circuit Design
In this first module, we will review number systems, particularly decimal, binary, octal, and
hexadecimal. We shall also dive into binary arithmetic, complements, and touch a little bit
on binary codes.
But first, we shall define some concepts.

Logic Circuits
Logic circuits are made up of one or more electronic devices that perform logical operations.
These circuits are used for control or processing functions in digital systems (Teach
Computer Science, 2019). We shall know more in succeeding modules.

Digital System
A digital system is made of interrelated components and manipulates discrete (finite and
distinct) elements of information, represented in 1’s and 0’s. A computer is an example of a
digital system.

Abstraction
We will be employing a lot of abstractions in this course to simplify things. We sometimes
need not bother with things that we don’t need, thus hiding them (abstracting), so that
complexity can be avoided. As an example, an application’s user interface is pretty much an

Course Module
abstraction of the complexity happening underneath. Ordinary users don’t need to know
programming to be able to use it.

Number Systems
Digital systems make use of more than one number system. We will constantly be seeing
them throughout the course.
Every number system has the following components: Unit, Number, and Base (or Radix).

Unit and Number


A unit is a single object (i.e., an apple) and a number is a symbol that represents a unit
or quantity.

Base
The base, or radix, of a number system signifies the number of symbols used in that
system. The base of any system is always expressed in decimal numbers. The radix of
the decimal system is 10. This means that there are 10 symbols used in the system:
0,1,2,3,4,5,6,7,8,9.
The base of a number system is indicated by a subscript following the value of the
number. The following are examples of numerical values in different bases with the
subscript to indicate the base:
91210 1234 20𝐴16
Note that the highest value symbol in a number system is always one less than the
base of the system.
In digital systems, the most common number systems other than the decimal are:
 Binary – uses 2 digits, 1 and 0 (called binary digits or bits)
 Octal – uses 8 digits: 0,1,2,3,4,5,6,7
 Hexadecimal – uses the 10 digits of the decimal number system, plus the letters A, B, C,
D, E, and F. A is equivalent to decimal 10, B to decimal 11, C to decimal 12, D to decimal
13, E to decimal 14, and F to decimal 15.
Numbers and their Value
The positional notation is commonly used to write numbers. The position determines
whether the value of a digit is high or low. Any number in positional notation can be
expanded in a power series:
𝐸𝑞. 1: 𝑁 = (… 𝑎4 𝑎3 𝑎2 𝑎1 𝑎0 𝑎−1 𝑎−2 𝑎−3 𝑎−4 … )𝑅
= … 𝑎4 × 𝑅 4 + 𝑎3 × 𝑅 3 + 𝑎2 × 𝑅 2 + 𝑎1 × 𝑅1 + 𝑎0 × 𝑅 0 + 𝑎−1 × 𝑅 −1 + 𝑎−2 × 𝑅 −2 …
where 𝑁 is the number written in positional notation, 𝑎𝑖 is the coefficient, 𝑅 is the base, and
𝑖 is the power to raise 𝑅.

Example 1:
1. DECIMAL: 478110 = (4 × 103 ) + (7 × 102 ) + (8 × 101 ) + (1 × 100 )
115.1210 = (1 × 102 ) + (1 × 101 ) + (5 × 100 ) + (1 × 10−1 ) + (2 × 10−2 )
2. BINARY: 11012 = (1 × 23 ) + (1 × 22 ) + (0 × 21 ) + (1 × 20 )
CPE 6204 – Logic Circuits and Switching Theory
3
Number Systems

1101.12 = (1 × 23 ) + (1 × 22 ) + (0 × 21 ) + (1 × 20 ) + (1 × 2−1 )
3. OCTAL: 1278 = (1 × 82 ) + (2 × 81 ) + (7 × 80 )
4. HEXADECIMAL: 𝐵65𝐹16 = (11 × 163 ) + (6 × 152 ) + (5 × 161 ) + (15 × 160 )
Using positional notation is not only employed in decimal, binary, octal, or hexadecimal
number systems but also in others. Positional notation is also useful in finding the decimal
equivalent of a certain value expressed in another number system. Hence, the decimal
equivalent of 10112 is 1110 , the decimal equivalent of 1278 is 8710 , and so on.

Number-Base Conversions
Decimal to Binary
To convert a decimal number into its equivalent binary number, divide the decimal number
by 2 to give an integer quotient. The remainder denoted by 𝑎𝑛 , which is either 0 or 1, will be
the coefficient of the binary number. Continue the division process until the integer quotient
becomes 0. Write the coefficients in descending subscript order.

Example 2: Convert 𝟏𝟏𝟏𝟎 into its binary equivalent.


Integer Quotient Remainder
11 ÷ 2 = 5 𝑎0 = 1
5÷2= 2 𝑎1 = 1
2÷2= 1 𝑎2 = 0
1÷2= 0 𝑎3 = 1
Hence, 1110 = 10112

Decimal to Octal
To convert a decimal number to its equivalent octal number, the process is like the decimal-
binary conversion, except we divide the decimal number by 8 instead of by 2.

Example 3: Convert 𝟏𝟓𝟑𝟏𝟎 into its octal equivalent.


Integer Quotient Remainder
153 ÷ 8 = 19 𝑎0 = 1
19 ÷ 8 = 2 𝑎1 = 3
2÷8= 0 𝑎3 = 2
Hence, 15310 = 2318

Decimal Fraction to Binary


To convert a decimal fraction into its equivalent binary number, the decimal fraction is
multiplied to 2 to give an integer and a fraction. The integer will be the coefficient of the
binary number. The process continues until the fraction becomes 0, or until the number of
digits has enough accuracy.

Course Module
Example 4: Convert 𝟎. 𝟔𝟖𝟕𝟓𝟏𝟎 to its binary equivalent
Integer Quotient Fraction
0.6875 × 2 = 𝑎−1 = 1 +0.3750
0.3750 × 2 = 𝑎−2 = 0 +0.7500
0.7500 × 2 = 𝑎−3 = 1 +0.5000
0.5000 × 2 = 𝑎−4 = 1 +0.0000
Hence, 0.687510 = 0.10112

Decimal Fraction to Octal


Converting a decimal fraction to its equivalent octal fraction is like the decimal fraction –
binary fraction conversion, except we multiply the decimal fraction to eight.

Binary to Octal
To convert a binary number into its octal equivalent, we first need to partition the binary
number into groups of three digits each, starting from 𝑎0 then proceeding to the left. In the
case of fractions, we start partitioning from 𝑎−1 then proceeding to the right. Each group of
three digits is equivalent to a single octal digit.

Example 5: Convert 𝟏𝟎𝟏𝟏𝟎𝟎𝟎𝟏𝟏𝟎𝟏𝟎𝟏𝟏. 𝟏𝟏𝟏𝟏𝟎𝟎𝟎𝟎𝟎𝟏𝟏𝟎𝟐 into its octal equivalent.


Binary 10 110 001 101 011 . 111 100 000 110
Octal 2 6 1 5 3 . 7 4 0 6

Binary to Hexadecimal
Converting from binary to hexadecimal is like the binary-octal conversion, except the
number is partitioned into groups of four. Given the same binary number as Example 8, it’s
hexadecimal equivalent would be 2𝐶6𝐵. 𝐹216

Octal to Binary/Octal to Hexadecimal


Each octal digit is converted to its three-digit binary equivalent, and each hexadecimal digit
is converted to its four-digit binary equivalent.

Example 6:
1. 673.1248 = 1 1011 1011.0010 1010 02
2. 306. 𝐷16 = 0011 0000 0110.11012

Binary Addition and Subtraction


Digital systems are made primarily of electronic components. These components function
according to the presence, or absence, of electrical signals such as voltages and currents.
Electrical signals in modern digital systems use only two discrete values: high and low and
are easily represented in the binary number system: 1 for high, and 0 for low.
While decimal, octal, and hexadecimal numbers are shorter and more convenient to use, the
“language” used by digital systems is expressed in 0’s and 1’s.
Computers, smart phones, tablets, and other gadgets that manipulate discrete elements of
information all require components that will perform information processing. That job falls
on the processor, or microprocessor.
CPE 6204 – Logic Circuits and Switching Theory
5
Number Systems

Most processors have similar components: parts that do computations and logical
operations (Arithmetic and Logic Unit, or ALU), parts that fetch and execute instructions (the
Control Unit), and parts that serve as immediate information storage (Memory Unit).
To be able to design these components, we must first understand how each part performs its
function. We shall start with arithmetic operations.

Binary Arithmetic
Arithmetic operations in digital systems are usually done in binary because design of logic
circuits to perform binary arithmetic is much easier than for decimal (imagine how
complicated it would be having to use ten different current or voltage values to represent
decimal numbers).
Binary arithmetic is carried out in a similar manner as decimal, but easier.

Binary Addition
Binary Addition
Just like in decimal addition, if the sum of two 0+0=0
numbers cannot be expressed as a single digit, we 0+1=1
carry a 1 into the next column. Figure 1 summarizes 1+0=1
the principles of binary addition. 1 + 1 = 0∗
*carry 1 to the column to the left
Example 7: Add 𝟎𝟏𝟏𝟏𝟐 and 𝟎𝟏𝟎𝟏𝟐 . Figure 1. Summary of Binary Addition
Figure 1.2 below shows how
⬚ 111 ← 𝑐𝑎𝑟𝑟𝑖𝑒𝑠
we add two 4-bit binary numbers. The sum is computed to be 11002 .
⬚ 0111
We can check by performing the addition in decimal. 01112 = 710
+ 0101
and 01012 = 510 . The sum is 11002 = 1210 .
⬚ 1100
Figure 2. Example 6 Binary Addition with Overflow
Addition is said to overflow if the sum does not fit in the available digits.
11 1 That is, if we add two 4-bit binary numbers and the sum requires 5 bits,
1101 an overflow occurs. If the digital system only has 4 bits for the output, the
+ 0101 5th bit is discarded. However, this will result in an incorrect sum.
1 0010
Example 8: 𝑪𝒐𝒎𝒑𝒖𝒕𝒆 𝟏𝟏𝟎𝟏𝟐 + 𝟎𝟏𝟎𝟏𝟐 . 𝑫𝒐𝒆𝒔 𝒐𝒗𝒆𝒓𝒇𝒍𝒐𝒘 𝒐𝒄𝒄𝒖𝒓?
Figure 3. Binary
addition with Figure 3 shows that the sum of the two binary numbers is 100102 . Notice
overflow that the result requires 5 bits, whereas the addend and augend are only
4-bit numbers. In this case, we discard the most significant bit (or MSB,
the bit at the leftmost position; the rightmost bit is the least significant bit, or LSB). So the
sum is “clipped” to only 0010, giving us an incorrect output.
If we used 5 bits instead of 4, all bits in the sum would have been accommodated, and the
result would be correct.

Course Module
Signed Binary Numbers
What we have seen so far is addition of binary numbers. How about subtraction, then?
Subtraction can be expressed in terms of addition, that is, addition of two numbers with
different signs – one being positive, the other being negative.

One important consideration in designing digital systems is cost. Designs


are translated into physical components and manufacturing does not come
cheap. Therefore, designs that work well, are simpler, and use the least amount
of resources are favored over those that perform just as well but are more
complicated and costlier.
So, it would be more practical to use one circuit to do addition and
subtraction rather than separate ones. In terms of manufacturing, having two
separate circuits to do the arithmetic could mean more components, larger board
size, and bigger manufacturing cost.

There are many ways to represent signed binary numbers, the two most common are
sign/magnitude, and two’s complement.

Sign/Magnitude Numbers
Comparing the two most common ways of representing binary numbers, sign/magnitude
will win in terms of human readability. That is because we are used to seeing this
representation. Negative numbers are written with a minus sign, followed by the magnitude.
Positive numbers may or may not have a plus sign preceding the magnitude.
Signed binary numbers are written with the MSB signifying the sign, the rest of the bits are
used for the magnitude. The number is positive if the sign bit is 0, and negative if the sign bit
is 1.

Example 9: Write 𝟓 and −𝟓 as 4-bit sign/magnitude numbers.


Both numbers have a magnitude of 510 = 1012 . Therefore,
510 = 01012 and −510 = 11012
Unfortunately, it would be very hard to perform ordinary binary addition on these numbers.
We would expect that −5 + 5 to give us 0 but 11012 + 01012 gives us 100102 , which is
nowhere near what we expected the output to be.
Another difficulty we might face with using sign/magnitude is that 0 can be represented as
+0 or −0.

Two’s Complement Numbers


Two’s complement numbers overcome the limitations of the sign/magnitude numbers. Zero
is written as all zeros, and ordinary addition works.
Like sign/magnitude representation, a two’s complement number is positive if the MSB is 0
and negative if the MSB is 1. However, the rest of the bits cannot be interpreted as the
magnitude. This is because two’s complement numbers go through taking the two’s
complement process.
To take the two’s complement of a binary number, we simply flip the bits, that is, we turn 0’s
into 1’s and 1’s into 0’s and then we add a 1 to the least significant bit position. This is useful
in finding the representation of a negative number or determining the magnitude of a
negative number.
CPE 6204 – Logic Circuits and Switching Theory
7
Number Systems

Example 10: Find the representation of −𝟐𝟏𝟎 as a 4-bt two’s complement number.
210 = 0010. Flipping the bits and adding 1 to the LSB gives us:
11012 + 0001 = 11102 . So, −210 = 11102 .

Example 11: Find the decimal value of the two’s complement number 𝟏𝟎𝟎𝟏𝟐
Since the sign bit is 1, the number must be negative. To find the magnitude, we flip the bits
and add 1.
01102 + 0001 = 01112 = −710 .

Arithmetic with Two’s Complement Numbers


With two’s complement representation, we can now add signed binary numbers. If the
number is positive, we leave it as is. If the number is negative, we get it’s two’s complement
and use it to add to the other number.

Example 12: Compute a) −𝟐𝟏𝟎 + 𝟏𝟏𝟎 and b) −𝟕𝟏𝟎 + 𝟕𝟏𝟎 using 4-bit two’s complement
numbers.
a) We use the two’s complement of −210 and add it to 110 . The two’s complement of −210
is 11102 .
−210 = 11102
+ 110 = 00012
−110 = 11112
At this point, it would be good to know that the sum 11112 is still in two’s complement form.
Notice that the decimal equivalent is −110 . In general, if the expected output is a positive
number, the sum will produce the magnitude of the binary number. If the expected output is
a negative number, the sum will be in two’s complement form, like the one in Example 12.
To get the magnitude of the number, simply take the two’s complement of 11112 .
b) We take the two’s complement of −710 and add it to 710 . The two’s complement of −710
is 10012 .
−710 = 10012
+ 710 = 01112
010 = 1 00002
The sum is 5 bits long, so we discard the 5th bit, 1, which is in the MSB position. The
answer is not negative therefore, the magnitude is immediately produced.

Example 13: Compute a) 𝟓𝟏𝟎 − 𝟑𝟏𝟎 and b) 𝟑𝟏𝟎 − 𝟓𝟏𝟎 using 4-bit two’s complement numbers.
a) We take the two’s complement of −310 and add it to 510 . The two’s complement of −310
is 11012 .
510 = 01012
+ −310 = 11012
210 = 100102

Course Module
We discard the 5th bit, 1, in the MSB position and retain only the 4 bits. The sum correctly
reflects 210 in binary.
b) We take the two’s complement of −510 and add it to 310
310 = 00112
+ −510 = 10112
−210 = 11102
Flip the bits of 11102 to get the magnitude. The result is 00102 .

Binary Codes
Digital systems make use of not only binary numbers but also other discrete elements of
information. These elements of information can be represented with a binary code.
We say binary code because information is transformed into what looks like binary numbers.
However, the meaning is unlike that of ordinary binary numbers. For example, 1210 can be
coded as 0001 0010 or as 0100 0101. If these strings of 0’s and 1’s were to be interpreted
using ordinary binary number system, the former would be interpreted as 1810 , and the
latter would be interpreted as 6910 .
There are many digital systems that use of binary codes. They simply transform the decimal
numbers into binary numbers without changing their meaning. In the example previously,
0001 0010 is the binary code for 1210 . The first four bits is equivalent to 1, and the last four
bits is equivalent to 2.
It would be easy for humans to read this kind of code because it allows easy conversion
between decimal and binary number systems. However, it is wasteful as it uses more bits
than the equivalent ordinary binary number.
Binary codes for decimal digits require a minimum of four bits per digit. Many different codes
can be formulated by arranging four bits into 10 distinct combinations. Below is a table of
four representative codes. Each code uses only 10 out of possible 16-bit combinations that
can be arranged with four bits. The other six unused combinations have no meaning and
should be avoided.
Table 1. Binary Codes
Decimal BCD
2421 Excess-3 8,4,-2,-1
Digit 8421
0 0000 0000 0011 0000
1 0001 0001 0100 0111
2 0010 0010 0101 0110
3 0011 0011 0110 0101
4 0100 0100 0111 0100
5 0101 1011 1000 1011
6 0110 1100 1001 1010
7 0111 1101 1010 1001
8 1000 1110 1011 1000
9 1001 1111 1100 1111
1010 0101 0000 0001
1011 0110 0001 0010
Unused bit 1100 0111 0010 0011
combinations 1101 1000 1101 1100
1110 1001 1110 1101
1111 1010 1111 1110
CPE 6204 – Logic Circuits and Switching Theory
9
Number Systems

Binary Coded Decimal (BCD), 2421, and 8,4,-2,-1 are examples of weighted code. In a
weighted code, each bit position is assigned a weighting factor in such a way that each digit
can be evaluated by adding the weights of all the 1’s in the combination.
BCD adders (hardware) add BCD values directly, digit by digit, without converting the
numbers to binary. However, it is necessary to add 6 to the result if it is greater than 9. BCD
adders require significantly more hardware.
The 2421 and the Excess-3 (also written as XS-3) codes are examples of self-complementing
code. Such codes have the property that the 9’s complement of a decimal number is obtained
directly by changing 1’s to 0’s and 0’s to 1’s.

The 9’s complement is a diminished radix complement which is obtained


by subtracting each decimal digit from 9. For example, the 9’s complement of
12310 is 876. The binary equivalent of a diminished radix complement is the 1’s
complement. It can be obtained by simply flipping all the bits.

For example, the decimal 395 is represented in excess-3 code as 0110 1100 1000. The 9’s
complement of 395 is 604 and is represented in excess-3 code as 1001 0011 0111. Notice
that simply flipping the bits of the excess-3 equivalent of 395 gives us the excess-3 equivalent
of 604.

Gray Code
Table 2. Gray Code

Decimal Gray The output data of many physical systems are quantities that are
Equivalent Code continuous. These data must be converted to digital form before they
0 0000 can be used in a digital system. Continuous or analog information is
1 0001 converted to digital form by means of an analog-to-digital converter.
2 0011 It is sometimes convenient to use the Gray code to represent digital
3 0010 information that have been converted from analog. The advantage of
4 0110 Gray code over the ordinary binary sequence is that only one bit in
5 0111 the code group changes in going from one number to the net.
6 0101
7 0100 The Gray code is non-weighted like the XS-3 code.
8 1100
9 1101
10 1111
11 1110
12 1010
13 1011
14 1001
15 1000

Course Module
References and Supplementary Materials
Books and Journals
1. Mano, M. M., Ciletti, M.D. (2018). Digital Design: With an Introduction to the Verilog HDL,
VHDL, and SystemVerilog (6th ed.). New Jersey: Pearson.
2. Harris, D.M, Harris, S.L. (2016). Digital Design and Computer Architecture: ARM Edition.
Massachusetts: Elsevier Inc.
3. Roth, C.H. Jr., Kinney, L.L. (2014). Fundamentals of Logic Design (7th ed.). Connecticut:
Cengage Learning

Online Supplementary Reading Materials


Online Instructional Videos
1. Adding in Binary, https://www.khanacademy.org/math/algebra-home/alg-intro-to-
algebra/algebra-alternate-number-bases/v/binary-addition, Date Accessed: 01
October 2019.
2. BCD Addition, https://youtu.be/yPu57aSj9kA, Date Accessed: 01 October 2019.

You might also like