BITS Pilani: Computer Programming
BITS Pilani: Computer Programming
Computer Programming
Lecture 2
BITS Pilani Dr. Kiran D C
Pilani Campus SEMTZC163
Today’s Agenda
Binary Numbers
Unsigned Integers √
Non Positional
Positional
• Signed Integers
Signed Magnitude
1’s Complement
2’s complement
Arithmetic Operation
Floating Point
--- IEEE 754 Representation
COMPUTER PROGRAMMING
3/2/2022 3
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Hexadecimal Notation
It is often convenient to write binary (base-2) numbers as hexadecimal (base-
16) numbers instead.
Why?
– Fewer digits -- four bits per hex digit
– Less error prone -- easy to corrupt long string of 1’s and 0’s
3 A 8 F 4 D 7
5
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Exercise
6
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
How do we represent data in a computer?
7
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Unsigned Binary Arithmetic
Base-2 addition – just like base-10!
– Add from right to left, propagating carry
carry
10111
+ 111
11110
10
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Signed Integers Representation
• Signed Magnitude
• 1’s Complement
• 2’s Complement
COMPUTER PROGRAMMING
3/2/2022 11
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Representation of Signed Integers
Ex: n = 3
Signed
magnitude
000 +0
001 +1
010 +2
011 +3
100 -0
101 -1
110 -2
111 -3
12
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
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
13
1’s Complement Representation
•Positive numbers representation:
– same as signed integers.
•Negative numbers representation:
– by flipping all the bits of corresponding
positive numbers
For Example:
+5 is represented as 00101
-5 is represented by 11010
14
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Example: 1’s Complement
How we represent -12 in 1’s complement form?
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
16
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
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 ??
17
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Two’s Complement Representation
If number is negative
– Start with positive number
– Flip every bit (i.e., take the one’s complement)
– Then add one
Example:
00101 (5) 01001 (9)
11010 (1’s comp) 10110 (1’s comp)
+ 1 + 1
11011 (-5) 10111 (-9)
18
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Representation of Signed Integers
Ex: n = 3
Signed 1’s 2s
magnitude Complement Complement
000 +0 +0 +0
001 +1 +1 +1
010 +2 +2 +2
011 +3 +3 +3
100 -0 -3 -4
101 -1 -2 -3
110 -2 -1 -2
111 -3 -0 -1
19
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Two’s 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.
23 22 21 20 23 22 21 20
0 0 0 0 0 1 0 0 0 -8
0 0 0 1 1 1 0 0 1 -7
0 0 1 0 2 1 0 1 0 -6
0 0 1 1 3 1 0 1 1 -5
0 1 0 0 4 1 1 0 0 -4
0 1 0 1 5 1 1 0 1 -3
0 1 1 0 6 1 1 1 0 -2
0 1 1 1 7 1 1 1 1 -1
20
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Example: 2’s complement
2+(-3) =??
Express -3 in 2’s complement form:
+3 → 00011
1’s Complement of 3 →11100
2’s Complement of 3 →11101 i.e. -3
2 →00010
+ +
-3 →11101
11111
Look at MSB ➔ 1, So take 2’s complement again
00001
21
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Converting Binary (2’s C) to Decimal
23
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
EXERCISE 1
2. Represent (-10)10 in its 1’s complement form using 8 bits
00001010-> 11110101
3. Represent (-12)10 in its 2’s complement form using 8-bits
00001100->11110011->11110100
4. How many bits required to represent (-128)10 in 2’s complement and 1’s complement
representation
Range of an n-bit number: -2n-1 through 2n-1 – 1 -→ 2’s complement
1’s complement:
The Maximum +ive value is : 2n-1 -1
The Maximum -ive value is : 2n-1 -1
5. What is the maximum and minimum number we can represent in 2’s complement form
using 16 bits
24
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
2’s Complement Arithmetic
9 ⎯→ 00001001
+ 5 ⎯→ + 00000101
14 ⎯ 00001110
28
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
POS + NEG → POS Answer
Take the 2’s complement of the negative number and
use regular binary addition.
9 ⎯→ 00001001
+ (-5) + 11111011
4 ⎯ 1]00000100
8th Bit = 0: Answer is Positive
Disregard 9th Bit
00000101
2’s
11111010 Complement
Process
+1
11111011
29
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
POS + NEG → NEG Answer
Take the 2’s complement of the negative number and
use regular binary addition.
(-9) 11110111
+ 5 ⎯→ + 00000101
-4 ⎯ 11111100
8th Bit = 1: Answer is Negative
11111100 00001001
To Check: 2’s
Perform 2’s
Complement
00000011 11110110 Complement
Process
On Answer +1 +1
00000100 11110111
30
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
NEG + NEG → NEG Answer
Take the 2’s complement of both negative numbers and
use regular binary addition.
2’s Complement
(-9) ⎯→ 11110111 Numbers, See
Conversion Process
-14 ⎯ 1]11110010
8th Bit = 1: Answer is Negative
Disregard 9th Bit
11110010
To Check:
Perform 2’s
Complement
00001101
On Answer +1
00001110
31
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
2’s Complement Arithmetic
• If the result of an arithmetic operation is to too
large (positive or negative) to fit into the resultant
bit-group, then arithmetic overflow occurs. It is
normally left to the programmer to decide how to
deal with this situation.
32
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Overflow/Underflow Rule for addition
If you add two positive numbers and the result appears to be negative (i.e., msb = 1)
then you have an overflow. This will generate an exception in your program.
If you add two negative numbers and the result appears to be positive (i.e., msb = 0)
then you have an underflow.
COMPUTER PROGRAMMING
3/2/2022 33
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Underflow Rule for addition
Example:
Using 4-bit Two's Complement numbers (−8 ≤ x ≤ +7)
(−7) 1001 +
(−6) 1010
------------
(−13) 1] 0011 = 3 :
Underflow (largest −ve number is −8)
COMPUTER PROGRAMMING
3/2/2022 34
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
COMPUTER PROGRAMMING
3/2/2022 35
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
EXERCISE-2 (ARITHMETIC OPERATION)
36
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
EXERCISE 3
• Assume two’s complement representation.
• Find R - S for each of the following.
• Determine whether or not an overflow occurs.
38
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Today’s Agenda
Binary Numbers
Unsigned Integers √
Non Positional
Positional
• Signed Integers √
Signed Magnitude
1’s Complement
2’s complement
Arithmetic Operation √
Floating Point
--- IEEE 754 Representation
40
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Floating Point Representation
1.3750 …1
0.375
2 (0.6875)10 = (0.1011)2
…0
0.750
2
…1
1.500
0.500
2
…1
1.0
0
0.85 × 2 = 1.7
0.7 × 2 = 1.4
0.4 × 2 = 0.8
0.8 × 2 = 1.6
0.6 × 2 = 1.2
0.2 × 2 = 0.4
0.4 × 2 = 0.8
0.8 × 2 = 1.6
Very Large and Very Small:
Floating-Point