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

4 - Number Representation

COL 100 - Introduction to Computer


p
Science
II Semester 2015-2016
Department of Computer Science and Engineering
Indian Institute of Technology Delhi

Number Representation
Numbers are key to computer operation
Instructions can be viewed as just numbers
interpreted in a particular way

D
Data
t consists
i t off numbers
b
...or other entities that can be encoded as
numbers (e
(e.g.,
g characters)
A, B, ... encoded as numbers (65, 66, etc. in ASCII)

We need a mechanism for storing and


retrieving numbers on a machine
Jan 2016

P. R. Panda, I.I.T Delhi

Representing Integers
We want to store number 3465 at
memory address 2
Problem: how big can this number
be?

0
1
2

3465

an integer can be infinitely large!

Simplify: fixed number of digits


machine specifies limits on largest
and smallest integers it can
store/handle
t /h dl

Jan 2016

P. R. Panda, I.I.T Delhi

254
255

D i l Representation
Decimal
R
t ti
Number
N b 3465 is
i a DECIMAL number
b
base is 10
each digit of number {0,1,2,3,4,5,6,7,8,9}

Interpretation:
3465 = 3 103 + 4 102 + 6 101 + 5 100

But we can represent the same number in a different base,


say 12
in this base
base, each digit of number {0,1,2,3,4,5,6,7,8,9,10,11}
{0 1 2 3 4 5 6 7 8 9 10 11}
346510 = 200912 = 2 123 + 0 122 + 0 121 + 9 120

...or base 5
in this base, each digit of number {0,1,2,3,4}
346510 = 1023305 = 1 55 + 0 54 + 2 53 + 3 52 + 3 51 + 0 50
Jan 2016

P. R. Panda, I.I.T Delhi

Representing Integers in Arbitrary


Bases
Base r
n-digit number
Digits an-1,...,a2,a1,a0 {0,1,2,...,r-1}
{0 1 2 r 1}
Interpretation of number in base r:
an-1 rn-11 + an-2 rn-22 + ... + a2 r2 + a1 r1 + a0 r0

Jan 2016

P. R. Panda, I.I.T Delhi

Binary Numbers
Binary number: Base 2
n-digit number
Digits an-1,...,a2,a1,a0 {0,1}
Interpretation of number in base 2:
an-1 2n-1 + an-2 2n-2 + ... + a2 22 + a1 21 + a0 20

11012 = 1 23 + 1 22 + 0 21 + 1 20
= 1 8 + 1 4 + 0 2 + 1 1
= 8 + 4 + 1 = 1310

Thus, 11012 is another way to represent thirteen


Jan 2016

P. R. Panda, I.I.T Delhi

Decimal vs Binary Representation

Each memory location stores an


integer with fixed number of
digits
Ultimately, each digit has to be
electrically stored in machine
Decimal computer:
p
we need to
find a way to represent 10
different entities for each digit
Binary
a y co
computer:
pu e find
d a way
ay to
o
represent 2 different things
Modern computers: 2 voltage
levels

0
1
2

254
255

1 V (or 2V, etc.) represents 1


0 V represents 0
Jan 2016

13

P. R. Panda, I.I.T Delhi

0
1
2

1101

254
255
Decimal
p
Computer

Binary
Computer
p
7

Why
h Binary Representation?
G
Greatt for
f computers!
t !
Think of each digit as a
charge stored on a
capacitor

0
1
2

if capacitor is charged, a 1 is
stored
t d
if capacitor is discharged, a
0 is stored

Since ANY number can be


represented as a binary
number we have a way to
number,
store anything we want
both data and instructions
Jan 2016

P. R. Panda, I.I.T Delhi

1101

254
255
8

Other Popular Bases


Base 8 (Octal)
digits are 0,1,2,3,4,5,6,7

Base 16 (Hexadecimal)
digits are 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
10=A, 11=B, 12=C, 13=D, 14=E, 15=F

Jan 2016

P. R. Panda, I.I.T Delhi

Representing Real Numbers


3465.28 = 3 103 + 4 102 + 6 101 + 5 100 +
2 10-1 + 8 10-2
1101.112 = 1 23 + 1 22 + 0 21 + 1 20 +
1 2-1 + 1 2-2
= 1 8 + 1 4 + 0 2 + 1 1 +
1 0.5 + 1 0.25
= 8 + 4 + 1 + .5 + .25 = 13.7510
Jan 2016

P. R. Panda, I.I.T Delhi

10

Conversion between Number


Systems
y
Conversion from some base to decimal
system easy
use equation

Conversion from decimal system to


other base
repeated division by powers of base
keep track of remainders
Jan 2016

P. R. Panda, I.I.T Delhi

11

Example: Convert 3510 to Base 2

Jan 2016

P. R. Panda, I.I.T Delhi

12

When one base is a power of


another...

Jan 2016

P. R. Panda, I.I.T Delhi

13

Representing Negative Numbers


Sign-magnitude
representation
Separate digit for sign

Ones complement
representation
invert representation of
positive
iti number
b

Twos complement
representation
1 + ones complement
Jan 2016

5:

-5:

5:

-5:
5:

5:

-5:

P. R. Panda, I.I.T Delhi

14

Representing Integers in 2s
Complement
p
Range of integers for nbit binary number: -2
2nn-11
to +2n-1-1
With 3 bits we can
represent: -4 to +3
Leftmost bit represents
sign
0: positive
1: negative
Jan 2016

P. R. Panda, I.I.T Delhi

-4

-3

-2

-1

15

Binary Arithmetic
Exactly the same rules as decimal
Multiplication
Addition

+
=

11

11

17

+
=
Jan 2016

P. R. Panda, I.I.T Delhi

66
16

Binary Arithmetic: Subtraction


Confirm:

Subtraction: Convert to negative


negative, then add

11

Jan 2016

1 complement
1s
l
t

=
0

P. R. Panda, I.I.T Delhi

+1
2s complement

-11 (2s complement)


-5 (2s complement)

-5

17

Representing Floating Point Numbers


Scientific Notation: 2.754 10-4
in decimal
one digit to left of decimal point
no lleading
di zeroes ((special
i l case - number
b zero))

Binary representation: 1.0010 2-011


Floating Point: decimal point is not fixed
Total number of bits is fixed
31 30
s

Exponent e

Mantissa m

((-1))s
Jan 2016

23 22

m 2e
P. R. Panda, I.I.T Delhi

18

Bits and
d Bytes
Bit - a single binary digit (0 or 1)
Byte - 8 bits
1 integer usually 32 bits (4 bytes)
or 64 (8 bytes)

Characters (('a', 'b',...)) can be encoded


1 byte in ASCII code (American Standard Code for
Information Interchange)
Other codes (Unicode (All languages): up to 4 bytes)

Boolean values (true/false)


1 bit is sufficient

Jan 2016

P. R. Panda, I.I.T Delhi

19

You might also like