0% found this document useful (0 votes)
89 views42 pages

BITS Pilani: Computer Programming

The document discusses different methods for representing signed integers in binary, including signed magnitude, 1's complement, and 2's complement representations. It explains how 2's complement addresses limitations with the other methods by representing negative numbers through flipping bits and adding one. The document also covers converting between binary, hexadecimal, and decimal number systems.

Uploaded by

kiranbits
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views42 pages

BITS Pilani: Computer Programming

The document discusses different methods for representing signed integers in binary, including signed magnitude, 1's complement, and 2's complement representations. It explains how 2's complement addresses limitations with the other methods by representing negative numbers through flipping bits and adding one. The document also covers converting between binary, hexadecimal, and decimal number systems.

Uploaded by

kiranbits
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

BITS Pilani

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

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Why Number System ?

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

Binary Hex Decimal Binary Hex Decimal


0000 0 0 1000 8 8
0001 1 1 1001 9 9
0010 2 2 1010 A 10
0011 3 3 1011 B 11
0100 4 4 1100 C 12
0101 5 5 1101 D 13
0110 6 6 1110 E 14
0111 7 7 1111 F 15
4
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Converting from Binary to Hexadecimal

Every four bits is a hex digit.


– Start grouping from right-hand side
Example:
011101010001111010011010111

3 A 8 F 4 D 7

This is not a new machine representation,


just a convenient way to write the number.

5
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Exercise

Convert a Hexadecimal Number into Binary


Number?
1. ADF6
2. DB69
3. CA36
4. 5678
5. DABC

6
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
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”

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

10010 10010 1111


+ 1001 + 1011 + 1
11011 11101 10000

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

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

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?

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
15
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
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
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

1. If leading bit is one, take two’s


complement to get a positive n 2n
0 1
number. 1 2
2 4
3 8
2. Add powers of 2 that have “1” in 4 16
the corresponding bit positions. 5 32

Assuming 8-bit 2’s complement numbers. 6 64


7 128
8 256
3. If original number was negative,
9 512
add a minus sign. 10 1024
22
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
EXERCISE 1
1. Represent (-26)10 in its signed magnitude form using 8-bits:
00011010
2. Represent (-26)10 in its 1s Complement form using 8-bits:
11100101

3. Represent (-26)10 in its 2s Complement form using 8-bits:


11100101
+1
11100110→ 2s Complement

4. What is the minimum bits required to represent (-26)?

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

−2n−1 ≤ Two's Complement ≤ 2n−1 − 1

24
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
2’s Complement Arithmetic

This presentation will demonstrate


• How the 2’s complement process can be used to
add (and subtract) binary numbers?

• The nice feature with Two's Complement is that


addition and subtraction of Two's complement
numbers works without having to separate the
sign bits (the sign of the operands and results is
effectively built-into the addition/subtraction
calculation).
25
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Using The 2’s Complement Process
Use the 2’s complement process to add together
the following numbers.

POS 9 NEG (-9)


+ POS  + 5 + POS  + 5
POS 14 NEG -4

POS 9 NEG (-9)


+ NEG  + (-5) + NEG  + (-5)
POS 4 NEG - 14 27
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
POS + POS → POS Answer
If no 2’s complement is needed, use regular binary
addition.

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

+ (-5) ⎯→ + 11111011 In Previous Slides

-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)

1. Add the following binary numbers in 2’s complement;


Mention overflow/underflow if any.
(00101010)2 + (01010110)2

2. Add the following decimal numbers using 2’s complement 8 bit


representation:
(+90)10 + (-110)10
3. Add the following binary numbers assuming 2’s complement 5 bit
representation
(11001)2 + (111)2 =( ?? )2

4. Add the following binary numbers assuming 2’s complement 8 bit


representation
(11001)2 + (0110)2

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.

Assume 8-bit representation of integers.


1. R = +47 and S = +47
2. R = +17 and S = +16
3. R = -17 and S = -17
4. R = -95 and S = +37
37
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Exercise – Number conversion

1.Convert (7FA8BB)16 into its equivalent binary number


2.Add the following hexadecimal numbers assuming 16 bit binary
numbers assuming 2’s complement representation and answer
should be in hexadecimal.
a.(025B)16 + (26DE)16
3.Convert (235)16 into its equivalent decimal number
4.Convert (456)10 into its equivalent hexadecimal number
5.Convert (456)16 into its equivalent octal number
6.Convert the following unsigned binary number to Hexa decimal
(1110110010110010)2 = (??)16
7.Convert (234)5 into its equivalent decimal number .

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

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


❑Fixed point Representation?
Example:
❑Floating point representation?
----- Why?

40
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956
Floating Point Representation

• Non-integral binary numbers


– 0.123 = 1 × 10−1 + 2 × 10−2 + 3 × 10−3
– 0.1012 = 1 × 2−1 + 0 × 2−2 + 1 × 2−3 = 0.625
– 110.0112 = 4 + 2 + 0.25 + 0.125 = 6.375

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


DECIMAL TO BINARY
0.6875
 2

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

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


Converting 0.85 to binary

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

Large values: 6.023 x 1023 -- requires 79 bits


Small values: 6.626 x 10-34 -- requires >110
bits

Use equivalent of “scientific notation”: F x 2E


Need to represent F (fraction), E (exponent),
and sign.

BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956


COMPUTER PROGRAMMING
3/2/2022 45
BITS Pilani, Deemed to be University under Section 3 of UGC Act, 1956

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy