0% found this document useful (0 votes)
30 views

Chapter 2: Data Representation

1) Computers store information using binary digits (bits) represented as 1s and 0s. 2) Decimal numbers can be converted to binary using division to determine the place values for each bit position. 3) Signed integers add a sign bit to indicate positive or negative numbers, but this introduces challenges for representation and arithmetic operations.

Uploaded by

saad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Chapter 2: Data Representation

1) Computers store information using binary digits (bits) represented as 1s and 0s. 2) Decimal numbers can be converted to binary using division to determine the place values for each bit position. 3) Signed integers add a sign bit to indicate positive or negative numbers, but this introduces challenges for representation and arithmetic operations.

Uploaded by

saad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

Chapter 2: Data Representation

• In chapter 1, we briefly saw that computer technology is


based on integrated circuits (IC), which themselves are
based on transistors
– The idea is that ICs store circuits that operate on electrical
current by either letting current pass through, or blocking the
current
• So, we consider every circuit in the computer to be in a state of on or off
(1 or 0)
• Because of this, the computer can store information in binary form
• But we want to store information from the real world, information that
consists of numbers, names, sounds, pictures, instructions
– So we need to create a representation for these types of
information using nothing but 0s and 1s
• That’s the topic for this chapter
Numbering Systems
• In mathematics, there are many different numbering
systems, we are used to decimal (base 10)
– In a numbering system base k (also known as radix k), the digits
available are 0..k-1 (so in base 10, we use digits 0-9)
– Each digit represents a different power of the base
• For instance, 572 in decimal has 5 100s, 7 10s and 2 1s, so we have 100s
column, 10s column 1s column
• If the value was in octal (base 8), it would instead have 5 64s (8 2), 7 8s and
2 1s
• In base 2, our digits are only 1 and 0 and our powers are all powers of 2 (1,
2, 4, 8, 16, 32, etc)
– NOTE: we denote a value’s numbering system by placing a subscript of the
base after the number as in 5728 or 57210, however, if a number is omitted, it is
assumed to be base 10
» For this course, we will also omit the 2 for base 2 numbers for convenience
– So we can store decimal numbers in any numbering system
Conversions
• There is a simple formula to convert a number from a
given base into base 10:
– abcde = a * e3 + b * e2 + c * e1 + d * e0
– Note that e0 = 1, so the rightmost column will always be the 1s
column no matter what the base is
• Example: 71638 = 7 * 83 + 1 * 82 + 6 * 81 + 3 * 80 = 369910
• To convert from base 10 to another base, e:
// assume value is the value to be converted Example: 3699 to base 8
sum = 0; sum = 0 * 10 + 3699 / 8^3 = 7
for(j=numColumns(value) – 1; j>= 0; j--) value = 3699 – 7 * 8^3 = 115
{ sum = 7 * 10 + 115 / 8^2 = 71
divisor = e^j; value = 115 – 1 * 8^2 = 51
temp = value / divisor; sum = 71 * 10 + 51 / 8^1 = 716
sum = sum * 10 + temp; value = 51 – 6 * 8^1 = 3
value = value – temp * divisor; sum = 716 * 10 + 3 / 8^0 = 7163
} value = 3 – 3 * 8^0 = 0
Binary
• In CS we concentrate on binary (base 2)
– Why?
• Because digital components (from which the
computer is built) can be in one of two states
– on or off
• We use 1 and 0 to represent these two states
– we want to store/manipulate bits (binary digits)
– We want to develop a method for
representing information in binary
• numbers (positive, negative, integer, floating
point, fraction), strings of characters, booleans,
images, sounds, programming instructions
– For unsigned integer values, we can store
them directly using binary
Some useful powers
• we convert from one to the other using the of 2 – these illustrate
conversion algorithms on the previous slide where the values of each
base = 2 column (1, 2, 4, 8,…)
Binary Conversions
• We can simplify the more general base e
conversions when dealing with decimal and binary
– Convert from binary to decimal
• For each 1 in the binary number, add that column’s power of
2
– e.g., 11001101 = 128 + 64 + 8 + 4 + 2 = 206
– Convert from decimal to binary
– Two approaches (see next slide)
• Approach 1: divide number by 2, recording quotient and
remainder, continue to divide quotient by 2 until you reach 0,
binary value is the remainder written backward
• Approach 2: find all powers of 2 that make up the number,
for each power of 2, place a 1 in that corresponding column,
otherwise 0
Decimal  Binary
Convert 91 to binary: Convert 91 to binary:

91 / 2 = 45, remainder 1 Largest power of 2: 64


45 / 2 = 22, remainder 1 91 – 64 = 27
22 / 2 = 11, remainder 0 Largest power of 2: 16
11 / 2 = 5, remainder 1 27 – 16 = 11
5 / 2 = 2, remainder 1 Largest power of 2: 8
2 / 2 = 1, remainder 0 11 – 8 = 3
1 / 2 = 0, remainder 1 Largest power of 2: 2
3–2=1
91 = 10110112 Largest power of 2: 1
1–1=0
We can test this:
64 + 16 + 8 + 2 + 1 = 10110112
1011011 = 64 + 16 + 8 + 2 + 1 = 91
Abbreviations
• We can’t store much in 1 bit (0 or 1) so we group bits
together into larger units
– 1 byte = 8 bits
– 1 word = 4 bytes (usually)
– But we can’t store much in 1-4 bytes either, so we refer to
larger storage capacities using abbreviations as shown below
• notice how each unit is 2power of 10
• on the right we see similar abbreviations for time units
Some Decimal and Binary Numbers
• The table to the left shows for the
first 16 decimal values, what the
corresponding binary values are
– Can you expand this table to the first
32 decimal values using 5 bits?
– For convenience
• we often group bits into 3s and write the
value in octal
• or in groups of 4 bits and write the value
in hexadecimal (base 16)
– In hexadecimal, since we cannot write
numbers 10-16 as single digits, we use
the letters A – F
• The number FA316 is F (15) in the 162
column, A (10) in the 161 column and 3 in
the 160 column
Binary Coded Decimal (BCD)
• Some programming languages
provide the decimal type
(COBOL for one)
– This type stores a decimal digit in
½ a byte using the binary
equivalent
– Since a digit is 0-9, this leaves 6
codes unused
– Some architectures will use these 6
extra codes for special characters
such as ‘$’, ‘.’, etc or to reference
whether the decimal value is
unsigned, positive or negative
Fractions
• We can extend our unsigned representational
system of binary to include a decimal point
– After the decimal point, the i exponent, in 2i, becomes
negative
• So, we now have the ½ column, the ¼ column, etc
– 1011.1001 =
– 1*23 + 0*22 + 1*21 + 1*20 + 1*2-1 + 0*2-2 + 0*2-3 + 1*2-4 =
– 8 + 2 + 1 + ½ + 1/16 =
– 11 9/16 = 11.5625
• What is .4304? Use 8-bits with 4 fraction bits
– .4304 has a .25, .125, .03125, .015625, and more fractions, but this
exceeds the number of fraction bits so the number is 0000.0110
– But 0000.0110 = .125 + 0.3125 = .375, we have a loss in precision!
• In the fraction representation, our decimal point is typically
fixed, so this is often known as fixed point representation
• We will cover a floating point representation later
Signed Integers
• So far we have treated all of our numbers as
unsigned (or positive only)
– To implement signed integers (or signed fractions),
we need a mechanism to denote the sign itself
(positive or negative)
• Unfortunately, this introduces new problems, so we will see
3 different approaches, all of which add a special bit known
as the sign bit
– If the sign bit is 0, the number is positive
– If the sign bit is 1, the number is negative
• If we have an 8 bit number, does this mean that we now
need 9 bits to store it with one bit used exclusively for the
sign?
Signed Magnitude
• The first signed integer format is signed
magnitude where we add a bit to the front of our
numbers that represents the sign
– In 4 bits, 3 = 0011 and –3 = 1011
• Notice in 4 bits, we can store 16 numbers in unsigned
magnitude (0000 to 1111, or decimal 0 to 15) but in signed
magnitude we can only store 15 numbers (between –7, or
1111, and +7, 0111), so we lose a number
– Two problems:
• 0 is now represented in two ways: 0000, 1000, so we lose
the ability to store an extra number since we have two 0s
• We cannot do ordinary arithmetic operations using signed
magnitude
– we have to “strip” off the sign bit, perform the operation, and
insert the sign bit on the new answer – this requires extra hardware
One’s Complement
• An alternative approach to signed magnitude is one’s
complement where the first bit is again a sign bit
• But negative numbers are stored differently from positive
numbers
– Positive number stored as usual
– Negative number – all bits are inverted
• 0s become 1s, 1s become 0s
– Example: +19 in 6 bits = 010011, -19 = 101100
• The first bit is not only the sign bit, but also part of the number
– Notice that we still have two ways to represent 0, 000000 and
111111
• So, we won’t use one’s complement
Two’s Complement
• Positive numbers remain the same 4-bit Two’s Complement
Binary Decimal
• Negative numbers: derived by flipping 1000 -8
each bit and then adding 1 to the result 1001 -7
1010 -6
– +19 in 6 bits = 010011, 1011 -5
1100 -4
– -19 in 6 bits = 101101 1101 -3
• 010011  101100 +1  101101 1110 -2
1111 -1
– To convert back, flip all bits and add 1 0000 0
• 101101  010010 + 1  010011 0001 1
– While this is harder, it has two advantages 0010 0011
2
3
• Only 1 way to represent 0 (000000) so we can 0100 4
store 1 extra value that we lost when we tried 0101 5
signed magnitude and one’s complement 0110 6
• 0111 7
Arithmetic operations do not require “peeling”
off the sign bit
Some Examples
Represent 83 and –83 using 8 bits in all 3 signed representations:

+83 = 64 + 16 + 2 + 1 = 01010011 (in all 3 representations)

-83:
Signed magnitude = 11010011 (sign bit is 1 for negative)
One’s complement = 10101100 (flip all bits from +83)
Two’s complement = 10101101 (flip all bits from +83 and add 1)

Convert 11110010 into a decimal integer in all 4 representations


Unsigned magnitude = 128 + 64 + 32 + 16 + 2 = 242
Signed magnitude = -114 (negative, 1110010 = 114)
One’s complement = -13 (leading bit = 1, the number is negative,
flip all bits 00001101 = 13)
Two’s complement = -14 (negative, so flip all bits and add 1 
00001101 + 1 = 00001110 = 14)
Addition
• This operation is much like decimal addition
except that you are only adding 1s and 0s
• Add each column as you would in decimal, write down the
sum and if the sum > 1, carry a 1 to the next column
• Four possibilities:
– Sum of the two digits (and any carry in) = 0, write 0, carry 0
– Sum = 1, write 1, carry 0
– Sum = 2, write 0, carry 1 (this represents 10 = 2)
– Sum = 3, write 1, carry 1 (this represents 11 = 3)

Examples:
1 + 1 = 2, write 0, carry 1
01000101 11111111 The carry out of this
+ 00001111 + 10101010 last bit causes overflow
01010100 110101001
Subtraction
• There are two ways we could perform subtraction
– As normal, we subtract from right to left with borrows now
being 2 instead of 10 as we move from one column to the next
– Or, we can negate the second number and add them together
(36 – 19 = 36 + -19)
• We will use the latter approach when implementing a subtraction
circuit as it uses the same circuit as addition

Examples:
borrow 2 from the previous
11010100 column 11010100  11010100
- 00110011 - 00110011 + 11001101
10100001 110100001

Notice the overflow in this case too, but it differs from the
last example because we are using two’s complement
Overflow Rules
• In unsigned magnitude addition
– a carry out of the left-most bit is also an overflow
• In unsigned magnitude subtraction
– overflow will occur in subtraction if we must borrow prior to the
left-most bit
• In two’s complement addition/subtraction
– if the two numbers have the same sign bit and the
sum/difference has a different sign bit, then overflow

Below we see examples of four signed additions


Multiplication
• Multiplication is much like as you do it in decimal
– Line up the numbers and multiply the multiplicand by one digit of the
multiplier, aligning it to the right column, and then adding all products
together
• but in this case, all values are either going to be multiplied by 0 or 1
– So in fact, multiplication becomes a series of shifts and adds:

110011
* 101001 This is the same as:
110011
000000 110011 * 101001 =
000000 110011 * 100000 + 110011 * 00000 +
110011 110011 * 1000 + 110011 * 000 +
000000 110011 * 00 + 110011 * 1
110011 = 110011 * 100000 + 110011 * 1000 + 110011 * 1
Add these values

We will use a tabular approach for simplicity (see next slides)


NOTE: this algorithm works only if both
Multiplication numbers are positive. If we have negative
values in two’s complement, we will use a

Algorithm different algorithm

A is the accumulator

M and Q are temporary


registers

C is a single bit storing the


carry out of the addition of
A and M

The result is stored in the


combination of registers
A and Q (A storing the upper
half of the product, Q the
lower half)
Example
First, load the multiplicand
in M and the multiplier in Q

A is an accumulator along
with the left side of Q

As we shift C/A/Q, we
begin to write over part of Q
(but it’s a part that we’ve
already used in the
multiplication)

For each bit in Q, if 0 then


merely shift C/A/Q,
Need 8 bit location to store result of otherwise add M to C/A
two 4 bit multiplications
Notice that A/Q stores the
resulting product, not just A
Booth’s Algorithm
Compare rightmost bit of
We will use Q (that is, Q0) with the
Booth’s algorithm previous rightmost bit
if either or both
from Q (which is stored
numbers are
in a single bit Q-1)
negative

The idea is based Q-1 is initialized to 0


on this observation:
If this sequence is 0 – 1
0011110 = then add M to A
0100000 –
0000010
If this sequence is 1 – 0
So, in Booth’s, then sub M from A
we look for
transitions of 01 If this sequence is 0 – 0
and 10, and ignore or 1 – 1 then don’t add
00 and 11 sequences
in our multiplier After each iteration, shift
Example of Using Booth
Initialize A to 0
Initialize Q to 0011
Initialize M to 0111
Initialize Q-1 to 0

1) Q/Q-1=10, AA–M,
Shift
2) Q/Q-1=11, Shift

3) Q/Q-1=01,AA+M,
Shift

4) Q/Q-1=00, Shift

Done, Answer = 00010101


Division
• Just as multiplication is a series of additions and
shifts, division is a series of shifts and subtractions
– The basic idea is this:
• how many times can we subtract the denominator from the
numerator?
Consider 110011 / 000111

We cannot subtract 000111 from 000001 Our divisor is 0, shift 000001


We cannot subtract 000111 from 000011 Our divisor is 00, shift 00011
We cannot subtract 000111 from 000110 Our divisor is 000, shift 000110
We can subtract 000111 from 001100 Now, our divisor is 0001,
leaving 000101 shift 000101
We can subtract 000111 from 001010 Now our divisor is 00011,
leaving 000101 shift 000101
We can subtract 000111 from 001010 Our divisor is now 000111
leaving 000101
Giving the answer 000111 with a remainder We are done after 6 iterations (6 bits)
of 000101
Division Algorithm This algorithm
only works on
• Dividend is expressed using 2*n positive values
bits and loaded into the combined (unsigned)
A/Q registers
– upper half in A, lower half in Q We will not cover
• Notice that we subtract M from A a division
and then determine if the result is algorithm for
negative – if so, we restore A, two’s complement
• An easier approach is:
– Remove A  A – M
– Replace A < 0? With A< M?
– If No, then A  A – M, Qn  1
– If Yes, then Qn  0
– Now we don’t need to worry about
restoring A
• At the conclusion of the
operation
– the quotient is in Q
– and any remainder is in A
Division Example: 7 / 3
A Q M
0000 0111 0011 Initial Values

0000 1110 Shift A/Q left 1 bit


Since A < M, insert 0 into Q0

0001 1100 Shift A/Q left 1 bit


Since A < M, insert 0 into Q0

0011 1000 Shift A/Q left 1 bit


0000 1001 Since A >= M, AA-M, insert 1 into Q0

0001 0010 Shift A/Q left 1 bit


Since A < M, insert 0 into Q0
Done (4 shifts)

Result: Q = 0010, A = 0001


A = remainder (1) and Q = quotient (2) or 7 / 3 = 2 1 / 3
Bias Representation
• We can use unsigned magnitude to represent Excess-8 Notation
0000 -8
both positive and negative numbers by using a 0001 -7
bias, or excess, representation 0010 -6
– The entire numbering system is shifted up some 0011 -5
positive amount 0100 -4
0101 -3
• To get a value, subtract it from the excess
0110 -2
– For instance, in excess-16, we subtract 16 from the number
to get the real value (11001 in excess-16 is 11001 – 10000 0111 -1
in binary = 01001 = +9) 1000 0
1001 1
– To use the representation 1010 2
• numbers have to be shifted, then stored, and then shifted 1011 3
back when retrieved 1100 4
– this seems like a disadvantage, so we won’t use it to 1101 5
represent ordinary integer values
1110 6
– but we will use it to represent exponents in our floating
point representation (shown next)
1111 7
Floating Point Representation
• Floating point numbers have a floating decimal point
– Recall the fraction notation used a fixed decimal point
– Floating point is based on scientific notation
• 3518.76 = .351876 * 104
• We represent the floating point number using 2 integer values called the
significand and the exponent, along with a sign bit
– The integers are 351876 and 4 for our example above
– For a binary version of floating point, we use base 2 instead of
10 as the radix for our exponent
– We store the 2 integer values plus the sign bit all in binary
• We normalize the floating point number so that the decimal is implied
to be before the first 1 bit, and in shifting the decimal point, we
determine the exponent
• The exponent is stored in a bias representation to permit both positive
and negative exponents
• The significand is stored in unsigned magnitude
Examples
• Here, we use the following 14-bit representation:
Exponents
will be stored
using excess-16
Sign bit = 0 (positive)
Exponent = 5 (10101 – 10000 = 5)
Significand = .10001000
We shift the decimal point 5 positions giving us 10001.0 = +17

Sign bit = 0 (positive)


Exponent = -2 (01110 – 10000 = -2)
Significand = .10000000
We shift the decimal point 2 positions to the left,
giving us 0.001 = +.125
Sign bit = 1 (negative)
Exponent = 3 (10011 – 10000 = 3)
Significand = .11010100
We shift the decimal point 3 positions to the right,
giving 110.101= -6.625
Floating Point Formats and Problems
• To provide a standard for • Problems
all architectures, IEEE – there are numerous ways
provides the following to represent the same
formats: number (see page 58), but
because we normalize the
– Single precision numbers, there will
• 32-bits: 1-bit sign, 8-bit ultimately be a single
exponent using excess-127, representation for the
23-bit significand
number
– Double precision
– Errors arise from
• 64-bits: 1-bit sign, 11-bit
exponent using excess- • overflow (too great a
1023, 52-bit significand positive number or too great
a negative number) –
– IEEE also provides NAN overflowing the signficand
for errors when a value is • underflow (too small a
not a real number fraction) – overflowing the
• NAN = not a number exponent
Representing Characters
• We use a code to represent characters
– EBCDIC – developed for the IBM 360 and used in all IBM
mainframes since then
• An 8-bit representation for 256 characters
– ASCII – used in just about every other computer
• A 7-bit representation plus the high-order bit used for parity
– Unicode – newer representation to include non-Latin based
alphabetic characters
• 16 bits allow for 65000+ characters
• It is downward compatible with ASCII, so the first 128 characters
are the same as ASCII
– See figures 2.6-2.8
Error Detection
• Errors will still arise, • Simpler approach is to use a parity bit
so we should also
to every byte of information
provide error
– Add up the number of 1 bits in the byte,
detection and
add a bit so that the number of total 1s is
correction
even
mechanisms • 00101011 has a parity bit of 0, 11100011
– One method is to add has a parity bit of 1
a checksum to each
block of data – With more parity bits, we can not only
• Bits are appended detect an error, but correct it, or detect 2
to every block that errors
somehow encode
the information
• Hamming Codes are a common way to
• One common form provide high redundancy on error
is the CRC checking
– Cyclic
redundancy – We will skip the discussion on Hamming
check Codes, but if you want to read it, its on
– see pages 81-84 pages 84-90 of the textbook)
for details

You might also like