0% found this document useful (0 votes)
124 views40 pages

w4 One PDF

This document summarizes a lecture on data representation in computer architecture. It discusses binary, hexadecimal, and floating point number systems. It covers converting between decimal, binary, and hexadecimal. It also explains signed integer representation using two's complement and issues with signed magnitude and one's complement. Key topics included binary fractions, data sizes for different data types, endianness, and representing positive and negative numbers.

Uploaded by

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

w4 One PDF

This document summarizes a lecture on data representation in computer architecture. It discusses binary, hexadecimal, and floating point number systems. It covers converting between decimal, binary, and hexadecimal. It also explains signed integer representation using two's complement and issues with signed magnitude and one's complement. Key topics included binary fractions, data sizes for different data types, endianness, and representing positive and negative numbers.

Uploaded by

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

198:211

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

l  Binary representation is used in computers

l  Easy to represent by switches (on/off)

l  Manipulation by Digital logic in hardware

l  But hard for humans to read

l  (13)10 = (1101)2

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

l  0010 1010 0100 1100

9
Convert Binary to hex
l  Group binary bits into groups of four
l  Replace each nibble by a hex digit

l  Example 1011011110011100

l  1011011110011100

l  B79C or 0x B79C

l  In C, numeric constants starting with 0x are


interpreted as being in hexadecimal

10
Examples
l  0x8F7A93 to binary
l  1011011110011100 to hex

l  0xC4E5 to decimal

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

l  One computer (big endian) sending data to another computer


(small endian)
l  Need to convert into standard form before transmitting

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

l  Used in almost all computers

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

l  All 1s is -1, so add 1 to make it zero

l  So, to get –X, invert all bits and add 1

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 ……………

l  Find 2s complement of 9

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

l  Example find 2s complement of 0100, 0110


l  0100 0110
22
Decimal value of 2s
complement
l  Most significant bit has –ve value
l  Xn-1, Xn-2, ….. X1, X0

l  Value of MSB is -2 (n-1)


l  Rest is same as +ve binary number

l  110011 … what is the decimal value?

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

l  And add –ve sign to it

l  If leading bit or MSB is 0, compute as normal

l  Example 1100110TWO

24
1s complement
l  Alternative representation 000 001 010 011 100 101 110 111

l  -ve numbers represented by the


complement 0 1 2 3 -3 -2 -1 -0
l  Max numbers that can be
represented is halved
l  When adding –ve numbers carry
need to be added to the result
101 (-2)

l  -3 + (-2)
l  100 110 (-1)

l  101 -----

l  1 001 à add carry 1011

l  1 --------

1

---------

l  010 à 2 but correct answer 100 (-3)

is (-)5

l  Overflow

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)

l  Java supports this format

l  In Unicode, the first 128 characters are ASCII

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

l  Number of digits in Mantissa à precision


l  Exponent gives range

l  A binary number can also be expressed in


this form
l  10111011 à 1.0111011x27
30
Same number with varying
exponent
l  10101 à 21 ….. In decimal
l  1010.1 x 2 à10.5 x 2

l  101.01 x 22 à 5.25 x 4

l  10.101 x 23à2.625 x 8

l  Too many ways to represent the same


number
l  Need a standard representation

31
IEEE floating point standard
l  Most computers follow IEEE 754 standard
l  Single precision (32 bits)

l  Double precision (64 bits)

l  Extended precision (80 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

l  Note: it is not 2s complement

l  23 is 2130 which is 2 10000010

l  20 is 2127 which is 2 01111111

l  2-3 is 2124 which is 2 01111100

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

l  4.625 is 100.101 x 20 adjusted to 1.00101 x22

l  64 is 1000000 x 2 0 adjusted 1x 26

l  In this way, the mantissa always has 1 digit


with value 1
l  This can be omitted to save a bit!!!

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

What is the decimal value of the above?



Exponent is (10000010) - 127

Mantissa is 1.01001

Sign bit is -1

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

l  20 decimal digits of accuracy

l  10-4932 to 10 4932

l  Not supported in C

40

You might also like