Vous êtes sur la page 1sur 24

COMPUTER PROGRAMMING I ( (TA C162) )

Lecture 3 Representation of Binary Numbers

Todays Agenda
Representation of Binary Numbers
Unsigned, Signed Magnitude, 1s Compliment g g g p Done !!! 2s Complement

Base Conversion
Binary to Decimal and Decimal to Binary Decimal to Octal and Octal to Decimal Octal to Binary and Binary to Octal Binary to Hexadecimal and Hexadecimal to Binary

Tuesday, January 12, 2010

Biju K Raveendran@BITS Pilani.

How to represent Signed Integers


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

Tuesday, January 12, 2010

Biju K Raveendran@BITS Pilani.

Representation of Signed Integers Cont


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

000 001 010 011 100 101 110 111


Tuesday, January 12, 2010

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

Tuesday, January 12, 2010

Biju K Raveendran@BITS Pilani.

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

Tuesday, January 12, 2010

Biju K Raveendran@BITS Pilani.

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


Tuesday, January 12, 2010

1s complement +0 +1 +2 +3 3 2 1 0
7

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

Tuesday, January 12, 2010

Biju K Raveendran@BITS Pilani.

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
Tuesday, January 12, 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
9

Limitations of Signed magnitude & 1s complement 1s


Problems with sign-magnitude and 1s complement!
Two representations of zero (+0 and 0) Arithmetic circuits are complex to implement above representation (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 e g tr 4 + ( 3) (-3)
Tuesday, January 12, 2010 Biju K Raveendran@BITS Pilani. 10

A Good Representation
Should keep the logic circuits simple Representation of Negative Numbers should make Arithmetic & Logic Unit (ALU) simple Addition of an arbitrary integer with same magnitude but opposite sign should return 0
This can be accomplished by 2s complement representation. 2s representation A + (-A) = 0 From (2n-1-1) to +(2n-1-1) ALU adds 1 to successive representations

Mathematically we can represent as: REPRESENTATION(value+1) = REPRESENTATION(value) + REPRESENTATION(1)

Tuesday, January 12, 2010

Biju K Raveendran@BITS Pilani.

11

Twos Complement Representation


If number is positive or zero Normal binary representation If number is negative Start with positive number Flip every bit (i.e., take the ones complement) Then add one Example:

00101 (5) 11010 (1s comp) + 1 11011 (-5)

01001 (9) 10110 (1s comp) + 1 10111 (-9)

Tuesday, January 12, 2010

Biju K Raveendran@BITS Pilani.

12

Example
Represent -12 in 2s complement form (5 digits)
Step 1:Represent +12 in binary 01100 Step 2: Find 1s complement of that 10011 Step 3: Add 00001 to 1s complement 10100 Result (-12 in 2s complement form) is 10100
Tuesday, January 12, 2010 Biju K Raveendran@BITS Pilani. 13

Twos Complement Shortcut


To take the twos complement of a number:
Copy bits from right to left until ( py g (and including) the g) first 1 Flip remaining bits to the left

011010000 100101111 + 1 100110000

011010000
(1 s (1s comp) (flip) (copy)

100110000

Tuesday, January 12, 2010

Biju K Raveendran@BITS Pilani.

14

Advantage of 2s complement
Twos complement representation developed to make circuits easy for arithmetic.
For each positive number (X), assign value to its negative (-X) (X) ( X), such that X + (-X) = 0 with normal addition, ignoring carry out

00101 (5) + 11011 (-5) 00000 (0)

01001 (9) + 10111 (-9) 00000 (0)

Tuesday, January 12, 2010

Biju K Raveendran@BITS Pilani.

15

Example: p
2+(-3) =?? (in 5 digits) p p Express -3 in 2s complement form: +3 1s Complement of 3 2s Complement of 3 2 C l t f +2 + -3 Look at MSB 00011 11100 11101 i -3 i.e. 3 00010 + 11101 11111 1 So take 2s complement again 00001

Tuesday, January 12, 2010

Biju K Raveendran@BITS Pilani.

16

Twos Complement Signed Integers


MS bit is sign bit: it has weight (2n-1) Range of an n-bit number: (2n-1)through 2n-1 1.
The most negative number -(2n-1) has no positive counterpart (2 counterpart. 23 0 0 0 0 0 0 0 0 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 23 1 1 1 1 1 1 1 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 -8 8 -7 -6 -5 -4 -3 -2 -1 1
17

Tuesday, January 12, 2010

Biju K Raveendran@BITS Pilani.

What is Base?
Decimal Number System
Base is 10 All numbers are represented by 0 to 9

Binary Number System


Base i 2 B is All numbers are represented by 0 and 1

Octal Number System


Base is 8 All numbers are represented by 0 to 7

Inference
Maximum digit value in any number system is Base -1 1
Tuesday, January 12, 2010 Biju K Raveendran@BITS Pilani. 18

Converting Binary (2s C) to Decimal


8-bit 2s complement Number is of the form a7 a6 a5 a4 a3 a2 a1 a0 If leading bit ( 7) i one; th th number i l di (a is then the b is negative take twos complement to get a positive number; Otherwise number is positive. The Magnitude is: a6.26+a5.25+a4.24+a3.23+a2.22+a1.21+a0.20
n 2n
0 1 1 2 2 4 3 8 4 16 5 32 6 64 7 128

Add powers of 2 that have 1 in the 8 256 corresponding bit positions positions. 9 512 If original number was negative, affix a minus 10 1024 sign in front. Assuming 8 bit 2 complement numbers. A i 8-bit 2s l t b
Tuesday, January 12, 2010 Biju K Raveendran@BITS Pilani. 19

Example: Binary to Decimal Conversion


X = 01101000two Step 1: Leading bit is 0 no need to take 2s complement X = 01101000 Step 2: Add powers of 2 that have 1 in the corresponding bit positions. iti = 26+25+23 = 64+32+8 X = 104ten Step 3: Not required (Number is positive)
Tuesday, January 12, 2010 Biju K Raveendran@BITS Pilani. 20

More Examples
X = 00100111two = 25+22+21+20 = 32 4 2 1 2 2 2 32+4+2+1 X = 39ten X = 11100110two -X = 00011010 X = 24+23+21 = 16+8+2 = 26ten X = -26ten
Assuming 8-bit 2s complement numbers.
Tuesday, January 12, 2010 Biju K Raveendran@BITS Pilani. 21

n 2n
0 1 2 3 4 5 6 7 8 9 10 1 2 4 8 16 32 64 128 256 512 1024

Examples
X=11000111t two X=10001100two

Tuesday, January 12, 2010

Biju K Raveendran@BITS Pilani.

22

Algorithm
Consider a Number N (5-bit 2s complement) (5 bit 2 s Step 1:
Obtain the binary representation of magnitude of N

Step 2:
If N is positive, the current representation is final positive If N is negative, then form negative of this 2s complement representation

Tuesday, January 12, 2010

Biju K Raveendran@BITS Pilani.

23

Example 1: 5 4 5+4 Example 2: 9 12 -12 9 + (-12) Example 3: 3 -3 3 7 -7 -3 + ( 7) 3 (-7)


Tuesday, January 12, 2010

Addition & Subtraction 5 + 4 ? (in 5 bit notation)


00101 00100 01001 (9) 9 + - 12 ? (in 5 bit notation) 01001 01100 10100 11101 (-3) -3 + -7 ? (in 5 bit notation) 00011 11101 00111 11001 10110 ( 10) (-10)
Biju K Raveendran@BITS Pilani. 24

Vous aimerez peut-être aussi