Binary Arithmetic and Codes

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

18

2.7 Binary Arithmetic


Binary arithmetic is essential in all types of digital systems. To understand these
systems, you must know the basics of binary addition, subtraction, multiplication,
and division.

2.7.1 Binary Addition


The four basic rules for adding binary digits (bits) are as follows:
0 + 0 = 0 Sum of 0 with a carry of 0
0 + 1 = 1 Sum of 1 with a carry of 0
1 + 0 = 1 Sum of 1 with a carry of 0
1 + 1 = 10 Sum of 0 with a carry of 1

Notice that the first three rules result in a single bit and in the fourth rule the
addition of two 1s yields a binary two (10). When binary numbers are added, the
last condition creates a sum of 0 in a given column and a carry of 1 over to the
next column to the left, as illustrated in the following examples:

Example: Add 11 + 1

Sol.

Carry Carry
1 1
0 1 1
+ 0 0 1
1 0 0
In the right column, 1 + 1 = 0 with a carry of 1 to the next column to the left. In
the middle column, 1 + 1 + 0 = 0 with a carry of 1 to the next column to the left.
In the left column, 1 + 0 + 0 = 1.
Carry bits
1 + 0 + 0 = 0 1 Sum of 1 with a carry of 0
1 + 1 + 0 = 1 0 Sum of 0 with a carry of 1
1 + 0 + 1 = 1 0 Sum of 0 with a carry of 1
1 + 1 + 1 = 1 1 Sum of 1 with a carry of 1
19

Example: Add 111 + 11

Sol.
Carry Carry
1 1
1 1 1
+ 1 1
1 0 1 0

2.7.2 Binary Subtraction


The four basic rules for subtracting bits are as follows:

0-0=0
1-1=0
1-0=1
10 - 1 = 1 0 - 1 with a borrow of 1

When subtracting numbers, you sometimes have to borrow from the next column
to the left. A borrow is required in binary only when you try to subtract a 1 from
a 0. In this case, when a 1 is borrowed from the next column to the left, a 10 is
created in the column being subtracted, and the last of the four basic rules just
listed must be applied.

Example: Subtract 0112 from 1012.

Sol.
Left column: Middle column:
When a 1 is borrowed, Borrow 1 from next column
a 0 is left, so 0 - 0 = 0. to the left, making a 10 in
this column, then 10 - 1 = 1.
0
1 10 1
- 0 1 1
0 1 0

18/5/2021
20

2.7.3 Binary Multiplication


The four basic rules for multiplying bits are as follows:

0 × 0 = 0, 0 × 1 = 0, 1 × 0 = 0, 1 × 1 = 1
Multiplication is performed with binary numbers in the same manner as with
decimal numbers. It involves forming partial products, shifting each successive
partial product left one place, and then adding all the partial products.

Example: Perform the following binary multiplications:

(a) 112 × 112 (b) 1012 × 1112

Sol.

2.7.4 Binary Division


Division in binary follows the same procedure as division in decimal

Example: Perform the following binary divisions: (a) 1102 ÷ 112 (b) 1102 ÷ 102

Sol.

18/5/2021
21

2.8 Complements of Binary Numbers


The 1’s complement and the 2’s complement of a binary number are important
because they permit the representation of negative numbers. The method of 2’s
complement arithmetic is commonly used in computers to handle negative
numbers.

2.8.1 Finding the 1’s Complement


The 1’s complement of a binary number is found by changing all 1s to 0s
and all 0s to 1s, as illustrated below:

The simplest way to obtain the 1’s complement of a binary number with a
digital circuit is to use parallel inverters (NOT circuits), as shown in Figure
below for an 8-bit binary number.

Example of inverters used to obtain the 1’s complement of a binary number.

2.8.2 Finding the 2’s Complement


The 2’s complement of a binary number is found by adding 1 to the LSB of the
1’s complement. 2’s complement = (1’s complement) + 1

18/5/2021
22

Example: Find the 2’s complement of 10111000 using the alternative method.

Sol.

Related Problem:
Find the 2’s complement of 11000000.

The 2’s complement of a negative binary number can be realized using inverters
and an adder, as indicated in Figure below. This illustrates how an 8-bit number
can be converted to its 2’s complement by first inverting each bit (taking the 1’s
complement) and then adding 1 to the 1’s complement with an adder circuit.

Example of obtaining the 2’s complement of a negative binary number.

To convert from a 1’s or 2’s complement back to the true


(uncomplemented) binary form, use the same two procedures described
previously. To go from the 1’s complement back to true binary, reverse all the
bits. To go from the 2’s complement form back to true binary, take the 1’s
complement of the 2’s complement number and add 1 to the least significant bit.

18/5/2021
23

2.8.3 1’s Complement Form


Positive numbers in 1’s complement form are represented the same way as the
positive sign-magnitude numbers. Negative numbers, however, are the 1’s
complements of the corresponding positive numbers. For example, using eight
bits, the decimal number 225 is expressed as the 1’s complement of +25
(00011001) as
11100110
In the 1’s complement form, a negative number is the 1’s complement of the
corresponding positive number.

Example: Find the 1’s complement of 00010011.

Sol.
Change each bit in a number to get the 1’s complement. The 1’s complement of
a binary number is found by changing all 1s to 0s and all 0s to 1s, as illustrated
below:
0 0 0 1 0 0 1 1 Binary number

↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓

1 1 1 0 1 1 0 0 1's complement

2.8.4 2’s Complement Form


Positive numbers in 2’s complement form are represented the same way as in the
signmagnitude and 1’s complement forms. Negative numbers are the 2’s
complements of the corresponding positive numbers. Again, using eight bits, let’s
take decimal number 225 and express it as the 2’s complement of +25 (00011001).
Inverting each bit and adding 1, you get
-25 = 11100111

In the 2’s complement form, a negative number is the 2’s complement of the
corresponding positive number.

18/5/2021
24

Example: Represent −2 in 2’s complement.

Sol.

2 = 0 0 1 0

1 1 0 1 1’s complement

+ 1 results in 2’s complement

1 1 1 0 = -2

2.9 Binary Coded Decimal (BCD)

Binary coded decimal (BCD) is a way to express each of the decimal digits with a
binary code. There are only ten code groups in the BCD system, so it is very easy
to convert between decimal and BCD. Because we like to read and write in
decimal, the BCD code provides an excellent interface to binary systems.
Examples of such interfaces are keypad inputs and digital readouts.

2.9.1 The 8421 BCD Code


The 8421 code is a type of BCD (binary coded decimal) code. Binary coded
decimal means that each decimal digit, 0 through 9, is represented by a binary code
of four bits. The designation 8421 indicates the binary weights of the four bits (23,
22, 21, 20). The ease of conversion between 8421 code numbers and the familiar
decimal numbers is the main advantage of this code. All you have to remember
are the ten binary combinations that represent the ten decimal digits as shown in
Table below. The 8421 code is the predominant BCD code, and when we refer to
BCD, we always mean the 8421 code unless otherwise stated.

In BCD, 4 bits represent each decimal digit.

18/5/2021
25

2.9.1.1 Invalid Codes


You should realize that, with four bits, sixteen numbers (0000 through 1111) can
be represented but that, in the 8421 code, only ten of these are used. The six code
combinations that are not used—1010, 1011, 1100, 1101, 1110, and 1111—are
invalid in the 8421 BCD code.
To express any decimal number in BCD, simply replace each decimal digit with
the appropriate 4-bit code, as shown by example below.

Example: Convert each of the following decimal numbers to BCD:

(a) 35 (b) 98 (c) 170 (d) 2469

Sol.

It is equally easy to determine a decimal number from a BCD number. Start at the
right-most bit and break the code into groups of four bits. Then write the decimal
digit represented by each 4-bit group.

Example: Convert each of the following BCD codes to decimal:

(a) 10000110 (b) 001101010001 (c) 1001010001110000

Sol.

18/5/2021
26

2.9.1.2 Applications
Digital clocks, digital thermometers, digital meters, and other devices with seven-
segment displays typically use BCD code to simplify the displaying of decimal
numbers. BCD is not as efficient as straight binary for calculations, but it is
particularly useful if only limited processing is required, such as in a digital
thermometer.

2.9.2 BCD Addition


BCD is a numerical code and can be used in arithmetic operations. Addition is the
most important operation because the other three operations (subtraction,
multiplication, and division) can be accomplished by the use of addition. Here is
how to add two BCD numbers:

Step 1: Add the two BCD numbers.


Step 2: If a 4-bit sum is equal to or less than 9, it is a valid BCD number.
Step 3: If a 4-bit sum is greater than 9, or if a carry out of the 4-bit group is
generated, it is an invalid result. Add 6 (0110) to the 4-bit sum in order to skip the
six invalid states and return the code to 8421. If a carry results when 6 is added,
simply add the carry to the next 4-bit group.

Example: Add the following BCD numbers:


(a) 0011 + 0100 (b) 00100011 + 00010101
(c) 10000110 + 00010011 (d) 010001010000 + 010000010111
Sol.

Note that in each case the sum in any 4-bit column does not exceed 9, and the
results are valid BCD numbers.

18/5/2021
27

Example: Add the following BCD numbers:


(a) 1001 + 0100 (b) 1001 + 1001
(c) 00010110 + 00010101 (d) 01100111 + 01010011
Sol.

18/5/2021
28

Advantages of BCD Codes Disadvantages of BCD Codes


1. BCD coding is similar to the 1. Each decimal number requires four
binary equivalent of decimal bits to be represented in the BCD
numbers 0–9. code.
2. BCD has no limitation for number 2. Arithmetic operations in BCD or
size. weighted codes are much
complicated as it deals with more
number of bits and also it has
different set of rules.
3. It is easier to convert decimal 3. BCD is less efficient than binary.
numbers from or to BCD than to
binary form.

2.10 Digital Codes


Many specialized codes are used in digital systems. You have just learned about
the BCD code; now let’s look at a few others. Some codes are strictly numeric,
like BCD, and others are alphanumeric; that is, they are used to represent numbers,
letters, symbols, and instructions. The codes introduced in this section are the Gray
code, the ASCII code, and the Unicode.

2.10.1 The Gray Code


The Gray code is unweighted and is not an arithmetic code; that is, there are no
specific weights assigned to the bit positions. The important feature of the Gray
code is that it exhibits only a single bit change from one code word to the next in
sequence. This property is important in many applications, such as shaft position
encoders, where error susceptibility increases with the number of bit changes
between adjacent numbers in a sequence. The table below is a listing of the 4-bit
Gray code for decimal numbers 0 through 15. Binary numbers are shown in the
table for reference. Like binary numbers, the Gray code can have any number of
bits. Notice the single-bit change between successive Gray code words. For
instance, in going from decimal 3 to decimal 4, the Gray code changes from 0010
to 0110, while the binary code changes from 0011 to 0100, a change of three bits.
The only bit change in the Gray code is in the third bit from the right: the other
bits remain the same.

18/5/2021
29

The single bit change characteristic of the Gray code minimizes the chance for
error.

2.10.1.1 Binary-to-Gray Code Conversion


Conversion between binary code and Gray code is sometimes useful. The
following rules explain how to convert from a binary number to a Gray code word:
1. The most significant bit (left-most) in the Gray code is the same as the
corresponding MSB in the binary number.
2. Going from left to right, add each adjacent pair of binary code bits to get the
next Gray code bit. Discard carries.

Example: The conversion of the binary number 10110 to Gray code is as


Sol.

The Gray code is 11101.

18/5/2021
30

2.10.1.2 Gray-to-Binary Code Conversion


To convert from Gray code to binary, use a similar method; however, there are
some differences. The following rules apply:
1. The most significant bit (left-most) in the binary code is the same as the
corresponding bit in the Gray code.
2. Add each binary code bit generated to the Gray code bit in the next adjacent
position. Discard carries.

Example: The conversion of the Gray code word 11011 to binary is as follows:
Sol.

The binary number is 10010.

Example: (a) Convert the binary number 11000110 to Gray code.


(b) Convert the Gray code 10101111 to binary.
Sol.

18/5/2021
31

2.10.2 Alphanumeric codes


In order to communicate, you need not only numbers but also letters and other
symbols. Alphanumeric codes are codes that represent numbers and alphabetic
characters (letters). At a minimum, an alphanumeric code must represent 10
decimal digits and 26 letters of the alphabet, for a total of 36 items. This number
requires six bits in each code combination because five bits are insufficient
(25 = 32). There are 64 total combinations of six bits, so there are 28 unused code
combinations.
We need spaces, periods, colons, semicolons, question marks, etc. We also
need instructions to tell the receiving system what to do with the information. With
codes that are six bits long, we can handle decimal numbers, the alphabet, and 28
other symbols.

2.10.3 ASCII
ASCII is the abbreviation for American Standard Code for Information
Interchange. Pronounced “askee,” ASCII is a universally accepted alphanumeric
code used in most computers and other electronic equipment. Most computer
keyboards are standardized with ASCII. When you enter a letter, a number, or
control command, the corresponding ASCII code goes into the computer.
ASCII has 128 characters and symbols represented by a 7-bit binary code.
Actually, ASCII can be considered an 8-bit code with the MSB always 0. This
8-bit code is 00 through 7F in hexadecimal. The first 32 ASCII characters are
nongraphic commands that are never printed or displayed and are used only for
control purposes. Examples of the control characters are “null,” “line feed,” “start
of the text,” and “escape.” The other characters are graphic symbols that can be
printed or displayed and include the letters of the alphabet (lowercase and
uppercase), the ten decimal digits, punctuation signs, and other commonly used
symbols.
Info Note: A computer keyboard has a dedicated microprocessor that constantly
scans keyboard circuits to detect when a key has been pressed and released. A
unique scan code is produced by computer software representing that particular
key. The scan code is then converted to an alphanumeric code (ASCII) for use by
the computer.

18/5/2021
32

2.10.3.1 The ASCII Control Characters


The first 32 codes in the (ASCII Table) below represent the control characters.
These are used to allow devices such as a computer and printer to communicate
with each other when passing information and data. The control key function
allows a control character to be entered directly from an ASCII keyboard by
pressing the control key (CTRL) and the corresponding symbol.

18/5/2021
33

Example Use ASCII table to determine the binary ASCII codes that are entered
from the computer’s keyboard when the following C language program statement
is typed in. Also express each code in hexadecimal. If (x > 5)
Sol.
The ASCII code for each symbol is found in ASCII table.

18/5/2021

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy