Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 16

Computer

Programming
CS F111
BITS Pilani
Dubai Campus
BITS Pilani
Dubai Campus

Data Representation
Computer Programs
• Algorithms + Data Structures = Programs
• Book by Nicolas Wirth (Turning Award winner)

• Any program = some computation performed on some data.


• CGPA Calculation : (grade x credits)/credits (for all courses)

• Computation is Mul, Add, Div etc.


• Data is grades, Credits etc.

• Humans : data representation is in decimal


• Computer : data representation is in binary

BITS Pilani, Dubai Campus


Decimal Numbers
• “Decimal” means that we have ten digits to use
• Symbols 0 to 9
• 10 is the base of the number system.
• Positional Number System => Value depends on the position
• What is 3,536 ?
- it is three thousand + five hundred + three tens + six one
- 3x103 + 5x102 + 3x101 + 6x100
- 1st 3 appears at position 3 & 2nd 3 appears in position 1

• For negative nos we use 2 more symbols + and –


+23, -56

BITS Pilani, Dubai Campus


Binary Numbers
• “Binary” means that we have two digits to use
• Symbols 0 to 1
• 2 is the base of the number system.
• Positional Number System => Value depends on the position
• What is 1011 ?
- it is one eight + zero four + one tows + one ones
- 1x23 + 0x22 + 1x21 + 1x10
- The 1 at position 3 has a weight of 23 (=8)
- The 1 at position 1 has a weight of 21 (=2)

• For negative nos we have multiple representations


- Sign Magnitude, 1’s Complement, 2’s complement
- 2’s Complement is used (advantaged explained later)
BITS Pilani, Dubai Campus
Computer uses Binary Num. Sys
• “Binary” has 2 states 0 & 1 known as bits (binary digits)
• Digital ckts for computation like Add/Sub etc. can be
• Bit values represent physical values stored in variables
(in computer memory), eg credits, grade, pay, voltage etc.
• With a single digit decimal no we can represent a value 0 - 9
• For larger values we use multiple digits
• 3 digit decimal not can store values from 000 to 999
• each digit can take any of the 10 values. So 10 x 10 x 10 = 103 = 1000 values.
• ‘n’ digit decimal no can store 10n values
• llIrly with 2 bit no we have two values (0/1) at 2 positions
• 2 x 2 = 22 = 4 values (stages) ie. 00, 01, 10, 11
• 3 bit no represents 23 = 8 states = 000 to 111
• n bit no represents 2n states.

BITS Pilani, Dubai Campus


Type of Data
• Data type: representation and operation with in computer

• Different Data Types representation used


•Number : unsigned, Singed , floating point etc
•Text : Characters, strings
• Logical : True, False,
• Images,: pixels, colors.,
• Instructions: programs.

• First we consider Numeric Data type


• Unsigned,
• Signed
• Floating point

BITS Pilani, Dubai Campus


Unsigned Integers
• Weighted Positional Notation
•3 is weighted as 300 as 3 is at position 2, while 9 is only 9 as its at position 0

• 2 digit decimal no 3 bit binary no


1001 1000 Value
0
0 1 1

0 9 9
1 0 10
..
1 9 19
2 0 20

9 9 99

BITS Pilani, Dubai Campus


Decimal to Binary Conversion
• Continually divide the number by 2 and track the reminder
(43)10 = (?)2 2 43 R
2 21 1
2 10 1
2 5 0
2 2 1
2 1 0
0 1

(43)10 = (101011)2
(101011)2 = 1x25 + 0x24 + 1x23 + 0x22 + 1x21 + 1x20
= 32 + 0- + 8 + 0 + 2 + 1
= (43)10
• -ve no => ??

BITS Pilani, Dubai Campus


Sign Magnitude Representation
• The most significant bit is used for sign (0: +ve, 1:-ve no)
• Use binary division to convert from decimal to binary
• Bit pattern (format) of Sign Magnitude Representation is
s m m m m m m m (8 bits : s is sign bit & 7 bits for magnitude)
• +20 : R
s m m m m m m m
2 20 pos 7 6 5 4 3 2 1 0
2 10 0 Bits 0 0 0 1 0 1 0 0
2 5 0
2 2 1
2 1 0
0 1 Result = (00010100)2 = 1x24 + 1x22 = (20)10
• -20 : Division procedure remains the same, only sign changes
s m m m m m m m Result = (10010100)2
pos 7 6 5 4 3 2 1 0
Bits 1 0 0 1 0 1 0 0 =1x24 + 1x22 = (20)10
= -20

BITS Pilani, Dubai Campus


1’s Complement Representation
• aUse binary division to convert from decimal to binary
• b) For +ve nos: the result same as that from a)
For –ve nos: complement (invert) each bit from a).
+20 : R
s m m m m m m m
2 20
pos 7 6 5 4 3 2 1 0
2 10 0 Bits 0 0 0 1 0 1 0 0
2 5 0
2 2 1
2 1 0 Result = (00010100)2
• -20 : 0 1 procedure remains the same, complement each
Division bit
Result = (11101011)2
s m m m m m m m
pos 7 6 5 4 3 2 1 0
Bits 0 0 0 1 0 1 0 0
1’s Comp 1 1 1 0 1 0 1 1

BITS Pilani, Dubai Campus


2’s Complement Representation
• a) Use binary division to convert from decimal to binary
• b) For +ve nos: the result same as that from a)
For –ve nos: complement (invert) each bit from a) and add one
R
+20: 2 20
2
10 0
2
5 0
2
2 1 Result = (00010100)2
2
1 0
-20:Division
0 1 procedure remains the same, complement each
bit, add 1
Result = (11101100)2
s m m m m m m m
pos 7 6 5 4 3 2 1 0
Bits 0 0 0 1 0 1 0 0
1’s Comp 1 1 1 0 1 0 1 1
add 1 1
Ans 1 1 1 0 1 1 0 0

BITS Pilani, Dubai Campus


Binary Addition
2-Input Output 1 bit Addition
0
A B Sum Carry 0
0 A B A B
0 0 0 0 0 0
1
0 1 1 0 0 1 1
1 0 1 0 0 C
1 1 0 1 1 0 1
1
0 1 0
0 Carry Sum Carry Sum
3-Input Output 0
A B C Sum Carry 0 0 0
0 • Add 2 bits (columnwise)
0 0 0 0 0 1
0 1
• Propagate carry (if any) to next
0 0 1 1 0 • column
0
0 1 0 1 0
1
0 1 1 0 1 1 Carry 1 1 0 0
1 0 7 0 1 1 1
1 0 0 1 0 + 2 0 0 1 0
9 1 0 0 1
1 0 1 0 1
1
1 1 0 0 1 1
1
1 1 1 1 1 1 1

BITS Pilani, Dubai Campus


Binary Addition (2’s Comp)

• Add the following nos is 2’s complement representation


• (+17) + (+22) = (+39) in 8 bits
• (+17) : 2 17 R (+22) : 2 22 R • Add 2 bits (columnwise)
2 8 1 2 11 0
• Propagate carry (if any) to
2 4 0 2 5 1
next column
2 2 0 2 2 1
2 1 0
• Ignore carry from MSB (if any)
2 1 0
0 1 0 1

0 0 0 1 0 0 0 1 0 0 0 1 0 1 1 0

Carry 0 0 0 1 0 0 0 0 0
(+17) 0 0 0 1 0 0 0 1
+ (+22) 0 0 0 1 0 1 1 0
(+39) 0 0 1 0 0 1 1 1

BITS Pilani, Dubai Campus


Binary Addition (2’s Comp)

• Add the following nos is 2’s complement representation


• (+24) + (-17) = (+7) in 8 bits 2 17 R
• (+24) 2 24 R (-17) : Binary rep of 17 : 2 8 1
2 4 0
2 12 0 Comp each bit 2 2 0
2 6 0 Add 1 2 1 0
2 3 0 0 1
2 1 1
0 1 0 0 0 1 0 0 0 1
17
0 0 0 1 1 0 0 0 1’sComp 1 1 1 0 1 1 1 0
Add 1 0 0 0 0 0 0 0 1

-17 1 1 1 0 1 1 1 1
Carry 1 1 1 1 1 0 0 0 0
'(+24) 0 0 0 1 1 0 0 0 • Add 2 bits (columnwise)
• Propagate carry (if any) to
+ (-17) 1 1 1 0 1 1 1 1
next column
(+7) 0 0 0 0 0 1 1 1 • Ignore carry from MSB (if any)

BITS Pilani, Dubai Campus


Binary Addition (2’s Comp)

• Add the following nos is 2’s complement representation


• (-24) + (-17) = (-41) in 8 bits 2 24 R 2 17 R
2 12 0
(-17): 2 8 1
(-24): Binary rep 2 4 0
Binary rep 2 6 0
of 17 2 2 0
of 24 2 3 0 2 1 0
2 1 1 0 1
24 0 0 0 1 1 0 0 0 0 1
Comp 1 1 1 0 0 1 1 1
17 0 0 0 1 0 0 0 1
0 0 0 0 0 0 0 1 Comp 1 1 1 0 1 1 1 0
Add 1
1 1 1 0 1 0 0 0 Add 1 0 0 0 0 0 0 0 1
-24
-17 1 1 1 0 1 1 1 1
Carry 1 1 1 0 1 0 0 0 0
(-24) 1 1 1 0 1 0 0 0 • Add 2 bits (columnwise)
+ (-17) 1 1 1 0 1 1 1 1 • Propagate carry (if any) to
next column
(-41) 1 1 0 1 0 1 1 1
• Ignore carry from MSB (if any)

BITS Pilani, Dubai Campus

You might also like