w4 One PDF
w4 One PDF
Computer Architecture
Lecture 8 (W5)
Fall 2012
l Topics:
l Data representation 2.1 and 2.2 of the book
l Floating point 2.4 of the book
1
Computer Architecture
l What do computers do?
l Manipulate stored
information
l Manipulation: operations …
more on this later
l Information is data: how is it
represented?
Basic information: numbers
Discoveregypt.com
l
l Human beings have
represented numbers
throughout history
l Roman numerals
l Decimal system
2
Number System
l Comprises of
l Set of numbers or elements
l Operations on them (+ - / *)
l Rules that define properties of operations (identity,
inverse…)
l Need to assign value to numbers
l Let us take decimal system
l 1’s place, 10’s place, 100’s place etc
l Base 10
i
l Value= ∑ i ×10
l Humans use decimal
3
Binary numbers
l Base 2; each digit is 0 or 1
l Each bit in place i has value 2i
4
Hexadecimal representation
l Binary hard to read for humans
l Especially 16 bits, 32 bits, 64 bits
l 1010101001010101
l Base 16 representation or hex representation
l Symbols ={0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F}
i
l Value = ∑ i ×16
l First 10 (0 through 9) symbols are same as
decimal numbers
l A=10,B=11,C=12, D=13, E=14, F=15
5
Decimal to binary
main(){
int N,i=0; int Maxbits=32; int bitarray[Maxbits];
scanf (“%d”, &N):
for (i=0;i<Maxbits;i++) bitarray[i]=0; /* initialize */
i=0;
while (N > 0){
/* MSB is bit 0, LSB is bit 31 */
bitarray[Maxbits-1-i]=N%2;i++; /* mod function */
N=N/2;
}
i=0; for (i=0; i<Maxbits; i++) printf(“%d”,bitarray[i]);
}
6
Same for Hex
l Example: Convert 10 in decimal to binary
l The code for binary works for any base with modifications
l For hex, replace by N%16 and N=N/16 replace
reminders > 9 by A, B, C, D, E and F
l Example: Convert 240 to HEX
7
Binary to decimal
l Convert 110110 to decimal
l Value = i*2i
l Value = 25 24 22 21
l = 32+ 16 + 4 + 2
l = 54
l
l
8
Converting Hex to binary
l Each digit in Hex can be represented by 4 bit
binary (base 16) or nibble
l Convert 2A8C to binary
9
Convert Binary to hex
l Group binary bits into groups of four
l Replace each nibble by a hex digit
l 1011011110011100
10
Examples
l 0x8F7A93 to binary
l 1011011110011100 to hex
11
Decimal and binary fractions
l In decimal, digits to the right of radix point
have value 1/10i for each digit in the ith place
l 0.25 is 2/10 + 5/100
l Similarly, in binary, digits to the right of radix
point have value 1/2i for each ith place after
the decimal point.
l Binary Fractions are the same except the
base is different
l 8.625 is 1000.101
12
Decimal to binary example
l 0.625 to binary
l ANS: 0.101 Algorithm
/* precondition: 0< number <1)
l 0.625*2 = 1.25 number=decimalfraction
while (number >0)
l output 1 { number=number*2 /*shift
left */
l 0.25*2 =0.5 if (number >=1) {Output
l output 0 result=1; number=number-1}
else Output result=0
l 0.5*2 = 1 }
l output 1
l Exit
13
Decimal to binary fractions
Decimal Binary
0.5 0.1
0.25 0.01
0.625 0.101
0.75 0.11
14
Data sizes
l All Information is represented in binary form but
require different sizes
l Characters in 1 byte, integers 2 to 4 bytes, real
numbers 4 to 8 bytes
C declaration 32-bit machine 64-bit machine
char 1 1
Short int 2 2
int 4 4
pointer 4 8
float 4 4
double 8 8 15
Big endian vs little endian
l A binary representation of a number in memory may require
multiple bytes
l How to determine value of a sequence of bytes
l Most Significant byte first … big endian
l Lease significant byte first … little endian
l Depends on the type of machine
l Why we need to know:
l Look at machine code
l If we are to interpret bytes and determine value
16
Representing integers
l Signed integers
l Unsigned or natural numbers
l Directly represent as binary
l What about negative numbers?
l Signed Magnitude
S
Magnitude
l MSB is sign bit
l 4 is 0100 -4 1100
l 3 is 0011 -3 1011
l 2 is 0010 -2 1010
l 1 is 0001 -1 1001
l 0 is 0000 ..what is 1000?
l Problems: two zeros +ve 0 and –ve 0
l normal bit-wise addition does not work… -1 + 4
17
Representing Integers
l Divide the binary space into two halves
l One with leading 0s are +ve
l One with leading 1s are –ve
l This is called two’s complement representation
l Two bit binary numbers
l 00 01 10 11
l 0 1 -2 -1
l Three bit 000 001 010 011 100 101 110 111
0 1 2 3 -4 -3 -2 -1
18
2s complement advantages
l Adding two integers works as normal
arithmetic
l Normal arithmetic works for 2’s complement
(ignore carry)
l Only 1 zero
l X+ (-X) = 0
19
Negate a 2s complement
l invert the bits and add 1 The image cannot be displayed. Your computer may not have enough memory to open the image, or the image may have been corrupted. Restart your computer, and
then open the file again. If the red x still appears, you may have to delete the image and then insert it again.
−
l X = 001 X 110 à 111
20
Finding 2s complement
l Take the binary representation and invert all
bits
l Step1: 0 to 1 and 1 to 0
l Step 2:Then add 1
l Example: 00101 (5)
l (-5) is ……………
21
Finding 2s complement
l Copy all bits from right to left until first 1
l Atlantic ocean rule!!
l At
l Then flip all bits
l 0100 1
l 1011 1
23
Decimal value of 2’s
complement
l If leading bit or MSB is 1, take 2’s
complement to get +ve number
l Determine decimal value of number
24
1s complement
l Alternative representation 000 001 010 011 100 101 110 111
25
ASCII
l Characters are also stored as bits
l 1 byte is 8 bits but MSB is used for error
detection
l ASCII represents typical keys on a keyboard
l 7 bits is 128 different possibilities
l 7 bit ASCII included printable and non-
printable characters
l Non-printable for controlling printer such as
LF or linefeed
26
ASCII table
27
unicode
l Extended to 16 bit characters
l Represent other characters (Japanese)
28
Floating point
l With integers range is limited
l short int 2 bytes 216
l unsigned short int
l Unsigned: Range is natural number 0 to 65536
l Signed: 2s complement is -32768 to 32767
l for int (4 bytes) -2147483648 to 2147483647
l Can also be represented as magnitude and
exponent
l 2.147483647x109
29
Scientific notation
exponent
l 2.147483647x109
Mantissa
31
IEEE floating point standard
l Most computers follow IEEE 754 standard
l Single precision (32 bits)
S
Fraction
Exponent
32
Floating point in C
l 32 bits single precision or float
l 1 sign bit, 23 bits for mantissa, 8 bits for exponent
l Exponent is power of 2
l Signed magnitude
l Sign bit is 1 for –ve numbers, sign bit is 0 for +ve numbers
l Exponent has 8 bits
l 0 to 256 or -128 to + 127
l 2128 is approx 1038
l Range is -3.4x 1038 to +3.4x 1038
l For double precision
l 1 sign bit, 11 bits for exponent, 52 bits for mantissa
l Range is -1.798x 10308 to +1.798x 10308
33
Exponent bias
l The binary exponent is represented by
adding a bias of 127
l Saves an extra bit for sign bit
34
Normalization
l The exponent value is adjusted so that the
mantissa has a 1 before the radix point
l 0.25 is 0.01x 20 adjusted to 1x 2-2
35
Decimal to floating point
l 5.625
l In binary
l 101.101 à 1.01101 x 22
l Exponent field has value 2
l add 127 to get 129
l Exponent is 10000001
l Mantissa is 01101
l Sign bit is 0
l 0 0110100000000000000000 10000001
36
One more example
l Convert 12.375 to floating point
representation
l Binary is 1100.011
37
Floating point to decimal
1
10000010
0100100000000000000000
s
f
e
(1 − 2s) ∗ (1 + f ) ∗ 2e−bias
38
Special cases
l 0 00000000 00000000000000000000000
l All zeros 0 (+ 0)
l 1 00000000 00000000000000000000000
l All zeros with sign bit is -0 (-0)
l 0 11111111 00000000000000000000000
l Note: the minimum value in the exponent after adding the
bias should be 1. Hence, the minimum value for the
exponent in single precision is = -126 (=1-127 or x1-x7F)
l + Infinity
l 1 11111111 00000000000000000000000
l -Infinity
l Note: the maximum value in the exponent after adding the
bias 254 . Hence, the maximum value for the exponent in
single precision is = +127 (=254-127 or xFE-x7F)
39
Extended precision
l 80 bits used to represent a real number
l 1 sign bit, 15 bit exponent, 64 bit mantissa
40