Binary Arithmetic and Codes
Binary Arithmetic and Codes
Binary Arithmetic and Codes
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
Sol.
Carry Carry
1 1
1 1 1
+ 1 1
1 0 1 0
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.
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
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.
Sol.
Example: Perform the following binary divisions: (a) 1102 ÷ 112 (b) 1102 ÷ 102
Sol.
18/5/2021
21
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.
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.
18/5/2021
23
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
In the 2’s complement form, a negative number is the 2’s complement of the
corresponding positive number.
18/5/2021
24
Sol.
2 = 0 0 1 0
1 1 0 1 1’s complement
1 1 1 0 = -2
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.
18/5/2021
25
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.
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.
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
18/5/2021
28
18/5/2021
29
The single bit change characteristic of the Gray code minimizes the chance for
error.
18/5/2021
30
Example: The conversion of the Gray code word 11011 to binary is as follows:
Sol.
18/5/2021
31
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
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