You are on page 1of 79

University of Bahrain

College of Information Technology

Department of Computer Science

Dr. Abdul Fattah Salman


ITCS 222: Computer Organization

Data Representation
ITCS 222: Computer Organization and
Assembly Language

These slides are based on slides of Prof.


Muhamed Mudawar (KFUPM) and modified
by Dr. Abdul Fattah Salman
Positional Numbering Systems
Different Representations of Natural Numbers

100112 Radix-2 or binary number (positional)


XVI Roman numerals (not positional)
3510 Radix-10 or decimal number (positional)
Fixed-radix positional representation with k digits
Number N in radix r = (dk–1dk–2 . . . d1d0)r
Value = dk–1 × r k–1
+ dk–2 × r k–2
+ … + d 1 × r + d0
(dn-1…d1d0)r = (dn-1x rn-1 )+ (dn-2x rn-2) + …+ (d1x r1) +( d0x r0)
Where d = {0,1,2,…,r-1}

Examples: (10011)2 = 1 × 24 + 0×23 + 0×22 + 1×21 + 1×20


(2043) 5 =
ITCS 222– Computer Organization
2×5 3
+ 0×5 2
+
slide #3
4×5 1
+ 3×5 0
Data Representation
Decimal & Binary Systems
• The base (radix) of the Decimal system is 10 and it uses 10 digits (0 … 9)
• A number in the decimal system is expressed by the following expression:

(dn-1…d1d0)10 = (dn-1x10n-1 )+ (dn-2x10n-2) + …+ (d1x101) +( d0x100)


Where d = {0,1,2,3,4,5,6,7,8,9} = {0..9}

• (59)10 = 5x101 + 9x100 (95)10 = 9x101 + 5x100


• The base (radix) of the Binary system is 2 and it uses 2 digits (0 , 1)
• A number in the Binary system is expressed by the following expression:

(dn-1…d1d0)2 = (dn-1x2n-1 )+ (dn-2x2n-2) + …+ (d1x21) +( d0x20)


Where d = {0, 1}

• (10011)2 = 1 × 24 + 0×23 + 0×22 + 1×21 + 1×20


ITCS 222– Computer Organization slide #4 Data Representation
Octal & Hexadecimal Systems
• The base (radix) of the Octal system 8 and it uses 8 digits (0 … 7)
• A number in the Octal system is expressed by the following expression:
(dn-1…d1d0)8 = (dn-1x8n-1 )+ (dn-2x8n-2) + …+ (d1x81) +( d0x80)
Where d = {0,1, …7}

• (52)8 = 5x81 + 2x80 (25)8 = 2x81 +


5x80
• The base (radix) of the Hexadecimal system 16 and it uses 16 digits (0 …
9,A,B,C,D,E,F)
• A number in the hexadecimal system is expressed by the following
expression:
(dn-1…d1d0)16 = (dn-1x16n-1 )+ (dn-216n-2) + …+ (d1x161) +( d0x160)
Where d = {0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F} = {0..9…A…F}

• ITCS 222– Computer


(5A) 16 = 5x16
Organization
1
+ 10x160 slide #6 (A5)16 = Data Representation
10x16 1
+ 5x160
Binary, Decimal, and Hexadecimal Table
Please Memorize this Table !!!
Binary, Decimal, and Hexadecimal Equivalents

ITCS 222– Computer Organization slide #7 Data Representation


Number Conversions
• Computer hardware understands only zeros and ones. Humans get
used and understand only decimal numbers. So, number conversions
are needed.

• Conversions of number with different bases are divided into 3 types:


1) Decimal  Other bases
• Done using repeated integer division by the new base.
2) Other bases  Decimal
• Done using sum of weights method .
3) Other base 1  Other bases 2
• In general: Done using the two above-mentioned methods
through decimal. Other 1  DEC  Other 2
• In special cases: Done using the above-mentioned two methods
through binary. Other 1  BIN  Other 2
ITCS 222– Computer Organization slide #8 Data Representation
Decimal  Binary Conversion
• A decimal number is converted into binary using repeated
integer division by 2 method. Division process continues
until the quotient becomes zero.
• The binary result is the remainders taken in reverse order.
• Example: (37)10  ( ??? )2

least significant bit

(37)10 = (100101)2

most significant bit

stop when quotient is zero


ITCS 222– Computer Organization slide #9 Data Representation
Examples: Decimal  Binary Conversion
• Divide the number by 2 and get quotient and remainder
• Divide the new quotient by 2 and again get quotient and remainder
• Repeat the previous steps until the quotient becomes 0
• The binary results is the remainder taken in reverse order

(91)10 = ( ? )2

QT RM (24)10 = ( ? )2
91/2 45 1
45/2 22 1 QT RM
22/2 11 0 24/2 12 0
11/2 5 1 12/2 6 0
5/2 2 1 6/2 3 0
2/2 1 0 3/2 1 1
1/2 0 1 1/2 0 1

(91)10 = (1011011)2 (24)10 = (11000)2

ITCS 222– Computer Organization slide #10 Data Representation


Binary  Decimal Conversion
• A binary number is converted to decimal using sum of weights
method (powers of 2).
• Decimal Value = (dn-1  2n-1) + ... + (d1  21) +(d0  20)
• Binary (10011101)2 = 27 + 24 + 23 + 22 + 20 = (157)10

7 6 5 4 3 2 1 0
1 0 0 1 1 1 0 1
27 26 25 24 23 22 21 20

Common
powers of 2
Memorize !!

ITCS 222– Computer Organization slide #11 Data Representation


Binary  Hexadecimal Conversion
 Since each hexadecimal digit is represented by a quadruple (4 bits):
• Divide the binary value into quadruples starting from the right side.
• Replace every quadruple by its equivalent hex digit
 Example: Convert the following 32-bit binary number to hexadecimal

11101011000101101010011110010100
 Solution:

1110 1011 0001 0110 1010 0111 1001 0100

E B 1 6 A 7 9 4
1110 1011 0001 0110 1010 0111 1001 0100

ITCS 222– Computer Organization slide #12 Data Representation


Decimal  Hexadecimal  Decimal
 Repeatedly divide the decimal integer by 16
 The result is the remainders taken in reverse order.
Example: (422)10 = ( ??? )16

least significant digit

most significant digit


stop when quotient is zero
(422)10 = (1A6)16

 HEX  DEC: Multiply each hex digit by its weight.


Value = (dn-1  16n-1) + (dn-2  16n-2) + ... + (d1  16) + d0

 Examples:
(1234)16 = (1163) + (2162) + (3  161) + (4160) = (4660)10
(3BA4)16 = (3163) + (11162) + (1016) + (4160) = (15268)10
ITCS 222– Computer Organization slide #13 Data Representation
Binary  Other Bases  Binary
• Computers use the binary system to represent data. In most cases a
number is represented with 16, 32, 64, or more bits, which is difficult to be
handled by humans.
• To make binary numbers easier to manipulate, we can group the bits of the
number in groups of 2, 3 or 4 bits.
• Binary  ( ??)4 is performed by dividing the bin value into group of 2
bits, starting from the right side.

• (10110100) = ( ??? )4 2 (11)2 = 3 (00)2 = 0

= (10 11 01 00)2 =
(10)2 =2 (01)2 = 1

= (2310)4

ITCS 222– Computer Organization slide #14 Data Representation


Binary  Other Bases  Binary
• Binary  ( ??)8 is performed by dividing the bin value into group of 3
bits, starting from the right side..

(111101011)2 = ( ??? )8 (101)2 = 5

(111 101 011)2


(111)2 = 7 (011)2 = 3

= (753)8
• Binary  ( ??)16 is performed by dividing the bin value into group of 4
bits, starting from the right side.. Letters A to F are used to represent
digits from 10 to 15. (1011)2 =11= B (0100)2 = 4
(10110100)2 = ( ??? )16
(1011 0100)2=
= (B4)
ITCS 222– Computer
16 Organization slide #15 Data Representation
Solved Examples: Sum of Weights Method
3 2 1 0
(1011)2 = (1x2 )+(0x2 )+(1x2 ) +(1x2 ) = 8+0+2+1= (11)10

4 3 2 1 0
(10110)2 = (1x2 )+(0x2 )+(1x2 ) +(1x2 ) +(0x2 ) = 16+0+4+2+0= (22)10

5 4 3 2 1 0
(101100)2=(1x2 )+(0x2 )+(1x2 )+(1x2 )+(0x2 )+(0x2 )=32+0+8+4+0+0=(44)10

2 1 0
(256)8 = (2x8 )+(5x8 )+(6x8 )= 128 + 40 + 6 = (174)10

3 2 1 0
(3256)8 = (3x8 )+((2x8 )+(5x8 )+(6x8 )= 1526 + 128 + 40 + 6 = (1710)10

3 2 1 0
(4207)8 = (4x8 )+((2x8 )+(0x8 )+(7x8 )= 2048 + 128 + 0 + 7 = (2183)10

2 1 0
(2CE)16 = (2x16 )+(12x16 )+(14x16 )= 512 + 192 + 14 = (718)10

3 2 1 0
(2F5B)16 =(2x16 )+(15x16 )+(5x16 )+(11x16 )=8192+3840+80+11= (12123)10
ITCS 222– Computer Organization slide #16 Data Representation
Exercises
Perform the following number conversions:

• (53) = (?)
10 2

• (47) = (?)
10 8

• (124) = (?) 10 16

• (253) = (?) 10 16

• (97) = (?)
10 8

• (145) = (?) 10 16

• (250) = (?) 10 2

• (100) = (?) 10 8

ITCS 222– Computer Organization slide #17 Data Representation


Exercises
Perform the number conversions and fill your answers in the
table

Decimal Octal Hexadecimal Binary


(base 10) (base8) (base 16) (base 2)
179
354
9F
11001011
487
11101010
2F7
276

ITCS 222– Computer Organization slide #18 Data Representation


Formative Homework Exercises
Perform the following number conversions:
• (244)8 = (?)10
• (354)8 = (?)10
• (A4F)16 = (?)10
• (11000111)2 = (?)10
• (10101010)2 = (?)10
• (3DF)16 = (?)10

• (53)10 = (?)2 = (?)8 = (?)16


• (1000111)2 = (?)10 = (?)8 = (?)16
• (10111110)2 = (?)10 = (?)8 = (?)16
• (300)10 = (?)2 = (?)8 = (?)16
• (253)8 = (?)10 = (?)2 = (?)16
• (9C)
ITCS 16 = (?)
222– Computer 2 = (?)8 = (?)10 slide #19
Organization Data Representation
Integer Storage Sizes
Byte 8

Half Word 16
Storage Sizes
Word 32

Double Word 64

Storage Type Unsigned Range Powers of 2


Byte 0 to 255 0 to (28 – 1)
Half Word 0 to 65,535 0 to (216 – 1)
Word 0 to 4,294,967,295 0 to (232 – 1)
Double Word 0 to 18,446,744,073,709,551,615 0 to (264 – 1)

What is the largest 20-bit unsigned integer?


Answer: 220 – 1 = 1,048,575

ITCS 222– Computer Organization slide #20 Data Representation


Addition in any Numbering System
• Addition in any numbering system can be performed by the rules used for
decimal addition, where 10 is replaced by the base of the system (R).
• Rules for addition in the decimal system:
 Begin by adding the least significant digits in the two numbers.
• Divide the resulting sum by 10. Write down the remainder and
carry out the quotient to the left column.
 Repeat the previous step for each subsequent column.
• Example: decimal addition:

3 + 6 = 9  9/10 = 0 + 9/10  write 9 and carry 0


6 4 3
1 8 6+
8 + 4 = 12  12/10 = 1 + 2/10  write 2 and carry 1
8 2 9
1+ 6 + 1 = 8  8/10 = 0 + 8/10  write 8 and carry 0

ITCS 222– Computer Organization slide #21 Data Representation


Binary Addition
• Align the two numbers and start with the least significant bit
(rightmost bit). Shorter value is extended by zeros on the left.
• Add the two bits in each position along with the carry coming
from the previous position.
• For each position, calculate the sum and the carry as follows:
 sum = sum % 2 and carry = sum / 2 to the left position

Binary addition rules carry 1 1 1 1

0 + 0 = 0 and carry is 0 0 0 1 1 0 1 1 0 (54)

0 + 1 = 1 and carry is 0 + 0 0 0 1 1 1 0 1 (29)


1 + 0 = 1 and carry is 0
1 + 1 = 0 and carry 1 0 1 0 1 0 0 1 1 (83)

1 + 1 + 1 = 1 and carry is 1 bit position: 7 6 5 4 3 2 1 0

ITCS 222– Computer Organization slide #22 Data Representation


Binary Addition: Examples
carry 1 1 1 1 1 2 2 2 2 1 1
0 1 1 1 0 1 1 0 (118)
185 = 1 0 1 1 1 0 0 1
+ 0 0 0 1 1 1 0 1 (29)
122 = 0 1 1 1 1 0 1 0
1 0 0 1 0 0 1 1 (147)
100 = 0 1 1 0 0 1 0 0
bit position: 7 6 5 4 3 2 1 0
158 = 1 0 0 1 1 1 1 0

565 1 0 0 0 1 1 0 1 0 1
carry 1 1 1 1
0 0 1 1 1 1 0 1 (61)

+ 0 0 1 1 1 1 1 0 (62)

0 1 1 1 1 0 1 1 (123)
bit position: 7 6 5 4 3 2 1 0

ITCS 222– Computer Organization slide #23 Data Representation


Octal Addition
• Align the two octal numbers.
• Start with the least significant octal digits
• Calculate the sum and the carry for the two octal digits in
each column:
 sum = sum % 8
 carry = sum / 8 to the left position
• Example:
carry: 1 1 1
1 1 3 1 2 4 2 6
+ 6 + 7 = 13
3 6 0 4 6 6 4 7 sum = 13 % 8 = 5
carry = 13 / 8 = 1
4 7 3 6 1 2 7 5
ITCS 222– Computer Organization slide #24 Data Representation
Hexadecimal Addition
• Align the two hexadecimal numbers.
• Start with the least significant hexadecimal digits
• Calculate the sum and the carry for the two hex digits in
each column:
 sum = sum % 16
 carry = sum / 16 to the left position

• Example:
carry: 1 1 1
1 C 3 7 2 8 6 A
+ A + B = 10 + 11 = 21
9 3 9 5 E 8 4 B sum = 21 % 16 = 5
carry = 21 / 16 = 1
A F C D 1 0 B 5
ITCS 222– Computer Organization slide #25 Data Representation
Signed Integers
• There are three ways to represent signed numbers:
 Sign-Magnitude
 Biased
 Complements: (1's complement, 2's complement )
• The range of signed values is divided into 2 equal parts
 First part corresponds to the positive numbers (≥ 0)
 Second part correspond to the negative numbers (< 0)
• In computers: Signed numbers are represented using 2's
complement representation!
 Has many advantages over other representations
 Used widely in processors to represent signed integers

ITCS 222– Computer Organization slide #26 Data Representation


Sign-Magnitude Representation
n-bit number

bit bit bit bit


Sign Bit n-2
...
2 1 0

Magnitude consists of n – 1 bits

• Independent representation of the sign and magnitude


• Leftmost bit is the sign bit: 0 is positive and 1 is negative
• Using n bits, largest represented magnitude = 2n-1 – 1
Sign-magnitude Sign-magnitude
representation of +45 representation of -45
using 8-bits using 8-bits
0 0 1 0 1 1 0 1 1 0 1 0 1 1 0 1

ITCS 222– Computer Organization slide #27 Data Representation


Properties of Sign-Magnitude Representation
• Symmetric range of represented values:
For n-bit number, the range is from -(2n-1 – 1) to +(2n-1 – 1)
For example using 8-bit number, the range is -127 to +127
• Two representations for zero: +0 and -0 NOT Good!
 A 4-bit positive zero is 0 0000
 A 4-bit negative zero is 1 0000
• Two different digital circuits are needed for addition & subtraction: NOT
Good and Expensive!
 For addition: adder, for subtraction: subtractor
• Very complicated algorithms for addition/subtraction
 The two parts (Sign and magnitude) should be processed independently
 Sign bit should be examined to determine addition or subtraction
 Adding two numbers of different signs is converted into subtraction
ITCS 222– Computer Organization slide #28 Data Representation
Sign-Magnitude Addition / Subtraction
Eight cases for Sign-Magnitude Addition / Subtraction

Subtract Magnitudes
Operation ADD
Magnitudes
A >= B A<B

(+A) + (+B) +(A+B)


(+A) + (-B) +(A–B) -(B–A)
(-A) + (+B) -(A–B) +(B–A)
(-A) + (-B) -(A+B)
(+A) – (+B) +(A–B) -(B–A)
(+A) – (-B) +(A+B)
(-A) – (+B) -(A+B)
(-A) – (-B) -(A–B) +(B–A)
ITCS 222– Computer Organization slide #29 Data Representation
1’s Complement Representation
• Given a binary number A
The 1’s complement of A is obtained by inverting each bit in A
~0 = 1 and ~1 = 0
• Example: 1’s complement of (01101001)2 = (10010110)2
• If A consists of n bits then A + (1’s complement of A) = (2n – 1)
= (1...111)2 (all bits are
1's)
1’s complement of A = (2n – 1) – A
• Example: if n = 8 then (28 – 1) = 255 = (11111111) 2

1’s complement of (01101001)2 = (10010110)2


(11111111) 2 – (01101001) 2
ITCS 222– Computer Organization slide=#30
(10010110) 2
Data Representation
Properties of 1's Complement Representation
• Symmetric range of represented values: –(2n-1 – 1) to +(2n-1 – 1)
For example, if n = 8 bits, range is – 127 to +127
• Two representations for zero: +0 and -0 NOT Good!
 1's complement of positive zero is (0…000)2 = (1…111)2
 1's complement of negative zero is (1…000)2 = (0…111)2
• Adding numbers in 1's complement might produce a final carry
that must be added to the result to compute the correct sum

1 1 1 1 1 1
0 1 0 0 0 1 1 1 71 0 0 1 1 0 0 0 1 4
+ + 9
1 1 1 0 1 0 1 0 -21 0 0 0 0 0 0 0 1 +1 =
carry
0 0 1 1 0 0 0 1 49 0 0 1 1 0 0 1 0 5
0
Carry = 1  Increment Result Correct Sum = 50

ITCS 222– Computer Organization slide #31 Data Representation


Two's Complement Representation
 Positive numbers
 Signed value = Unsigned value 8-bit Binary Unsigned Signed
value value value
 Negative numbers 00000000 0 0
 Signed value = Unsigned value – 2n 00000001 1 +1
 n = number of bits 00000010 2 +2
 Other Method . . . . . . . . .

 To obtain the signed value assign a 01111110 126 +126


negative weight to most-significant 01111111 127 +127
bit 10000000 128 -128
10000001 129 -127
1 0 1 1 0 1 0 0 . . . . . . . . .
-128 64 32 16 8 4 2 1 11111110 254 -2

= -128 + 32 + 16 + 4 = -76 11111111 255 -1

ITCS 222– Computer Organization slide #32 Data Representation


Forming the Two's Complement
starting value 00100100 = +36

step1: reverse the bits (1's complement) 11011011


step 2: add 1 to the value from step 1 + 1

sum = 2's complement representation 11011100 = -36

Sum of an integer and its 2's complement must be zero:


00100100 + 11011100 = 00000000 (8-bit sum)  Ignore Carry

• Another way to obtain the 2's complement: Binary Value


least
• Start at the least significant 1 and keep it = 00100 1 00 significant 1

unchanged
• Leave all the 0s to its right unchanged 2's Complement
• Complement all the bits to its left
= 11011 1 00
ITCS 222– Computer Organization slide #33 Data Representation
Properties of 2's Complement Representation
• Range of represented values: -2n-1 to +(2n-1 – 1)
For example, if n = 8 bits, range is -128 to +127
• There is only one zero = (0…000)2 (all bits are zeros)
 1's complement of positive zero is (0…0000)2 = (0 ...0000)2
 1's complement of negative zero is (1…0000)2 = (0 ...0000)2

• The 2’s complement of A is the negative of A


• The sum of A + (2’s complement of A) is always zero
The final carry is ignored
• Consider the 8-bit number A = 001011002 = +44
2’s complement of A = 110101002 = -44

001011002 + 110101002 = 1 00000000 (8-bit


Ignore2final sum
carry = 28is 0)
ITCS 222– Computer Organization slide #34 Data Representation
Values of Different Representations
8-bit Binary Unsigned Sign 1's Complement 2's
Representation Magnitude Value Complement
00000000 0 +0 +0 0
00000001 1 +1 +1 +1
00000010 2 +2 +2 +2
. . . . . . . . . . . . . . .
01111101 125 +125 +125 +125
01111110 126 +126 +126 +126
01111111 127 +127 +127 +127
10000000 128 -0 -127 -128
10000001 129 -1 -126 -127
10000010 130 -2 -125 -126
. . . . . . . . . . . . . . .
11111101 253 -125 -2 -3
11111110 254 -126 -1 -2
11111111 255 -127 -0 -1

ITCS 222– Computer Organization slide #35 Data Representation


Sign Bit
• Left-most bit indicates the sign:
• 1 = negative Sign bit
• 0 = positive
1 1 1 1 0 1 1 0
Negative

0 0 0 0 1 0 1 0 Positive

• For Hexadecimal Numbers, check most significant digit:


― If highest digit is > 7 then value is negative; otherwise it is positive
― Examples: 8A and C5 are negative bytes
B1C42A00 is a negative word (32-bit signed integer)

ITCS 222– Computer Organization slide #36 Data Representation


Two's Complement in other bases
• To form the 2's complement of an octal number: No need to convert it
to binary: Subtract each octal digit from 7 then add 1

• Examples:
2's complement of 3457 = 3457 ; not changed since it is positive
2's complement of 5724 = 2053 + 1 = 2054
2's complement of 77777777 = 00000000 + 1 = 00000001

• To form the 2's complement of a hex number : No need to convert it to


binary: Subtract each hexadecimal digit from 15 then add 1

• Examples:
2's complement of 6A3D = 6A3D ; not changed since it is positive
2's complement of 92F16AC0 = 6D0E953F + 1 = 6D0E9540
2's complement of FFFFFFFF = 00000000 + 1 = 00000001
ITCS 222– Computer Organization slide #37 Data Representation
Sign Extension
Step #1: Move the number into the lower-significant bits
Step #2: Fill all the remaining higher bits with the sign bit
• This ensures that both magnitude and sign are correct
• Examples
1) Sign-Extend 10110011 to 16 bits

10110011 = -77 11111111 10110011 = -77


2) Sign-Extend 01100010 to 16 bits

01100010 = +98 00000000 01100010 = +98

• Infinite 0s can be added to the left of a positive number


• Infinite 1s can be added to the left of a negative number
ITCS 222– Computer Organization slide #38 Data Representation
Binary Addition
• To add two 4-bit signed binary numbers ±A + ±B: represent both
numbers in 2's complement, align them, add the bits in each column, and
ignore the external carry, if any. External carry is the carry out of the
leftmost bits.
1 1 1 1
(+3) 0 0 1 1 (+3) 0 0 1 1
+(+2) 0 0 1 0 +(-2) 1 1 1 0
=(+5) 0 1 0 1 =(+1) 0 0 0 1
External carry

1 1
(-3) 1 1 0 1 (-3) 1 1 0 1
+(+2) 0 0 1 0 +(-2) 1 1 1 0
=(-1) 1 1 1 1 =(-5) 1 0 1 1

ITCS 222– Computer Organization slide #39 Data Representation


Converting Subtraction into Addition
• When subtracting ±A – ±B, convert B to its 2's complement.
 ±A – ±B = A + (2’s complement of B)
• Values to be subtracted must have equal number of bits !!!
Shorter value must be sign-extended.
borrow: 1 1 1 carry: 1 1 1 1

01001101 01001101
– 00111010 + 11000110 (2's complement)
00010011 00010011 (same result)
• The biggest advantage of 2's complement is that the same
adder is used for both addition and subtraction
• Final carry is ignored, because
A + (2's complement of B) = A + (2n – B) = (A – B) + 2n
Final carry = 2n, for n-bit numbers
ITCS 222– Computer Organization slide #40 Data Representation
Binary Subtraction
• To subtract two 4-bit signed binary numbers ±A - ±B: represent both
numbers in 2's complement, align them, add the 1st value with the 2’s
complement of the 2nd value, and ignore the external carry, if any. External
carry is the carry out of the leftmost bits.

(+3) 0 0 1 1 (-3) 1 1 0 1
-(+2) 0 0 1 0 -(+2) 0 0 1 0
0 0 1 1 1 1 0 1
2’s complement 1 1 1 0 2’s complement 1 1 1 0
=(+1) 0 0 0 1 =(-5) 1 0 1 1

(+3) 0 0 1 1 (-3) 1 1 0 1
-(-2) 1 1 1 0 -(-2) 1 1 1 0
0 0 1 1 1 1 0 1
2’s complement 0 0 1 0 2’s complement 0 0 1 0
=(+5) 0 1 0 1 =(-1) 1 1 1 1
ITCS 222– Computer Organization slide #41 Data Representation
Hexadecimal Subtraction

16 + 5 = 21
Borrow: 1 1 1 Carry: 1 1 1 1 1
B14FC675 B14FC675
- +
839EA247 7C615DB9 (2's complement)

2DB1242E 2DB1242E (same result)

• When a borrow is required from the digit to the left, then


Add 16 (decimal) to the current digit's value
• Last carry is ignored

ITCS 222– Computer Organization slide #42 Data Representation


Ranges of Signed Integers
For n-bit signed integers: range is: -2n–1 to (+2n–1 – 1)
 The largest value is positive and equals to (+2n–1 – 1)
 The smallest value is negative and equals to -2n–1
Storage Type Signed Range Powers of 2
Byte –128 to +127 –27 to (+27 – 1)

Half Word –32,768 to +32,767 –215 to (+215 – 1)

Word –2,147,483,648 to +2,147,483,647 –231 to (+231 – 1)

–9,223,372,036,854,775,808 to
Double Word +9,223,372,036,854,775,807
–263 to (+263 – 1)

What is the smallest and largest signed decimal can be stored in a 20-bit ?
Answer: –220-1 to +220-1 – 1

Exercise: What is the range of signed values stored in 24,30,40, 50 bits?


ITCS 222– Computer Organization slide #43 Data Representation
Storage Sizes for Signed/Unsigned values
• For n-bit unsigned integers: range is: 0 to (+2n – 1)
Storage Type Unsigned Range Powers of 2
Byte 0 to 255 0 to (28 – 1)

Half Word 0 to 65,535 0 to (216 – 1)

Word 0 to 4,294,967,295 0 to (232 – 1)

Double Word 0 to 18,446,744,073,709,551,615 0 to (264 – 1)

• For n-bit signed integers: range is: -2n–1 to (+2n–1 – 1)


Storage Type Signed Range Powers of 2
Byte –128 to +127 –27 to (+27 – 1)
Half Word –32,768 to +32,767 –215 to (+215 – 1)
Word –2,147,483,648 to +2,147,483,647 –231 to (+231 – 1)
–9,223,372,036,854,775,808 to
Double Word +9,223,372,036,854,775,807
–263 to (+263 – 1)

ITCS 222– Computer Organization slide #44 Data Representation


Carry and Overflow
• Carry is important when adding or subtracting unsigned
integers.
 Carry indicates that the unsigned result is out of range
• Overflow is important when adding or subtracting signed
integers
 Overflow indicates that the signed result is out of range
• When overflow occur?
 Overflow occurs when adding two numbers of the same sign
produces a sum of the opposite sign.
 Adding two positive numbers and the sum is negative
 Adding two negative numbers and the sum is positive
• Carry and overflow occur because computer use fixed
number of bits used to represent numbers.
ITCS 222– Computer Organization slide #45 Data Representation
Range, Carry, Borrow, and Overflow
• Bits have NO meaning. The same n bits stored in a register can
represent an unsigned or a signed integer.
• Unsigned integers: n-bit representation
Numbers < min Numbers > max

Borrow = 1 Carry = 1
Finite Set of Unsigned Integers
Subtraction Addition

min = 0 max = 2n–1


• Signed integers: n-bit 2's complement representation
Underflow Overflow
Numbers < min Numbers > max

Negative Positive
Finite Set of Signed Integers
Overflow Overflow
min = -2n-1 max = +2n-1–1
ITCS 222– Computer Organization
0
slide #46 Data Representation
Carry and Overflow Examples
• We can have carry without overflow and vice-versa
• Four cases are possible (Examples for adding 8-bit numbers)
1 1 1 1 1 1

0 0 0 0 1 1 1 1 15 0 0 0 0 1 1 1 1 15
+ +
0 0 0 0 1 0 0 0 8 1 1 1 1 1 0 0 0 248 (-8)

0 0 0 1 0 1 1 1 23 0 0 0 0 0 1 1 1 7

Carry = 0 Overflow = 0 Carry = 1 Overflow = 0

1 1 1 1

0 1 0 0 1 1 1 1 79 1 1 0 1 1 0 1 0 218 (-38)
+ +
0 1 0 0 0 0 0 0 64 1 0 0 1 1 1 0 1 157 (-99)

1 0 0 0 1 1 1 1 143 0 1 1 1 0 1 1 1 119
(-113)
Carry = 0 Overflow = 1 Carry = 1 Overflow = 1

ITCS 222– Computer Organization slide #47 Data Representation


The World is Not Just Integers
• Programming languages support numbers with fractions called
real or floating-point numbers
 Examples:
3.14159265… (π)
2.71828… (e)
9.1 × 10–31 (weight of electron in kg)
8.64 × 1013 (nanoseconds in a day)
The last two numbers are very small/large: cannot fit in 32-bit registers
• We use a scientific notation to represent
 Very small numbers (e.g. 9.1 × 10–31)
 Very large numbers (e.g. 8.64 × 1013)
 Scientific notation: ± d . fraction × 10 ± exponent

ITCS 222– Computer Organization slide #48 Data Representation


Floating-Point Numbers
• Examples of floating-point numbers in base 10
-5.341×103 , 2.013×10–1
decimal point
• Examples of floating-point numbers in base 2
-1.00101×223 , 1.101101×2–3
binary point
 Exponents are kept in decimal for clarity
• Floating-point numbers must be normalized:
 Exactly one non-zero digit should appear left to the point
o This digit must be from 1 to 9 in a decimal number
o This digit should be 1 In a binary number

 Examples of normalized: -5.341×103 and 1.101101×2–3


 Examples of NOT normalized: -0.05341×105 and 1101.101×2–6

ITCS 222– Computer Organization slide #49 Data Representation


Representation of Numbers with Fractions
• A general form of a number Nr in radix r with a fractional part is:
Nr = dn-1dn-2 … d1d0 . d-1 d-2 … d-m+1 d-m
0 ≤ di < r
Integer Part Fractional Part
Radix Point
• The value of a number Nr consisting of integer and fractional
parts is calculated as shown below:
Nr = dn-1 × rn-1 + … + d1 × r + d0 +
d-1 × r -1
+ d-2 × r -2
… + d-m × r –m

i = n-1 j = -1
Nr = åd
i = 0
i × ri + åd
j = -m
j × rj

ITCS 222– Computer Organization slide #50 Data Representation


Examples of Numbers with Fractions

• (2409.87)10 = 2×103 + 4×102 + 9 + 8×10-1 + 7×10-2

• (1101.1001)2 = 23 + 22 + 20 + 2-1 + 2-4 = 13.5625

• (703.64)8 = 7×82 + 3 + 6×8-1 + 4×8-2 = 451.8125

• (A1F.8)16 = 10×162 + 16 + 15 + 8×16-1 = 2591.5

• (423.1)5 = 4×52 + 2×5 + 3 + 5-1 = 113.2

• (263.5)6 Digit 6 is NOT allowed in radix 6 system


ITCS 222– Computer Organization slide #51 Data Representation
Converting Decimal Fraction to Binary
• Question: Convert N = 0.6875 to Radix 2
• Solution: Multiply N by 2 repeatedly & collect integer bits
Multiplication New Fraction Bit
0.6875 × 2 = 1.375 0.375 1 First fraction bit
0.375 × 2 = 0.75 0.75 0
0.75 × 2 = 1.5 0.5 1
0.5 × 2 = 1.0 0.0 1 Last fraction bit

• Stop when new fraction = 0.0, or when enough fraction


bits are obtained
• Therefore, N = 0.6875 = (0.1011)2
• Check (0.1011)2 = 2-1 + 2-3 + 2-4 = 0.6875
ITCS 222– Computer Organization slide #52 Data Representation
Converting Fraction to any Radix r
• To convert fraction N to any radix r
Nr = (0. d-1 d-2 … d-m)r = d-1 × r -1 + d-2 × r -2 … + d-m × r –m

• Multiply Nr by r to obtain d-1


Nr × r = d-1 + d-2 × r -1 … + d-m × r –m+1
 The integer part is the digit d-1 in radix r
 The new fraction is d-2 × r -1 … + d-m × r –m+1

• Repeat multiplying the new fractions by r to obtain d-2 d-3 ...


• Stop when new fraction becomes 0.0 or enough fraction digits
are obtained.
• The result is a fraction consisting of the integer digits taken in
normal order.
ITCS 222– Computer Organization slide #53 Data Representation
More Conversion Examples
• Question: Convert N = (139.6875)10  ( ??? )8
• The integer and fraction parts are converted separately
• Solution: N = 139 + 0.6875 (split integer from fraction)

Division Quotient Remainder Multiplication New Fraction Digit


139 / 8 17 3 0.6875 × 8 = 5.5 0.5 5
17 / 8 2 1 0.5 × 8 = 4.0 0.0 4
2/8 0 2

• Therefore, (139)10 = (213)8 and (0.6875)10 = (0.54)8


• Now, join the integer and fraction parts with radix point
N = (139.6875)10 = (213.54)8

ITCS 222– Computer Organization slide #54 Data Representation


Conversion Procedure to Radix r
• To convert decimal number N (with fraction) to any radix r
• Convert the integer part
 Repeatedly divide the number N (resulted quotient) by the radix r
and save the resulted remainders.
 If radix r > 10, then convert remainders > 10 to digits A, B, … etc.
 The integer part in radix r are the resulted remainders taken in
reverse order.

• Convert the fractional part


 Repeatedly multiply the fraction of N ( resulted new fraction) by
the radix r and save the resulted integer digits.
 If the radix r > 10, then convert remainders > 10 to A, B, … etc.
 The fraction part in radix r is the integer digits taken in order.

• Join the integer and fraction parts together with the radix point
ITCS 222– Computer Organization slide #55 Data Representation
Simplified Conversions
 Converting real numbers between Binary, Octal, and
Hexadecimal can be simplified since the radix is power of 2.
 Starting at the radix point, the integer part is converted
from right to left and the fractional part is converted from
left to right
 Group 4 bits into a hex digit or 3 bits into an octal digit
integer: right to left fraction: left to right
7 2 6 1 3 . 2 4 7 4 5 2 Octal
111010110001011.01010011110010101 Binary
7 5 8 B . 5 3 C A 8 Hexadecimal
 Use binary to convert between octal and hexadecimal

ITCS 222– Computer Organization slide #56 Data Representation


Important Properties of Fractions
• How many fractional values exist with m fraction bits?
2m fractions, because each fraction bit can be 0 or 1
• What is the largest fraction value if m bits are used?
Largest fraction value = 2-1 + 2-2 + … + 2-m = 1 – 2-m
Because if you add 2-m to largest fraction you obtain 1
• In general, what is the largest fraction value if m fraction digits
are used in radix r?
Largest fraction value = r -1
+ r -2
+ … + r -m
= 1 – r -m

For decimal, largest fraction value = 1 – 10-m


For hexadecimal, largest fraction value = 1 – 16-m

ITCS 222– Computer Organization slide #57 Data Representation


Floating-Point Representation
In scientific notation: ± d . fraction × 10 ± exponent

The representation of a floating-point number consists of the triple:


 Sign bit (0 for positive value and 1 for negative value)
 Representation is called sign and magnitude
 Exponent field (signed value)
 Very large numbers have large positive exponents
 Very small close-to-zero numbers have negative exponents
 More bits in exponent field increases the range of values
 Fraction field (fraction right to the binary point)
 More bits in fraction field improves the precision (accuracy) of FP numbers

S Exponent Fraction
ITCS 222– Computer Organization slide #58 Data Representation
IEEE 754 Floating-Point Standard
• Used virtually in every computer invented since 1980
 Simplifies porting of floating-point numbers among architectures
 Unifies the development of floating-point algorithms
 Increases the accuracy of floating-point numbers
• Single Precision Floating Point Numbers (32 bits)
 1-bit sign + 8-bit exponent + 23-bit fraction

S Exponent8 Fraction23
• Double Precision Floating Point Numbers (64 bits)
 1-bit sign + 11-bit exponent + 52-bit fraction

S Exponent11 Fraction52
(continued)

ITCS 222– Computer Organization slide #59 Data Representation


Normalized Floating Point Numbers
• For a normalized floating point number (S, E, F)
S E F = f1 f2 f3 f4 …

• Significand is equal to (1.F)2 = (1.f1f2f3f4…)2


 IEEE 754 assumes hidden 1. (not stored) for normalized numbers
 Significand is 1 bit longer than fraction
• Value of a Normalized Floating Point Number:
 (1.F)2 × 2exponent_value
 (1.f1f2f3f4 …)2 × 2exponent_value
 (1 + f1×2-1 + f2×2-2 + f3×2-3 + f4×2-4 …)2 × 2exponent_value

• S = 0 for positive numbers, S = 1 for negative numbers


ITCS 222– Computer Organization slide #60 Data Representation
Biased Exponent Representation
• How to represent a signed exponent? Three choices are …
 Sign and magnitude representation for the exponent
 Two’s complement representation
 Biased representation
• IEEE 754 uses biased representation for the exponent
 Biased exponent = True exponent + Bias (Bias is a constant)
• For single precision (SP), the exponent field is 8 bits
 BE can be in the range 0 to 255 (00000000 to 11111111)
 BE = 0 and BE = 255 are NOT used and reserved for special cases
 BE = 1 to 254 are used for normalized floating point numbers

Bias = 127 (half of 254)


True exponent = BE – 127 Range: -126 to
+127
ITCS 222– Computer Organization slide #61 Data Representation
Biased Exponent – Cont’d
• For double precision (DP), the exponent field is 11 bits
 BE can be in the range 0 to 2047
 BE = 0 and BE = 2047 are NOT used and reserved for special cases
 BE = 1 to 2046 are used for normalized floating point numbers

Bias = 1023 (half of 2046)


True exponent (TE) = BE – 1023 Range: -1022 to
+1023
• Value of a Normalized Floating Point Number is
 (1.F)2 × 2(TE-Bias)
 (1.f1f2f3f4 …)2 × 2(TE-Bias)
 (1 + f1×2-1 + f2×2-2 + f3×2-3 + f4×2-4 …)2 × 2(TE-
Bias)

• ITCS 222–SComputer
= 0 forOrganization
positive numbers,
slide #62 S = 1 for negative
Data Representation
Example: Decimal to IEEE single-precision
• Convert –0.8125 to single and double-precision floating-point
• Solution:
 Fraction bits can be obtained using multiplication by 2
 0.8125 × 2= 1.625
 0.625 × 2 = 1.25
 0.25 × 2 = 0.5 0.8125 = (0.1101)2
 0.5 × 2 = 1.0
 Stop when fractional part is 0, or after computing all required fraction bits
 Fraction = (0.1101)2 = (1.101)2 × 2 –1
(Normalized)
 Exponent = –1 + Bias = 126 (single precision) and 1022 (double)

10111111010100000000000000000000 Single Precision

10111111111010100000000000000000
Double Precision
00000000000000000000000000000000
ITCS 222– Computer Organization slide #63 Data Representation
Example: Decimal to IEEE single-precision
Convert -23.375 to into the IEEE single-precision.
Solution:
1) Integer part is converted by repeated division by 2;
2) Fractional part is converted by repeated multiplication by 2:
23.375 = 10111.011
3) Normalize the resulting binary value:
10111.011  1.0111011000*24
4) Calculate the exponent:
Biased Exponent = 4 + 127 = 131  10000011
5) Answer in binary =
1 100 00011 011 1011 0000 0000 0000 0000
6) Answer in hexadecimal =
0xC1BB 0000
ITCS 222– Computer Organization slide #64 Data Representation
Example: IEEE single-precision to Decimal
Convert the IEEE single-precision 0xC2B56000 to into decimal.

Solution:
1) Convert the hexadecimal value into binary and split it into SEF parts
0xC2B56000 = 1 10000101 011 0101 0110 0000 0000 0000

2) Calculate the decimal value of the true exponent


10000101 = 133  133 – 127 = 6

3) Compose the binary value: SEF (Don’t forget the hidden 1)


-(1.01101010110000000000000)*26

= -1011010.10110000000000000

4) Convert the binary value into decimal


-1011010.10110000000000000  -90.6875
ITCS 222– Computer Organization slide #65 Data Representation
Examples of Single Precision Float
• What is the decimal value of this single precision FP?
10111110001000000000000000000000

• Solution:
 Sign = 1 is negative
 E = (01111100)2 = 124; E – bias = 124 – 127 = –3
 Significand=(1.0100 ... 0)2 = 1 + 2-2 = 1.25 (1. is implicit)
 Value in decimal = –1.25 × 2–3 = –0.15625
• What is the decimal value of this single precision FP?
01000001001001100000000000000000
• Solution: implicit
 Value in decimal = +(1.01001100 ... 0)2 × 2130–127
= (1.01001100 ... 0)2 × 23
= (1010.01100 ... 0)2 = 10.375
ITCS 222– Computer Organization slide #66 Data Representation
Examples of Double Precision Float

• What is the decimal value of this double precision float ?


0 1 0 0 0 0 0 0 0 1 0 1 0 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

• Solution:
 Value of exponent = (10000000101)2 – Bias = 1029 – 1023 = 6
 Value of double = (1.00101010 ... 0)2 × 26 (1. is implicit)
= (1001010.10 ... 0)2 = 74.5

• What is the decimal value of this double precision float ?


1 0 1 1 1 1 1 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
• Do it yourself! (answer should be –1.5 × 2–7 = –0.01171875)

ITCS 222– Computer Organization slide #67 Data Representation


Largest Normalized Float Value
• What is the largest normalized FP? It is a value with largest
exponent 11111110 and largest fraction 0.11 ... 11
• Solution for single precision:
0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 E – bias = 254 – 127 = +127 (largest exponent for Single Precision)
 Significand = (1.111 … 1)2 = 1.99999988 = almost 2
 Value in decimal ≈ 2 × 2+127 ≈ 2+128 ≈ 3.4028 … × 10+38
• Solution for double precision: It is a value with largest
exponent 11111111110 and largest fraction 0.11 ... 11
0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 Value in decimal ≈ 2 × 2+1023 ≈ 2+1024 ≈ 1.79769 … × 10+308
• Overflow: exponent is too large to fit in the exponent field
ITCS 222– Computer Organization slide #68 Data Representation
Smallest Normalized Float Value
• What is the smallest (in absolute value) normalized float?
• Solution for single precision:
00000000100000000000000000000000
 Exponent – bias = 1 – 127 = -126 (smallest exponent for SP)
 Significand = (1.000 … 0)2 = 1
 Value in decimal = 1 × 2-126 = 1.17549 … × 10-38
• Solution for double precision:
00000000000100000000000000000000
00000000000000000000000000000000
 Value in decimal = 1 × 2-1022 = 2.22507 … × 10-308
• Underflow: exponent is too small to fit in exponent field

ITCS 222– Computer Organization slide #69 Data Representation


Minimum Number of Bits Required

• Given a set of M elements to be represented by a binary


code, the minimum number of bits, n, should satisfy:

2(n - 1)
< M ≤ 2n

n = log2 M where x , called the ceiling function, is the


integer greater than or equal to x

• How many bits are required to represent 10 decimal digits


with a binary code?

• Answer: log2 10 = 4 bits can represent 10 decimal digits

ITCS 222– Computer Organization slide #70 Data Representation


Adder/Subtractor for 2's Complement
• Same adder is used to compute: (A + B) or (A – B)
• Subtraction (A – B) is computed as: A + (2's complement of B)
2's complement of B = (1's complement of B) + 1
• Two operations: OP = 0 (ADD), OP = 1 (SUBTRACT)
B [n-1:0] OP OP = 0 (ADD)
A [n-1:0] n B XOR 0 = B
n XOR
n n-bit input S=A+B+0=A+B
gates
vectors n
OP = 1 (SUBTRACT)
c0
cn n-bit Adder B XOR 1 = 1's complement of B
S = A + (1's complement of B) + 1
n n-bit output S = A + (2's complement of B)
S [n-1:0] vector S=A–B
ITCS 222– Computer Organization slide #71 Data Representation
Decimal Codes
• Binary number system is most natural for computers
• But people are used to the decimal number 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 exist, a 4-bit code is used
 But a 4-bit code gives 16 unique combinations
 10 combinations are used and 6 are NOT unused

ITCS 222– Computer Organization slide #72 Data Representation


Packed Binary Coded Decimal (BCD)
• BCD is the simplest binary code for decimal digits
• BCD is a weighted code with 8,4,2,1 weights Decimal BCD
• Same weights as a binary number for ten digits from 0 0 0000
to 9 and six invalid code values 1 0001
2 0010
1010, 1011, 1100, 1101, 1110, 1111
3 0011
• Example on BCD coding: 63  (0110 0011)BCD 4 0100
5 0101
• Warning: Do NOT mix up conversion of a decimal 6 0110
number to a binary number with coding a decimal number 7 0111
with a binary code 8 1000
This is conversion 9 1001
 5310 = (110101)2
1010
 5310  (0101 0011)BCD This is coding Unused ···
1111
• In general, coding requires more bits than conversion
• A number with n decimal digits is coded with 4n bits in BCD
ITCS 222– Computer Organization slide #73 Data Representation
Character Storage
• I/O and secondary storage devices use only characters.
• Computers use various character standards (sets):
1) Standard ASCII: 7-bit character codes (0 – 127)
2) Extended ASCII: 8-bit character codes (0 – 255)
3) Unicode: 16-bit character codes (0 – 65,535)
 Unicode standard represents a universal character set
 Defines codes for characters used in all major languages
 Each character is encoded as 16 bits
4) UTF-8 (Unicode Transformation Format): variable-length
encoding used in HTML
 Encodes all Unicode characters
 Uses 1 byte for ASCII, but multiple bytes for other characters
• Null-terminated String
 Array of characters followed by a NULL character
ITCS 222– Computer Organization slide #74 Data Representation
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)
• ASCII code for ‘A' = 41 (hex) = 65 (decimal)
• ASCII code for “Abt 459” is 41 62 74 20 34 35 39
ITCS 222– Computer Organization slide #75 Data Representation
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  used to terminate a string
 Character 9 is the Horizontal Tab (HT) character
 Character 0A (hex) = 10 (decimal) is the Line Feed (LF)
 Character 0D (hex) = 13 (decimal) is the Carriage Return (CR)
 The LF and CR characters are used together
 They advance the cursor to the beginning of next line
• One control character appears at end of ASCII table
 Character 7F (hex) is the Delete (DEL) character

ITCS 222– Computer Organization slide #76 Data Representation


Parity Bit & Error Detection Codes
• Binary data are typically transmitted between computers
• Because of noise, a corrupted bit will change value
• To detect errors, extra bits are added to each data value
• Parity bit: is used to make the number of 1’s odd or even
• Even parity: number of 1’s in the transmitted value is even
• Odd parity: number of 1’s in the transmitted value is odd

7-bit ASCII Character With Even Parity With Odd Parity


‘A’ = 1000001 0 1000001 1 1000001
‘T’ = 1010100 1 1010100 0 1010100

ITCS 222– Computer Organization slide #77 Data Representation


Detecting Errors

7-bit ASCII character + 1 Parity bit


Sender Receiver
Sent ‘A’ = 01000001, Received ‘A’ = 01000101

• Suppose we are transmitting 7-bit ASCII characters


• A parity bit is added to each character to make it 8 bits
• Parity can detect all single-bit errors
 If even parity is used and a single bit changes, it will change the parity to
odd, which will be detected at the receiver end
 The receiver end can detect the error, but cannot correct it because it
does not know which bit is erroneous
• Can also detect some multiple-bit errors
 Error in an odd number of bits

ITCS 222– Computer Organization slide #78 Data Representation


Other Decimal Codes (optional)
• BCD, 5421, 2421, and 84-2-1 are weighted codes
• Excess-3 is not a weighted code
• 2421, 84-2-1, and Excess-3 are self complementary codes
BCD 5421 2421 8 4 -2 -1 Excess-3
Decimal 8421 code code code code
0 0000 0000 0000 0000 0011
1 0001 0001 0001 0111 0100
2 0010 0010 0010 0110 0101
3 0011 0011 0011 0101 0110
4 0100 0100 0100 0100 0111
5 0101 1000 1011 1011 1000
6 0110 1001 1100 1010 1001
7 0111 1010 1101 1001 1010
8 1000 1011 1110 1000 1011
9 1001 1100 1111 1111 1100
Unused ··· ··· ··· ··· ···
ITCS 222– Computer Organization slide #79 Data Representation
Gray Code (optional)
• As we count up/down using binary codes, the number of
bits that change from one binary value to the next varies.
000 → 001 (1-bit change) Digit Binary Gray Code
001 → 010 (2-bit change) 0 000 000
1 001 001
011 → 100 (3-bit change) 2 010 011
3 011 010
• Gray code: only 1 bit changes 4 100 110
5 101 111
as we count up/down. 6 110 101
7 111 100
• Binary reflected code
• Gray code can be used in low-power logic circuits that
count up/down, because only 1 bit changes per count

ITCS 222– Computer Organization slide #80 Data Representation

You might also like