Title: IKI10201 02Data Types
1IKI1020102-Data Types Representations
- Bobby Nazief
- Semester-I 2005 - 2006
The materials on these slides are adopted from
those in CS231s Lecture Notes at UIUC, which is
derived from Howard Huangs work and developed by
Jeff Carlyle.
2Road Map
Logic Gates Flip-flops
3
Boolean Algebra
3
6
Finite-StateMachines
6
4
Sequential DesignTechniques
Logic DesignTechniques
CombinatorialComponents
StorageComponents
2
Binary Systems Data Represent.
7
5
8
Register-TransferDesign
8
Generalized FSM
ProcessorComponents
9
3Number Systems
- To get started, well discuss one of the
fundamental concepts underlying digital computer
design - Deep down inside, computers work with just 1s and
0s. - Computers use voltages to represent information.
In modern CPUs the voltage is usually limited to
1.6-1.8V to minimize power consumption. - Its convenient for us to translate these
analogvoltages into the discrete, or digital,
values 1 and 0. - But how can two lousy digits be useful for
anything? - First, well see how to represent numbers with
- just 1s and 0s.
- Then well introduce special operations
- for computing with 1s and 0s, by treating them
as - the logical values true and false.
4Positional number systems decimal
- Numbers consist of a bunch of digits, each with a
weight - The weights are all powers of the base, which is
10. We can rewrite the weights like this - To find the decimal value of a number, multiply
each digit by its weight and sum the products.
(1 x 102) (6 x 101) (2 x 100) (3 x 10-1)
(7 x 10-2) (5 x 10-3) 162.375
5Positional number systems binary
- We can use the same trick for binary, or base 2,
numbers. The only difference is that the weights
are powers of 2. - For example, here is 1101.01 in binary
- The decimal value is
(1 x 23) (1 x 22) (0 x 21) (1 x 20) (0 x
2-1) (1 x 2-2) 8 4 0
1 0 0.25 13.25
6Base 8 - Octal
- The octal system uses 8 digits
- 0 1 2 3 4 5 6 7
- Earlier in this history of computing, octal was
used as a shorthand for binary numbers. (Now
hexadecimal is more common.) - Since 8 23, one octal digit is equivalent to 3
binary digits. - Numbers like 67 are easier to work with than
110111. - Still shows up in some places. For instance, file
access permissions in Unix file systems.
7Base 16 is useful too
- The hexadecimal system uses 16 digits
- 0 1 2 3 4 5 6 7 8 9 A B C D E F
- For our purposes, base 16 is most useful as a
shorthand notation for binary numbers. - Since 16 24, one hexadecimal digit is
equivalent to 4 binary digits. - Its often easier to work with a number like B4
instead of 10110100. - Hex is frequently used to specify things like
IPv6 addresses and 24-bit colors.
8Converting decimal to binary
- To convert a decimal integer into binary, keep
dividing by 2 until the quotient is 0. Collect
the remainders in reverse order. - To convert a fraction, keep multiplying the
fractional part by 2 until it becomes 0. Collect
the integer parts in forward order. - Example 162.375
- So, 162.37510 10100010.0112
9Why does this work?
- This works for converting from decimal to any
base - Why? Think about converting 162.375 from decimal
to decimal. - Each division strips off the rightmost digit (the
remainder). The quotient represents the remaining
digits in the number. - Similarly, to convert fractions, each
multiplication strips off the leftmost digit (the
integer part). The fraction represents the
remaining digits.
162 / 10 16 rem 2 16 / 10 1 rem 6 1 /
10 0 rem 1
0.375 x 10 3.750 0.750 x 10 7.500 0.500 x 10
5.000
10Binary and hexadecimal conversions
- Converting from hexadecimal to binary is easy
just replace each hex digit with its equivalent
4-bit binary sequence. - To convert from binary to hex, make groups of 4
bits, starting from the binary point. Add 0s to
the ends of the number if needed. Then, just
convert each bit group to its corresponding hex
digit.
261.3516 2 6 1 . 3 516
0010 0110 0001 . 0011 01012
10110100.0010112 1011 0100 . 0010 11002 B
4 . 2 C16
11Practice
- Convert 32103.510 to hexadecimal.
- Convert CAFE16 to decimal.
- Convert 7038 to base-12.
12Convert 32103.510 to hexadecimal.
- 32103 / 16 2006 rem 7
- 2006 / 16 125 rem 6
- 125 / 16 7 rem 13
- 7 / 16 0 rem 7
- .5 16 8.0
- So 7D67.816.
13Convert CAFE16 to decimal.
- C 12 163 49152
- A 10 162 2560
- F 15 161 240
- E 14 16- 14
- 4915225602401451966
14Convert 7038 to base-12.
- 7 7 82 448
- 0 0 81 0
- 3 3 80 3
- 44803 451
- 451 / 12 37 rem 7
- 37 / 12 3 rem 1
- 3 / 12 0 rem 3
- So 31712.
15Number Systems Summary
- Computers are binary devices.
- Were forced to think in terms of base 2.
- Today we learned how to convert numbers between
binary, decimal and hexadecimal. - Weve already seen some of the recurring themes
of architecture - We use 0 and 1 as abstractions for analog
voltages. - We showed how to represent numbers using just
these two signals.
16Additional information
- Assistants
- Panca Novianto panca.novianto_at_mhs.cs.ui.ac.id
- Evan Jonathan Winata evan.winata_at_mhs.cs.ui.ac.id
17Review - Positional number systems binary
- We can use the same trick for binary, or base 2,
numbers. The only difference is that the weights
are powers of 2. - For example, here is 1101.01 in binary
- The decimal value is
(1 x 23) (1 x 22) (0 x 21) (1 x 20) (0 x
2-1) (1 x 2-2) 8 4 0
1 0 0.25 13.25
18Review - Converting decimal to binary
- To convert a decimal integer into binary, keep
dividing by 2 until the quotient is 0. Collect
the remainders in reverse order. - To convert a fraction, keep multiplying the
fractional part by 2 until it becomes 0. Collect
the integer parts in forward order. - Example 162.375
- So, 162.37510 10100010.0112
19Addition and Subtraction of Binary Numbers
- Arithmetic is the most basic thing you can do
with a computer, but its not as easy as you
might expect! - These next few lectures focus on addition and
subtraction. - Computers were designed to compute, so arithmetic
is at the heart of a CPU.
20Binary addition by hand
- You can add two binary numbers one column at a
time starting from the right, just as you add two
decimal numbers. - But remember that its binary. For example, 1 1
10 and you have to carry!
21Subtraction
- Computers do subtraction by adding the minuend
with the negative representation of the
subtrahend - The main problem is representing negative numbers
in binary. We introduce three methods, and show
why one of them is the best. - With negative numbers, well be able to do
subtraction using the adders we made last time,
because A - B A (-B).
22Negative Numbers
23Signed magnitude representation
- Humans use a signed-magnitude system we add or
- in front of a magnitude to indicate the sign. - We could do this in binary as well, by adding an
extra sign bit to the front of our numbers. By
convention - A 0 sign bit represents a positive number.
- A 1 sign bit represents a negative number.
- Examples
11012 1310 (a 4-bit unsigned number) 0 1101
1310 (a positive number in 5-bit signed
magnitude) 1 1101 -1310 (a negative number in
5-bit signed magnitude)
01002 410 (a 4-bit unsigned number) 0 0100
410 (a positive number in 5-bit signed
magnitude) 1 0100 -410 (a negative number in
5-bit signed magnitude)
24Signed magnitude operations
- Negating a signed-magnitude number is trivial
just change the sign bit from 0 to 1, or vice
versa. - Adding numbers is difficult, though. Signed
magnitude is basically what people use, so think
about the grade-school approach to addition. Its
based on comparing the signs of the augend and
addend - If they have the same sign, add the magnitudes
and keep that sign. - If they have different signs, then subtract the
smaller magnitude from the larger one. The sign
of the number with the larger magnitude is the
sign of the result. - This method of subtraction would lead to a rather
complex circuit.
25Ones complement representation
- A different approach, ones complement, negates
numbers by complementing each bit of the number. - We keep the sign bits 0 for positive numbers,
and 1 for negative. The sign bit is complemented
along with the rest of the bits. - Examples
11012 1310 (a 4-bit unsigned number) 0 1101
1310 (a positive number in 5-bit ones
complement) 1 0010 -1310 (a negative number in
5-bit ones complement)
01002 410 (a 4-bit unsigned number) 0 0100
410 (a positive number in 5-bit ones
complement) 1 1011 -410 (a negative number in
5-bit ones complement)
26Why is it called ones complement?
- Complementing a single bit is equivalent to
subtracting it from 1. - 0 1, and 1 - 0 1 1 0, and 1 - 1 0
- Similarly, complementing each bit of an n-bit
number is equivalent to subtracting that number
from 2n-1. - For example, we can negate the 5-bit number
01101. - Here n5, and 2n-1 3110 111112.
- Subtracting 01101 from 11111 yields 10010
27Ones complement addition
- To add ones complement numbers
- First do unsigned addition on the numbers,
including the sign bits. - Then take the carry out and add it to the sum.
- Two examples
- This is simpler and more uniform than signed
magnitude addition.
28Twos complement
- Our final idea is twos complement. To negate a
number, complement each bit (just as for ones
complement) and then add 1. - Examples
11012 1310 (a 4-bit unsigned number) 0 1101
1310 (a positive number in 5-bit twos
complement) 1 0010 -1310 (a negative number in
5-bit ones complement) 1 0011 -1310 (a
negative number in 5-bit twos complement)
01002 410 (a 4-bit unsigned number) 0 0100
410 (a positive number in 5-bit twos
complement) 1 1011 -410 (a negative number in
5-bit ones complement) 1 1100 -410 (a negative
number in 5-bit twos complement)
29More about twos complement
- Two other equivalent ways to negate twos
complement numbers - You can subtract an n-bit twos complement number
from 2n. - You can complement all of the bits to the left of
the rightmost 1. - 01101 1310 (a positive number in twos
complement) - 10011 -1310 (a negative number in twos
complement) - 00100 410 (a positive number in twos
complement) - 11100 -410 (a negative number in twos
complement)
30Comparing the signed number systems
- Here are all the 4-bit numbers in the different
systems. - Positive numbers are the same in all three
representations. - Signed magnitude and ones complement have two
ways of representing 0. This makes things more
complicated. - Twos complement has asymmetric ranges there is
one more negative number than positive number.
Here, you can represent -8 but not 8. - However, twos complement is preferred because it
has only one 0, and its addition algorithm is the
simplest.
31Converting signed numbers to decimal
- Convert 110101 to decimal, assuming this is a
number in -
- (a) signed magnitude format
-
- (b) ones complement
-
- (c) twos complement
-
32Example solution
- Convert 110101 to decimal, assuming this is a
number in - Since the sign bit is 1, this is a negative
number. The easiest way to find the magnitude is
to convert it to a positive number. - (a) signed magnitude format
- Negating the original number, 110101, gives
010101, which is 2110 in decimal. So 110101
must represent -2110. - (b) ones complement
- Negating 110101 in ones complement yields
001010 1010, so the original number must have
been -1010. - (c) twos complement
- Negating 110101 in twos complement gives
001011 1110, which means 110101 -1110. - The most important point here is that a binary
number has different meanings depending on which
representation is assumed.
33Addition Subtraction of 2s complement numbers
- Subtraction
- Form 2s complement of the subtrahend
- Add the two numbers as in Addition
- Addition
- Just add the two numbers
- Ignore the Carry-out from MSB
- Result will be correct, provided theres no
overflow
0 1 0 1 (5)0 0 1 0 (2) 0 1 1 1 (7)
0 1 0 1 (5)1 0 1 0 (-6) 1 1 1 1 (-1)
0 0 1 0 (2) 0 0 1 0?0 1 0 0 (4) 1 1 0
0 (-4) 1 1 1 0 (-2)
1 0 1 1 (-5)1 1 1 0 (-2)11 0 0 1 (-7)
0 1 1 1 (7)1 1 0 1 (-3)10 1 0 0 (4)
1 1 1 0 (-2) 1 1 1 0?1 0 1 1 (-5) 0 1 0
1 (5) 10 0 1 1 (3)
34Overflow
- Examples 7 3 10 but ...
- - 4 5 - 9 but ...
1
1
1
0
1
0
1
1
1
1
1
0
0
7
4
3
5
0
0
1
1
1
0
1
1
1
0
1
0
0
1
1
1
6
7
35Binary Multiplication
- Since we always multiply by either 0 or 1, the
partial products are always either 0000 or the
multiplicand (1101 in this example). - There are four partial products which are added
to form the result.
36Shifting the multiplicand
1 1 0 1 Multiplicand x 0 1 1 0 Multiplier
0 0 0 0 first partial products 0 0 0 0 sh
ifted zeros 0 0 0 0 second partial
products 1 1 0 1 shifted multiplicand 1 1
0 1 0 third partial products 1 1 0 1 shifted
multiplicand 1 0 0 1 1 1 0 fourth partial
products 0 0 0 0 shifted
zeros 1 0 0 1 1 1 0 Product
37Binary Division
38Floating-Point Numbers
39Scientific notation review
6.02 x 1023
- Normalized form no leadings 0s (exactly one
digit to left of decimal point) - Alternatives to representing 1/1,000,000,000
- Normalized 1.0 x 10-9
- Not normalized 0.1 x 10-8,10.0 x 10-10
40Scientific notation for binary numbers
1.0two x 2-1
- Computer arithmetic that supports it called
floating point, because it represents numbers
where binary point is not fixed, as it is for
integers - Declare such variable in C as float
41Floating point representation
- Normal format 1.xxxxxxxxxxtwo2yyyytwo
- Multiple of Word Size (32 bits)
- S represents SignExponent represents
ysSignificand represents xs - Represent numbers as small as 2.0 x 10-38 to as
large as 2.0 x 1038
42BCD
43Character Codes
44Codes for Error Detection Correction
45Hamming Codes