You are on page 1of 15

TA C162 Computer Programming I

Today’s Agenda
Representation of Numbers
• Binary Numbers
• Unsigned Integers
 Non Positional
 Positional

• Signed Magnitude
• 1’s Complement

08/01/2009 1
TA C162 Computer Programming I

How do we represent data in a computer?


At the lowest level, a computer is an electronic
machine.
• Works by controlling the flow of electrons

Easy to recognize two conditions:


1. Presence of a voltage – we’ll call this state “1”
2. Absence of a voltage – we’ll call this state “0”

Could base state on value of voltage??


Problem:
Control and detection circuits more complex.
• Compare turning on a light switch to measuring or
regulating voltage
08/01/2009 2
TA C162 Computer Programming I

Computer is a binary digital system


Digital system:
• Finite number of symbols

Binary (base two) system:


• Has two states: 0 and 1

08/01/2009 3
TA C162 Computer Programming I

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.

• A sequence of two bits has four possible states:


00, 01, 10, 11

• A sequence of three bits has eight possible states:


000, 001, 010, 011, 100, 101, 110, 111

Inference:
• A sequence of n bits has 2n possible states.

08/01/2009 4
TA C162 Computer Programming I

What kinds of data do we need to


represent?
Numbers – signed, unsigned, integers, floating point,
complex, rational, irrational, …
Text – characters, strings, …
Images – pixels, colors, shapes, …
Sound
Logical – true, false
Instructions

08/01/2009 5
TA C162 Computer Programming I

Unsigned Integers
Non-positional notation
No weightage for the position {0th, 1st, …etc.. }

• Could represent a number (“5”) with a string of ones (“11111”)


• Problems?

Weighted positional notation


• like decimal numbers: “329”
• “3” is worth 300, because of its position, while “9” is only worth
most least
9
329 significant
101 significant

102 101 100 22 21 20


3x100 + 2x10 + 9x1 = 329 1x4 + 0x2 + 1x1 = 5

08/01/2009 6
TA C162 Computer Programming I

Unsigned Integers (cont.)


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

08/01/2009 7
TA C162 Computer Programming I

Unsigned Binary Arithmetic


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

carry

10010 10010 1111


+ 1001 + 1011 + 1
11011 11101 10000

10111
+ 111
11110

08/01/2009 8
TA C162 Computer Programming I

Signed Integers
With n bits, we have 2n distinct values.
• Assign about half to positive integers (1 through 2n-1)
and about half to negative (- 2n-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

08/01/2009 9
TA C162 Computer Programming I

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

1 1 1 . . . . . . 1
2n-1 2n-2 . . . . . 22 21 20

The Maximum +ive value is : 2n-1 -1


The Maximum -ive value is : 2n-1 -1

Ex: Given 5 bits,


Max. number in signed magnitude form is: 24 -1
=15
Min. number in signed magnitude form is: -24
-1= -15
08/01/2009 10
TA C162 Computer Programming I

Representation of Signed Integers Cont…


Ex: n = 3
Signed
magnitude
000 +0
001 +1
010 +2
011 +3
100 -0
101 -1
110 -2
111 -3

08/01/2009 11
TA C162 Computer Programming I

1’s 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
-5 is represented by 11010

This representation was used in some early computers

08/01/2009 12
TA C162 Computer Programming I

Example: 1’s Complement


Example 1:
How we represent -12 in 1’s complement form

Step1: Take +12 in binary representation


 01100
Step2: Flip all the bits of the above
 10011
Inference
-12 representation in 1’s complement form: 10011
+12 representation in 1’s complement form: 01100

08/01/2009 13
TA C162 Computer Programming I

Representation of Signed Integers


Ex: n = 3
Signed 1’s Complement
magnitude
000 +0 +0
001 +1 +1
010 +2 +2
011 +3 +3
100 -0 -3
101 -1 -2
110 -2 -1
111 -3 -0

08/01/2009 14
TA C162 Computer Programming I

Limitations!
Problems with sign-magnitude and 1’s complement!
• Two representations of zero (+0 and –0)
• Arithmetic circuits are complex to implement above
representation
Because:
 How to add two sign-magnitude numbers?
– e.g., try 2 + (-3)
00010
10011
10101 => -5 ??

 How to add two one’s complement numbers?


– e.g., try 4 + (-3)

08/01/2009 15

You might also like