Chapter 3

You might also like

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

Chapter Three

Data Representation and Computer Arithmetic

• Number Systems and Conversion


• Units of Data Representation
• Coding Methods
• Binary Arithmetic
• Complements
• Fixed and Floating points representation
• (Boolean Algebra) and Logic Circuits
Number systems and conversion

• Number Systems
• Decimal
• Binary
• Octal
• Hexadecimal
• Conversion
Decimal systems
• The decimal system
 Base 10 with ten distinct digits (0, 1, 2, …, 9)
 Any number greater than 9 is represented by
a combination of these digits
 The weight of a digit is based on power of 10
Example:
 The number 81924 is actually the sum of:
(8X104)+(1X103)+(9X102)+(2X101)+(4X100)
Binary systems
Computers use the binary system to store and
compute numbers.
Binary systems
• The binary system (0 & 1)
 The two digits representation is called a binary
system
 Two electrical states: on (1) & off (0)
 The position weights are based on the power of 2
 The various combination of the two digits
representation gives us the final value
Examples :
i) 1011011 in binary = 91 in decimal
ii) 1101.01 in binary = 13.25 in decimal
Binary Fractions
Binary fractions can also be represented:
Position Value: 2-1 2-2 2-3 2-4 2-5 etc.
Fractions: 1/2 1/4 1/8 1/16 1/32

Decimal: .5 .25 .125 .0625 .03125


Binary into Decimal conversion
5th4th3rd2nd1st0th
1 0 1 1 1 12 = (1X25)+(0X24)+(1X23)+(1X22)+(1X21)+(1X20)
= 32+8+4+2+1
= 4710

10110012 = 8910

Exercise :
Convert the following binary numbers into their decimal
equivalent
• 11101002 = (?)10
• 101101.11012 = (?)10
Conversion of Decimal to Binary
Divide by 2 (remainder division) till the dividend is zero and
read remainders in reverse order. The right column shows
result of integer division
(mod 2) 637/2
1 318
Read answer in this 0 159
direction, Write 1 79 63710 = 1001111101 2
answer left to right 1 39
1 19
1 9
1 4
0 2
0 1  Convert 78910 to base 2
1 0
To Binary Fractions - Conversions

 Convert 0.32510 to base 2


10
10
Exercise – Convert ...
Hexa-
Decimal Binary Octal decimal
29.8
101.1101
3.07
DF.E8
Exercise – Convert …
Answers

Hexa-
Decimal Binary Octal decimal
29.8 11101.110011… 35.63… 1D.CC…
5.8125 101.1101 5.64 5.D
3.109375 11.000111 3.07 3.1C
12.5078125 1100.10000010 14.404 C.82
Storage Memory Concepts
• How many bits does a computer use to store an
integer?
– Some models = 32 bits
– Other models = 64 bits
• What if we try to compute or store a larger integer?
– If we try to compute a value larger than the computer
can store, we get an arithmetic overflow error.

14
Representing Unsigned Integers

• How does a 16-bit computer represent the value 14?

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

• What is the largest 16-bit integer?

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

= 1x215 + 1x214 + … + 1x21 + 1x20 = 65,535

If we just add 1 to this number, for example, overflow will occur. We will get a wrong result.
15
Representing Signed Integers
• How does a 16 bit computer represent the value -14?
1 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0

Sign bit. 0: + (positive), 1: - (negative)


• What is the largest 16-bit signed integer?
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

= 1x214 + 1x213 + … + 1x21 + 1x20 = 32,767


• Problem  the value 0 is represented twice!
– Most computers use a different representation, called two’s
complement.
16
Signed-magnitude
representation
• Also called, “sign-and-magnitude representation”
• A number consists of a magnitude and a symbol
representing the sign
• Usually 0 means positive, 1 negative
– Sign bit
– The number is represented with 1 sign bit to the left,
followed by magnitude bits
Machine arithmetic with signed-
magnitude representation
• Takes several steps to add a pair of numbers
– Examine signs of the addends
– If same, add magnitudes and give the result the same
sign as the operands
– If different, must…
• Compare magnitude of the two operands
• Subtract smaller number from larger
• Give the result the sign of the larger magnitude operand
– If magnitudes are equal and sign is different; two
representations of zero problem
• For this reason the signed-magnitude representation is
not as popular as one might think because of its
“naturalness”
Complement number systems
• Negates a number by taking its complement instead of
negating the sign
• Not natural for humans, but better for machine
arithmetic
• Will describe 2 complement number systems
 Radix complement – very popular in real computers
 Must first decide how many bits to represent the number – say n.
 Complement of a number = rn – number
 Example: 2’s Complement
 Diminished radix-complement – not very useful, may skip it
or not spend much time on it
 Example: 1’s Complement: rn - number - 1
Two’s-complement representation
• Just radix-complement when radix = 2
• The most used representation of integers in
computers and other digital arithmetic circuits
• 0 and positive numbers: leftmost bit = 0
• Negative numbers: leftmost bit = 1
• Representation of zero
• i.e. 0 is represented as 0000 using 4-bit binary
sequence.
• To find a number’s 2’s-complement – just flip
all the bits and add 1
Properties of Two’s Complement
Notation
• Relationship between +n and –n.

 0100 +4 00010010 +18

 1100 -4 11101110 -18

21
Two’s Complement Notation with 4-bits
Binary Pattern Value in 2’s complement.
0 1 1 1 7
0 1 1 0 6
0 1 01 5
0 1 0 0 4
0 0 1 1 3
0 0 1 0 2
0 0 0 1 1
0 0 0 0 0
1 1 1 1 -1
1 1 1 0 -2
1 1 0 1 -3
1 1 0 0 -4
1 0 1 1 -5
1 0 1 0 -6
1 0 0 1 -7
1 0 0 0 -8 22
Advantages of Two’s Complement
Notation

• It is easy to add two numbers.


0 0 0 1 +1 1 0 0 0 -8
+ +
0 1 0 1 +5 0 1 0 1 +5
0 1 1 0 +6 1101 -3
• Subtraction is 2’s complement addition
• Multiplication is just a repeated addition
• Division is just a repeated 2’s complement addition
• Two’s complement is widely used in ALU
23
Comparison of decimal and 4-bit numbers
Complements and other Notations
Excess is 24-1 = 8; Thus,
retrieved value=stored value-8

Decimal numbers, their


two’s complements,
ones’ complements,
signed magnitude and
excess 2m-1 binary codes

EXPLAIN
XXXXXXXXXXXXX Existence of two
XXXXXXXXXXXXX zeros!
Two’s-Comp Addition and Subtraction
Rules
• Starting from 1000 (-8) on up, each successive 2’s
comp number all the way to 0111 (+7) can be obtained
by adding 1 to the previous one, ignoring any carries
beyond the 4th bit position
• Since addition is just an extension of ordinary
counting, 2’s comp numbers can be added by ordinary
binary addition!
• No different cases based on operands’ signs!
• Overflow possible
 Occurs if result is out of range
 if operands are the same sign but sum is a different sign of
that of the operands, overflow has occurred.
Storing an integer in two’s complement format:
• Convert the integer to an n-bit binary.
• If it is positive or zero, it is stored as it is. If it is
negative, take the two’s complement and then store it.

Retrieving an integer in two’s complement


format:
• If the leftmost bit is 1, the computer applies the two’s
complement operation to the n-bit binary. If the
leftmost bit is 0, no operation is applied.
• The computer changes the binary to decimal (integer)
and corresponding sign is added.
Example:
Retrieve the integer that is stored as 11100110 in
memory using two’s complement format.
Solution:
The leftmost bit is 1, so the integer is negative. The
integer needs to be two’s complemented before
changing to decimal.
Comparison
XXXXXXXXX
STORING REAL NUMBERS
A number is changed to binary before being stored in
computer memory, as described earlier. There are two issues
that need to be handled:
1. How to store the sign of the number (we already know
this).
2. How to show the (radix) point.

For the (radix) point, computers use two different


representations: fixed-point and floating-point. The first is
used to store a number as an integer, without a fraction part.
The second is used to store a number as a real number, with
a fractional part.
/Radix point

Fixed point representation of integers

An integer is normally stored in memory using


fixed-point representation.
Storing real numbers Continued
A real number is a number with an integral part and a
fractional part. For example, 23.7 is a real number—the
integral part is 23 and the fractional part is 7/10. Although a
fixed-point representation can be used to represent a real
number, the result may not be accurate or it may not have the
required precision. The next two examples explain why.

Real numbers with very large integral parts or very


small fractional parts should not be stored in fixed-
point representation.
Example 1:
In the decimal system, assume that we use a fixed-point
representation with two digits to the right of the decimal point
and fourteen digits to the left of the decimal point, for a total of
sixteen digits. The precision of a real number in this system is
lost if we try to represent a decimal number such as 1.00234: the
system stores the number as 1.00
Example 2:
In the decimal system, assume that we use a fixed-point
representation with six digits to the right of the decimal point and
ten digits to the left of the decimal point, for a total of sixteen
digits. The accuracy of a real number in this system is lost if we try
to represent a decimal number such as 236154302345.00 The
system stores the number as 6154302345.00 The integral part is
much smaller than it should be.
Floating-point representation
The solution for maintaining accuracy or precision is to use
floating-point representation.

A floating point representation of a number is made up of


three parts: a sign, a shifter and a fixed-point number.

Floating-point representation is used in science to represent very small


or very large decimal numbers. In this representation called scientific
notation, the fixed-point section has only one digit to the left of point
and the shifter is the power of 10.
Example:

The following shows the decimal number

7,425,000,000,000,000,000,000.00

in scientific notation (floating-point representation).

The three sections are the sign (+), the shifter (21) and
the fixed-point part (7.425). Note that the shifter is the
exponent.
Some programming languages and calculators shows the
number as +7.425E21
Example:

Show the number −0.0000000000000232

in scientific notation (floating-point representation).

Solution
We use the same approach as in the previous example—we
move the decimal point after the digit 2, as shown below:

The three sections are the sign (-), the shifter (-14) and
the fixed-point part (2.32). Note that the shifter is the
exponent.
Example:

Show the number,


(101001000000000000000000000000000.00)2
in floating-point representation.

Solution
We use the same idea, keeping only one digit to the left of
the radix (decimal) point.
Example:

Show the number


−(0.00000000000000000000000101)2
in floating-point representation.

Solution
We use the same idea, keeping only one digit to the left of
the decimal point.
Normalization

To make the fixed part of the representation uniform, both


the scientific method (for the decimal system) and the
floating-point method (for the binary system) use only one
non-zero digit on the left of the decimal (binary) point. This
is called normalization. In the decimal system this digit can
be 1 to 9, while in the binary system it can only be 1. In the
following, d is a non-zero digit, x is a digit, and y is either 0
or 1.
To store 1000111.01012 in memory, using floating point
representation; First we put it in normalized form
1.0001110101 * 26 , and then store it as shown below

Note that the point and the bit 1 to the left of the
fixed-point section (Mantissa) are not stored; They
are implicit (hidden or not shown).
Excess Notation
 The exponent (shifter), the power that shows how
many bits the decimal point should be moved to the left or
right, is a signed number.
 Although this could have been stored using two’s
complement representation, a new representation, called
the Excess notation, is used instead.
 In the Excess notation, both positive and negative
integers are stored as unsigned integers.
 To represent a positive or negative integer, a positive
integer (called a bias) is added to each number to shift
them uniformly to the non-negative side. The value of this
bias is 2m-1 - 1, where m is the size of memory in bits to
store the exponent.
Example:

We can express sixteen integers in a number system with 4-bit


allocation. By adding seven units to each integer in this range,
we can uniformly translate all integers to the right and make
all of them positive without changing the relative position of
the integers with respect to each other, as shown in the figure.
The new system is referred to as Excess-7, or biased
representation with biasing value of 7.

Shifting in Excess representation


IEEE Standard

IEEE standards for floating-point representation


IEEE Specifications
XXXXXX

Storage of IEEE standard floating point numbers:


1. Store the sign in S (0 or 1).
2. Change the number to binary.
3. Normalize.
4. Find the values of E and M.
5. Concatenate S, E, and M.
Example 1:
Show the Excess_127 (single precision) representation of
the decimal number 5.75
Solution
a. The sign is positive, so S = 0.
b. Decimal to binary transformation: 5.75 = (101.11)2.
c. Normalization: (101.11)2 = (1.0111)2 × 22.
d. E = 2 + 127 = 129 = (10000001)2, M = 0111. We need
to add nineteen zeros at the right of M to make it 23
bits.
e. The representation is shown below:
01110000000000000000000

The number is stored in the computer as


01000000101110000000000000000000
Example 2:
Show the Excess_127 (single precision) representation of
the decimal number –161.875
Solution
a. The sign is negative, so S = 1.
b. Decimal to binary transformation: 161.875=
(10100001.111)2.
c. Normalization: (10100001.111)2 = (1.0100001111)2 × 27.
d. E = 7 + 127 = 134 = (10000110)2 and M =
(0100001111)2.
e. Representation:

The number is stored in the computer as


110000110010000111100000000000000
Example 3:
Show the Excess_127 (single precision) representation of
the decimal number –0.0234375

Solution
a. S = 1 (the number is negative).
b. Decimal to binary transformation: 0.0234375 =
(0.0000011)2.
c. Normalization: (0.0000011)2 = (1.1)2 × 2−6.
d. E = –6 + 127 = 121 = (01111001)2 and M = (1)2.
e. Representation:

The number is stored in the computer as


10111100110000000000000000000000
Retrieving numbers stored in IEEE
standard floating point format:

1. Find the value of S,E, and M.


2. If S=0, set the sign to positive, otherwise set the sign
to negative.
3. Find the shifter (E-127).
4. De-normalize the mantissa.
5. Change the de-normalized number and find the
absolute value (in decimal).
6. Add sign.
Example 4:
The bit pattern (11001010000000000111000100001111)2
is stored in Excess_127 format, in memory. Show the
retrieved value in decimal.

Solution
a. The first bit represents S, the next eight bits, E and the
remaining 23 bits, M.

b. The sign is negative.


c. The shifter = E − 127 = 148 − 127 = 21.
d. This gives us (1.00000000111000100001111)2 × 221
e. The binary number is (1000000001110001000011.11)2
f. The absolute value is 2,104,387.75
g. The number is −2,104,387.75
Example 5

 Single precision
0 10000010 11000000000000000000000

1.112

130 – 127 = 3

0 = positive mantissa

+1.112 x 23 = 1110.02 = 14.010


Exercise
1. Represent +0.8 in the following floating-point
representation:
 1-bit sign
 4-bit exponent
 6-bit normalised mantissa (significand).
2. Convert the value represented back to
decimal.
3. Calculate the relative error of the
representation.

50
Binary Codes
• Computers also use binary numbers (integers) to
represent non-numeric information, such as text or
graphics (images).
• Binary representations of text, (letters, textual
numbers, textual symbols, etc.) are called codes.
• In a binary code, the binary number is a symbol and
does not represent an actual number.
• A code normally cannot be “operated on” in the
usual fashion – mathematical, logical, etc. That is,
one can not usually add up, for example, two binary
codes. It would be like attempting to add text and
graphics!
Character representation- ASCII
o ASCII (American Standard Code for Information
Interchange) - Binary Codes

o It is the scheme used to represent characters.

o Each character is represented using 7-bit binary code.

o If 8-bits are used, the first bit is always set to 0

52
Numeric and Alphabetic Codes
 ASCII code
 American Standard Code for Information
Interchange
 an alphanumeric code
 each character represented by a 7-bit code
 gives 128 possible characters
 codes defined for upper and lower-case alphabetic
characters, digits 0 – 9, punctuation marks and
various non-printing control characters (such as
carriage-return and backspace)
ASCII – examples
Symbol decimal Binary

7 55 00110111
8 56 00111000
9 57 00111001
: 58 00111010
; 59 00111011
< 60 00111100
= 61 00111101
> 62 00111110
? 63 00111111
@ 64 01000000
A 65 01000001
B 66 01000010
C 67 01000011

54
 Representation schemes:
 Top layers - Character string to character sequence:
Write each letter separately, enclosed in quotes. End
string with ‘\0’.
Notation: enclose strings in double quotes
"Hello world"
'H' 'e' 'l' 'l' 'o' ' ' 'W' 'o' 'r' 'l' 'd' '\0'

 Bottom layer - Character to bit-string:


Represent a character using the binary equivalent
according to the ASCII table provided.

"SI"
'S' 'I' '\0'
010100110100100100000000
The colors are intended to help you read it; computers
don’t care that all the bits run together.
56
exercise

 Use the ASCII table to write the ASCII


code for the following string of characters:

 CIS110
 6=2*3
 Write your name in hexadecimal.

57
Unicode - representation
 ASCII code can represent only 128 = 27
characters.
 It only represents the English Alphabet, numeric
characters, few other characters plus some
control characters.
 Unicode is designed to represent the worldwide
printable and non printable characters.
 It uses 32 bits and can represent 232 characters.
 For compatibility, the first 128 Unicode are the
same as that of the ASCII characters.

58
Unicode cont’d..
Let’s consider how Ethiopia’s character sets are
represented
The character set is called Ethiopic
Range: 1200-1378 (in hexadecimal)
Example character sets

59
exercise

 Use UNICODE character representation to


write the following:

ሀ ሁ ሂ ሃ ሄ ህ ሆ

60
Boolean Algebra & Digital Logic

• Boolean algebra is a mathematical system for


the manipulation of variables that can have
one of two values.
– In formal logic, these values are “true” and “false.”
– In digital systems, these values are “on” and “off,”
1 and 0, or “high” and “low.”
• Boolean expressions are created by
performing operations on Boolean variables.
– (Common Boolean operators include AND, OR,
and NOT.)
one unary operator “not” (symbolized by an over bar), two binary
operators "+" and "." 61
Boolean Algebra

• A Boolean operation can be


completely described using a
truth table.
• The truth table for the Boolean
operators AND and OR are
shown at the right.
• The AND operation is also known
as a Boolean product. The OR
operation is the Boolean sum.

62
Boolean Algebra

• The truth table for the


Boolean NOT operator is
shown at the right.
• The NOT operation is most
often designated by an
overbar. It is sometimes
indicated by a prime mark
( ‘ ) or an “elbow” ().

63
Logic Gates

• We have looked at Boolean functions in abstract


terms.
• In this section, we see that Boolean functions are
implemented in digital computer circuits called gates.
• A gate is an electronic device that produces a result
based on two or more input values.
– In reality, gates consist of one to six transistors, but digital
designers think of them as a single unit.
– Integrated circuits contain collections of gates suited to a
particular purpose.

64
Logic Gates

• The three simplest gates are the AND, OR, and NOT
gates.

• They correspond directly to their respective Boolean


operations, as you can see by their truth tables.
65
Logic Gates

• Another very useful gate is the exclusive OR


(XOR) gate.
• The output of the XOR operation is true only when
the values of the inputs differ.

Note the special symbol 


for the XOR operation.

66
Adders

 At the digital logic level, addition is


performed in binary
 Addition operations are carried out
by special circuits called, appropriately,
adders
Adders

 The result of adding


two binary digits could
produce a carry value
 Recall that 1 + 1 = 10
in base two
 A circuit that computes
the sum of two bits
and produces the
correct carry bit is
called a half adder
Adders
 Circuit diagram
representing
a half adder

 Two Boolean
expressions:
sum = A  B
carry = AB
Adders

 A circuit called a full adder takes the


carry-in value into account
Full Adder
 More complex circuits can add digital words

 Similar circuits can be


constructed to perform
subtraction
 More complex arithmetic
(such as multiplication and
division) can be done by
dedicated hardware but is
more often performed using a
microcomputer or complex
logic device
Exercise:
 Construct a digital circuit that takes a 4-bit
binary number as an input and outputs
the 2’s complement of the entered
number.

You might also like