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

COMPUTER PROGRAMMING I ( (TA C162) )

LECTURE 2 REPRESENTATION OF BINARY NUMBERS

Todays Agenda
Representation of Numbers
Binary Numbers Unsigned Integers
Non Positional Positional

Signed Magnitude 1s Complement

Saturday, January 9, 2010

Biju K Raveendran@BITS Pilani.

Representation of Information
Representation 1
One, Two, Three, .., Dozen, Gross Operation:
One + Two = Three Dozen x Dozen = Gross

Problem:
Cumbersome when working with large Numbers Operations are not systematic O ti t t ti

Saturday, January 9, 2010

Biju K Raveendran@BITS Pilani.

Representation of Information
Second Attempt
I, II, III, IV, V,, IX, X, XI, Operations
X*X=C XX * XX = CCCC

Problems
Operations are slightly better still not a good representation

Saturday, January 9, 2010

Biju K Raveendran@BITS Pilani.

Representation of Information
Simplest way
Unary Representation Example 5 is represented as 11111

Problems
Cumbersome when working with large numbers

Saturday, January 9, 2010

Biju K Raveendran@BITS Pilani.

Representation of Information
Decimal Numbers
1, 2, 1 2 , 10 11 10,11, Operations are systematic i.e. algorithmic Problem:
At the lowest level, a computer is an electronic machine. Computer is working with devices which react to p g presence or absence of voltage (controlling the flow of electrons).

Easy to recognize two conditions:


1. 2.

Presence of a voltage well call this state 1 Absence of a voltage well call this state 0

Saturday, January 9, 2010

Biju K Raveendran@BITS Pilani.

Computer is a binary digital system


Digital system: g y

Finite number of symbols


Binary (base two) system:

Has two states: 0 and 1

Saturday, January 9, 2010

Biju K Raveendran@BITS Pilani.

Computer is a binary digital system Cont


Basic unit of information is the binary digit, or bit. Values with more than two states require multiple bits (wires).
A sequence of two bits has four p q possible states: 00, 01, 10, 11 A sequence of three bits has eight possible states: 000, 001, 010, 011, 100, 101, 110, 000 001 010 011 100 101 110 111

Inference:
A sequence of n bits has 2n possible states.
Saturday, January 9, 2010 Biju K Raveendran@BITS Pilani. 8

What kinds of data do we need to represent?


Numbers signed unsigned integers floating signed, unsigned, integers, point, complex, rational, irrational, Text characters strings characters, strings, Images pixels, colors, shapes, Sound S d Logical true, false Instructions
Saturday, January 9, 2010 Biju K Raveendran@BITS Pilani. 9

Representation of a Number
Number N b 5 can b represented as be t d
Five 5 V 11111 0101 A representation is a data type if there are operations in the computer that can operate on information encoded in that t th t t i f ti d d i th t representation In this course we mainly use 2 data types
2s complement integers for representing +ve and ve integers (to perform arithmetic operations) ASCII codes f representing characters d for ti h t
Saturday, January 9, 2010 Biju K Raveendran@BITS Pilani. 10

Non-positional Non positional notation

Unsigned Integers

No weightage for the position {0th, 1st, etc.. }

Could represent a number (5) with a string of ones (11111) (5) ( 11111 ) Problems?
Weighted W i ht d positional notation iti l t ti
like decimal numbers: 329 3 is worth 300, because of its position, while 9 is only worth 9
most significant least significant most significant least significant

329

101
21

102 101 100 3x100 + 2x10 + 9x1 = 329


Saturday, January 9, 2010

22

20

1x4 + 0x2 + 1x1 = 5


11

Biju K Raveendran@BITS Pilani.

Unsigned Integers (cont.)


An n-bit unsigned integer represents 2n values: from 0 to 2n-1.
22 0 0 0 0 1 1 1 1 21 0 0 1 1 0 0 1 1 20 0 1 0 1 0 1 0 1 0 1 2 3 4 5 6 7

Saturday, January 9, 2010

Biju K Raveendran@BITS Pilani.

12

Unsigned Binary Arithmetic


Base-2 addition just like base-10!
Add from right to left, propagating carry
carry

10010 + 1001 11011

10010 + 1011 11101

1111 + 1 10000

10111 + 111 11110

Saturday, January 9, 2010

Biju K Raveendran@BITS Pilani.

13

Signed Integers
With n bits, we have 2n distinct values.
Assign about half to positive integers (1 through 2n-1 - 1) and about half to negative (- 2n-1 - 1 through -1) That leaves two values: one for 0, and one extra

Positive integers
Just like unsigned : zero in most significant (MS) bit 00101 = 5

Negative integers
Set MS bit to show negative, other bits are the same as unsigned 10101 = -5

Inference
MS bit indicates sign: 0=positive, 1=negative

Saturday, January 9, 2010

Biju K Raveendran@BITS Pilani.

14

Question
Given n bit string, What is the Maximum number we can represent in Signed Magnitude form? 1
2n-1

1
2n-2

1 .

. .

. .

. .

. .

.
22

.
21

1
20

The Maximum +ve value is : 2n-1 -1 The Maximum -ve value is : 2n-1 -1 Ex: Given 5 bits, Max. Max number in signed magnitude form is: 24 -1 =15 1 15 Min. number in signed magnitude form is: -24 -1= -15

Saturday, January 9, 2010

Biju K Raveendran@BITS Pilani.

15

How to represent Signed Integers


Signed magnitude representation 1s 1s complement representation 2s complement representation

Saturday, January 9, 2010

Biju K Raveendran@BITS Pilani.

16

Representation of Signed Integers Cont


Ex: n = 3 Signed magnitude +0 +1 +2 +3 -0 -1 -2 -3
Biju K Raveendran@BITS Pilani. 17

000 001 010 011 100 101 110 111


Saturday, January 9, 2010

Limitations!
Problems with sign-magnitude representation!
Two representations of zero (+0 and 0) Arithmetic circuits are complex to implement (hardware complexity is more)

Because:
How to add two sign-magnitude numbers?
e.g., try 2 + (-3) 00010 10011 10101 => -5 ??

Saturday, January 9, 2010

Biju K Raveendran@BITS Pilani.

18

1s Complement Representation
Positive numbers representation is same as signed integers. Negative numbers represented by flipping all the bits of corresponding positive numbers For Example: +5 is represented as 00101 p -5 is represented by 11010 This representation was used in some early computers

Saturday, January 9, 2010

Biju K Raveendran@BITS Pilani.

19

Question
Given n bit string, What is the Maximum number we can represent in 1s complement form? The Maximum +ve value is : 2n-1 -1 The Maximum -ve value is : 2n-1 -1 Ex: Given 5 bits, Max. Max number in signed magnitude form is: 24 -1 =15 1 Min. number in signed magnitude form is: -24 -1= -15

Saturday, January 9, 2010

Biju K Raveendran@BITS Pilani.

20

Representation of Signed Integers Cont


Ex: n = 3 Signed magnitude +0 +1 +2 +3 0 1 2 3
Biju K Raveendran@BITS Pilani.

000 001 010 011 100 101 110 111


Saturday, January 9, 2010

1s complement +0 +1 +2 +3 3 2 1 0
21

Example: 1s Complement p p
Example 1: How will we represent -12 in 1s complement form in 5 digits? Step1: Take +12 in binary representation 01100 Step2: Flip all the bits of the above 10011 Inference -12 representation in 1s complement form: 10011 +12 representation in 1s complement form: 01100

Saturday, January 9, 2010

Biju K Raveendran@BITS Pilani.

22

Representation of Signed Integers


Ex: n = 4 Signed magnitude 1s Complement

0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
Saturday, January 9, 2010

+0 +1 +2 +3 +4 +5 +6 +7 0 1 2 3 4 5 6 7
Biju K Raveendran@BITS Pilani.

+0 +1 +2 +3 +4 +5 +6 +7 7 6 5 4 3 2 1 0
23

Limitations!
Problems with sign-magnitude and 1s complement!
Two representations of zero (+0 and 0) Arithmetic circuits are complex to implement (hardware complexity is more)

Because:
How to add two sign-magnitude numbers?
e.g., try 2 + (-3) 00010 10011 10101 => -5 ??

How to add two ones complement numbers?


e.g., try 4 + (-3) g, y ( )
Saturday, January 9, 2010 Biju K Raveendran@BITS Pilani. 24

You might also like