EC 381
Lecture 4
Mohamed Elgalhud
1
Arithmetic Addition
The signed‐magnitude system is used in ordinary
arithmetic, but is awkward when employed in computer
arithmetic because of the separate handling of the sign and
the magnitude. It needs signs comparison and then
performing either addition or subtraction
the signed‐complement system is normally used.
The addition of two signed binary numbers with negative
numbers represented in signed‐2’s-complement form is
obtained from the addition of the two numbers, including
their sign bits. A carry out of the sign‐bit position is
discarded.
2
Sign addition in 2’s complement
Example:
3
Arithmetic Addition
In order to obtain a correct answer, we must ensure that the
result has a sufficient number of bits to accommodate the sum.
When we add two n-bit numbers and end carry occurs:
if the two numbers have different signs, just discard the carry.
If the two numbers have the same sign and the result has the same
sign, just discard the carry.
If the two numbers have the same sign and the result has a different
sign, it is an overflow.
When one performs the addition with paper and pencil, an
overflow is not a problem
Overflow is a problem in computers because the number of bits
that hold a number is finite, and a result that exceeds the finite
value by 1 cannot be accommodated.
4
Binary Codes
How to represent characters, colors, etc?
Define the set of all represented elements
Assign a unique binary code to each element of the set
Given n bits, a binary code is a mapping from the set of
elements to a subset of the 2n binary numbers
Coding Numeric Data (example: coding decimal digits)
Coding must simplify common arithmetic operations
Tight relation to binary numbers
Coding Non-Numeric Data (example: coding colors)
More flexible codes since arithmetic operations are not applied
5
Example of Coding Non-Numeric Data
Suppose we want to code 7 colors of the rainbow
2(n - 1) < M ≤ 2n
7
Decimal Codes
Binary number system is most natural for computers
But people are used to the decimal system
Must convert decimal numbers to binary, do arithmetic on
binary numbers, then convert back to decimal
To simplify conversions, decimal codes can be used
Define a binary code for each decimal digit
Since 10 decimal digits exit, a 4-bit code is used
But a 4-bit code gives 16 unique combinations
10 combinations are used and 6 will be unused
8
Binary Coded Decimal (BCD)
Simplest binary code for decimal digits
Decimal BCD
Only encodes ten digits from 0 to 9 0 0000
BCD is a weighted code 1 0001
2 0010
The weights are 8,4,2,1
3 0011
Same weights as a binary number 4 0100
There are six invalid code words 5 0101
6 0110
1010, 1011, 1100, 1101, 1110, 1111
7 0111
Example on BCD coding: 8 1000
13 (0001 0011)BCD 9 1001
It is important to realize that BCD numbers are decimal numbers
and not binary numbers, although they use bits in their
representation.
9
Binary Codes for Decimal Digits
Any discrete element of information that is distinct among a
group of quantities can be represented with a binary code
These codes represent some type of coded information rather
than binary numbers
Decimal 8,4,2,1 Excess3 8,4,-2,-1 Gray
0 0000 0011 0000 0000
1 0001 0100 0111 0100
2 0010 0101 0110 0101
3 0011 0110 0101 0111
4 0100 0111 0100 0110
5 0101 1000 1011 0010
6 0110 1001 1010 0011
7 0111 1010 1001 0001
8 1000 1011 1000 1001
9 1001 1100 1111 1000
10
BCD Arithmetic
If the binary sum is greater than or equal to 1010, we add
0110 to obtain the correct BCD sum and a carry
Examples
11
BCD Arithmetic
8 1000
+5 +0101
13 1101 is 13 (> 9)
Note that the result is MORE THAN 9, so must be
represented by two digits!
To correct the digit, add 6 (0110)
8 1000
+5 +0101
13 1101 is 13 (> 9)
+0110 so add 6
carry = 1 0011 leaving 3 + cy
0001 | 0011 Final answer (two digits)
If the digit sum is > 9, add one to the next significant digit
12
Gray Code
Gray code represents digital data
that have been converted from
analog data.
As we “counts” up or down in
decimal, the code word for the
Gray code changes in only one bit
position as we go from decimal
digit to digit including from 9 to 0.
eliminates errors during the
transition from one number to the
next
13
Warning: Conversion or Coding?
Do NOT mix up conversion of a decimal number to a
binary number with coding a decimal number with a
BINARY CODE.
14
Character Codes
ASCII
Stands for American Standard Code for Information
Interchange
Represents characters or symbols, such as the letters of the
alphabet
Only 7 bits defined codes 128 characters
15
Printable ASCII Codes
0 1 2 3 4 5 6 7 8 9 A B C D E F
2 space ! " # $ % & ' ( ) * + , - . /
3 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
4 @ A B C D E F G H I J K L M N O
5 P Q R S T U V W X Y Z [ \ ] ^ _
6 ` a b c d e f g h i j k l m n o
7 p q r s t u v w x y z { | } ~ DEL
Examples:
ASCII code for space character = 20 (hex) = 32 (decimal)
ASCII code for 'L' = 4C (hex) = 76 (decimal)
ASCII code for 'a' = 61 (hex) = 97 (decimal)
Control Characters
The first 32 characters of ASCII table are used for control
Control character codes = 00 to 1F (hexadecimal)
Not shown in previous slide
Examples of Control Characters
Character 0 is the NULL character (‘\0’) used to terminate a string
Character 9 is the Horizontal Tab (HT) character (‘\t’)
Character 0A (hex) = 10 (decimal) is the Line Feed (LF) (‘\n’)
Character 0D (hex) = 13 (decimal) is the Carriage Return (CR) (‘\r’)
One control character appears at end of ASCII table
Character 7F (hex) is the Delete (DEL) character
Error-Detecting code
To detect errors in data communication and processing, an
eighth bit is sometimes added to the ASCII character to
indicate its parity.
A parity bit is an extra bit included with a message to
make the total number of 1’s either even or odd.
18
Even Parity
19
Odd Parity
Similar except make the number of 1’s odd
Examples
A (1000001) with odd parity is 11000001
C (1000011) with odd parity is 01000011
20
Detecting Errors
7-bit ASCII character + 1 Parity bit
Sender Receiver
Sent ‘A’ = 01000001, Received ‘A’ = 01000101
22