You are on page 1of 16

BINARY NUMBERS AND CODES

UNIT 2: BINARY NUMBERS AND CODES

INTRODUCTION

The binary number system and digital codes are fundamental to digital electronics. This
chapter will elaborate on the binary number system and its relationship to other number
system such as decimal. Arithmetic operations with binary numbers are covered to
provide a basis for understanding how computers and many other types of digital system
work. Also digital codes such as binary coded decimal (BCD), Gray code and ASCII are
covered.

LEARNING OBJECTIVES

The objectives of this unit are to :


1. Count binary number system
2. Convert decimal to binary and from binary to decimal
3. Add and subtract binary numbers
4. Express decimal numbers in BCD forms.

LEARNING OUTCOMES

After completing the unit, students should be able to:


1. Determine the weighting factor for each digit position in the decimal and
binary system.
2. Able to convert decimal to binary number and binary number to decimal
3. Able to describe the format and use of BCD, Gray and ASCII code.
4. Able convert decimal to BCD number and BCD number to decimal
5. Able convert decimal to Hex number and Hex number to decimal and to
binary elsewell
6. Able to perform BCD addition
7. Convert decimal to grey code and grey code to decimal

BPLK 15 DFE1013 (VERSION 3.0)


BINARY NUMBERS AND CODES

2.1 INTRODUCTION TO BINARY NUMBERS

The binary number system is simply another way to count. The binary system is less
complicated than the decimal system because it has two digits. The decimal system with
its ten digits is a based-ten system; the binary system with its two digits is a base-two
system. The two binary digits (bits) are 1 and 0.
• Base-2 system
• 2 digits/symbols: 0, 1
• Examples: 0, 1, 01, 111, 101010
• The position of each digit (bit) in a binary number can be assigned a weight

2.1.1 Evaluating Binary Numbers

The decimal value of any binary number can be determined by adding the weights of all
bits that are 1 and discarding the weights of all bits that are 0. The following two
examples will illustrate this

Example 2.1 :
1011.1012

Solution :
1011.1012=(1x23) + (0x22) + (1x21) + (1x20) + (1x2-1) + (0x2-2) + (1x2-3)
= 8 + 0 + 2 + 1 + 0.5 + 0 + 0.125
= 11.62510

Value according to 23 22 21 20 2-1 2-2 2-3


position
Base 2 Digit 1 0 1 1 ● 1 0 1

MSB LSB

MSB: Most Significant Bit


LSB: Least Significant Bit

With N bit, the maximum value that can be show is 2N-1 and the summation of
combinational bit can be obtain is 2N

BPLK 16 DFE1013 (VERSION 3.0)


BINARY NUMBERS AND CODES

Example 2.2:
How many combinational number can be obtain by 2 bit and what is the biggest number
can be show?

Solution :
There is 22 =4 combinational number which is 002, 012, 102 and 112
The biggest number can be show is 22-1=310 or 112

Table 2.1 below shows the differentiation of 3 bits binary number and its equivalent
decimal value

Binary Decimal
22=4 21=2 20=1
0 0 0 0
0 0 1 1
0 1 0 2
0 1 1 3
1 0 0 4
1 0 1 5
1 1 0 6
1 1 1 7

Table 2.1

2.1.2 Decimal to Binary Conversion

Decimal number is simply expressed as a sum of powers of 2 and then 1s and 0s which
are written in the appropriate bit position. To illustrate:

= 1 0 1 1 0 12
Note that a 0 is placed in the 25, 23, 22 and 20 positions.

There are two ways (method 1 and 2) to convert a decimal whole number to its
equivalent binary-system representation. There is a method (method 3) to convert
decimal fraction to binary.

BPLK 17 DFE1013 (VERSION 3.0)


BINARY NUMBERS AND CODES

METHOD 1: SUM OF WEIGTH

Given 2510
Step 1: Find the power of two that fulfills the following:
a. nearest to the given decimal number; and
b. its decimal number is less than or equal to the given decimal
number 22? No, because it is not the nearest. 8 (23) is nearer and still
less than 25.
25? No, because it’s decimal number, 32 is more than 25.
24? Yes, because it is the nearest and its decimal, 16 is less than 25.

Step 2: Subtract the power of two (from Step 1) from the given Decimal number
25-16=9
The result of the subtraction is 9.

Step 3: If the result of the subtraction in Step 2 is 0, go to Step 4.


Else, repeat Steps 1 and 2 for the result of the subtraction in Step 2.

Step 4: Write out the binary number based on all the powers of two from Step 1.

 1 1 0 0 12

The binary number is 110012

BPLK 18 DFE1013 (VERSION 3.0)


BINARY NUMBERS AND CODES

METHOD 2: REPEATED DIVISION BY 2

Method 1 can convert both whole numbers and fractional numbers to binary. Method 2
is to convert whole numbers to binary.
Given 4510

– Repeat the division until the quotient is 0.

Quotien Remainde
L
S
1 B

The binary number is 1011012


M
S
METHOD 3: REPEATED MULTIPLICATION BY 2
B
Method 3 is to convert decimal fraction to binary.Repeat the multiplication until the
fractional part is all zeros.
Given 0.3125 M
S
B
0.3125 x 2 = 0.625 0
0.625 x 2 = 1.25 1
0.25 x 2 = 0.50 0
0.50 x 2 = 1.00 1 L
S
B
The binary fraction is 01012

BPLK 19 DFE1013 (VERSION 3.0)


BINARY NUMBERS AND CODES

2.1.3 Binary to Decimal Conversion

Given 110012
S1: write the weights

24 23 22 21 20
weights

Binary number 1 1 0 0 1

S2: write the sum of the products of each digit with its weight
110012 =(1x24)+(1x23)+(0x22)+(0x21)+(1x20)
=16 + 8 + 1
=2510

2.2 BINARY SYSTEM ARITHMETIC

Binary arithmetic is essential in all digital computers and in other types of digital systems.

2.2.1 Binary Subtraction

The four basic rules for subtracting digits are as follows:


• 0–0=0
• 1–1=0
• 1–0=1
• 10 – 1 = 1 0 – 1 with a borrow of 1

Example 2.3:
101 – 11 = ?
In decimal
101 5
- 11 -3
10 2

BPLK 20 DFE1013 (VERSION 3.0)


BINARY NUMBERS AND CODES

2.2.2 Binary Addition

The four basic rules for addition digits are as follows:


• 0 + 0 = 0 with a carry of 0
• 0 + 1 = 1 with a carry of 0
• 1 + 0 = 1 with a carry of 0
• 1 + 1 = 10 with a carry of 1

Example 2.4:
111 + 11 = ?
In decimal
111 7
+ 11 +3
1010 10

2.3 BINARY CODES

There are 3 binary codes


– BCD (Binary Coded Decimal)
– Gray Coded
– ASCII

The comparison is shown in Table 2.1

BPLK 21 DFE1013 (VERSION 3.0)


BINARY NUMBERS AND CODES

Table 2.1: Number Coded


DECIMAL BINARY BCD GRAY CODE
0 0000 0000 0000
1 0001 0001 0001
2 0010 0010 0011
3 0011 0011 0010
4 0100 0100 0110
5 0101 0101 0111
6 0110 0111 0101
7 0111 0111 0100
8 1000 1000 1100
9 1001 1001 1101
10 1010 0001 0000 1111
11 1011 0001 0001 1110
12 1100 0001 0010 1010
13 1101 0001 0011 1011
14 1110 0001 0100 1001
15 1111 0001 0101 1000

2.3.1 Binary Coded Decimal (BCD)

BCD stands for Binary Coded Decimal Code. BCD is a way to represent each digit of a
decimal number with its 4-bit binary number. Table 2.1 shows the decimal numbers that
have been converted to BCD code.

Example 2.5:
87410

Solution :
Decimal 8 7 4

BCD 1000 0111 0100

BPLK 22 DFE1013 (VERSION 3.0)


BINARY NUMBERS AND CODES

Therefore, the BCD code for 87410 is 1000 0111 0100BCD. Convert a BCD code to its
decimal equivalent.

Step 1: Break the BCD into 4-bit groups, starting from LSB
Step 2: Replace each 4-bit group with its equivalent decimal

Example: 0110 1000 0011 1001

6 8 3 9

So, the decimal equivalent is 683910

Example 2.6:
BCD addition follows the same rules as binary addition. However, if the addition
produces a carry and/or creates an invalid BCD number, an adjustment is required to
correct the sum. The correction method is to add 6 to the sum in any digit position that

has caused an error.

Decimal BCD

4 7 0100 0111
+ 2 5 + 0010 0101
7 2 0110 1100
6 12 (?)

Correction must be made by adding decimal 6 or 0110 to 12

0100 0111
6 is added to have ‘carry’
+ 0010 0101
0110 1100
+ 0110
0111 0010
7 2___

Example 2.7:

BPLK 23 DFE1013 (VERSION 3.0)

6 is added
BINARY NUMBERS AND CODES

Decimal BCD

4 7 8 0100 0111 1000


+ 1 3 5 + 0001 0011 0101
6 1 3 0101 10101 1101 (Error)
+0000 0000 0110
0101 1011 0011 (Error)
+0000 0110 0000 6 is added
0101 0001 0011

BCD code is easier to convert to and from decimal number. The code produce ‘carry’
resulting longer delay time before obtaining the answer (refer example 2.5). It also used
more binary bit rather than ‘straight binary coding’. For example:
137=100010012
137=0001 0011 0111BCD

2.3.2 Gray Code

Created by Gray (1953), Gray code has no weights assigned to the bit positions. Only a
single bit change from one code word to the next in sequence. Converting binary number
to gray code can be done as follow:

If N-digit gray code is required, its binary number must have (N+1) bit. If the bits are not
enough, add as many 0 that is required so that (N+1) bit is enough on left MSB side.

The steps are as follows:


 Starting with the rightmost digit.
 Compare the 2 binary bit next to each other
 If the two neighboring digits are the same, the output is 0
 If the two neighboring digits are different, the output is 1
 The combination of the output is the gray code

Converting Gray Code to binary number is done as follow:


 Do the sequential additional from left (MSB) to right without the ‘carry’
 Additional start from 0

BPLK 24 DFE1013 (VERSION 3.0)


BINARY NUMBERS AND CODES

Example 2.8:
Convert the binary number 1011 into 4 digits Gray code

Solution :
N=4, the sum of binary bit=4+1=5 bit

Add 0

Binary Code 0 1 0 1 1

4 digits Gray Code for


binary number 1011
1 1 1 0

Compare the pair next to each other (output)

Example 2.9:
Convert the binary number 11 into 3 digits Gray code

Solution :

N=3, the sum of binary bit=3+1=4 bit


Add 0

Binary Code 0 0 1 1

3 digits Gray Code for


0 1 0 binary number 11

Compare the pair next to each other (output)

Example 2.10:
Convert the Gray Code 1110 into binary number

Solution :
1 1 1 0
Start the
BPLK additional 25
+ DFE1013 (VERSION 3.0)
with 0
BINARY NUMBERS AND CODES

0 1 0 1
1 0 1 12 Binary Number

2.3.3 ASCII Code

ASCII stands for American Standard Code for International Interchange. The code is
used in computers and electronic equipment.

processor 101100
1

ASCII code has 128 characters and symbols and represented by 7-bit binary code. It can
be considered an 8-bit code with MSB 0. The first 32 ASCII characters are non-graphic
commands only for control purposes—The ASCII Control Characters, e.g.: null, line feed,
start of text, escape

Table 2.2: ASCII Code

BPLK 26 DFE1013 (VERSION 3.0)


BINARY NUMBERS AND CODES

2.4 HEXADECIMAL NUMBER SYSTEM

The hexadecimal system uses base 16. Thus, it has 16 possible digit symbols. It uses
the digits 0 throgh 9 plus A, B, C, D, E and F as the 16 digit symbols. Table 2.2 shows
the relationships between hexadecimal,, decimal and binary. Note that each
hexadecimal digit represents a group of four binary digits. It is important to remember
that the hex (abbreviation for hexadecimal) degits A through F are equivalent to the
decimal values of 10 though 15.

2.4.1 Hex to decimal conversion

BPLK 27 DFE1013 (VERSION 3.0)


BINARY NUMBERS AND CODES

A hex number can be converted to its decimal equivalent by using the fact that each hex
digit position has a weight that is a power of 16. The LSD has a weight of 16 o = 1, the
next higher has a weight of 162 = 256 and so on. The conversion process is
demonstrated in the example below

Table 2.2

Hexadecimal Decimal Binary


0 0 0000
1 1 0001
2 2 0010
3 3 0011
4 4 0100
5 5 0101
6 6 0110
7 7 0111
8 8 000
9 9 1001
A 10 1010
B 11 1011
C 12 1100
D 13 1101
E 14 1110
F 15 1111

BPLK 28 DFE1013 (VERSION 3.0)


BINARY NUMBERS AND CODES

2.4.2 DECIMAL TO HEX CONVERSION


Decimal to hex conversion can be done using repeated division by 16 as example below

Example 2.11
Convert 42310 to hex
Quotient reminder
7

10

Therefore

2.4.3 Hex to binary conversion

It is a relatively simple matter to convert a hex number to binary. Each hex digit is
converted to it’s 4 digit equivalent as in Table 2.2 as example below
9 F 2

1001 1111 0010


= 1001 1111 00102

2.4.4 Binary to hex conversion

The binary number is grouped into groups of four bits and each group is converted
to its equivalent hex digit. Zeros are added as need to complete 4 digit group
1110100110 0011 1010 0110
3 A 6
= 3A616

BPLK 29 DFE1013 (VERSION 3.0)


BINARY NUMBERS AND CODES

2.5 UNIT’s SUMMARIZED NOTES


In this unit we have studied that:

1. A binary number is a weighted number in which the weight of each whole number
digit is a positive power of two and the weight of each fractional digit is a negative
power of two.
2. A binary number can be converted to a decimal number by summing the decimal
values of the weights of all the 1s in the binary numbers.
3. The basic rules for binary addition is as follows:

0+0=0
0+1=1
1+0=1
1 + 1 = 10

4. The basic rules for binary subtraction is as follows:

0–0=0
1–1=0
1–0=1
10 – 1 = 1

5. A decimal number is converted to BCD by replacing each decimal digit with the
appropriate 4-bit binary code.
6. The ASCII is a seven-bit alphanumeric code that is widely used in computer
systems for input and output of information.

BPLK 30 DFE1013 (VERSION 3.0)

You might also like