Anda di halaman 1dari 20

Birla Institute of Technology & Science, Pilani

Second Semester 2015-2016, Computer Programming [CS F111]


Week #1
Tutorial Sheet #1 Date:___/_____/________
Name: ______________ _____________ID: ______________________ Lec Sec/Lab
Sec:______/_____
Note: In this worksheet
1. We are using the notation ^ to denote power.

Section 1 Representing Unsigned Integer:


Positive integers are represented using a place-value binary system (base 2),
similar to the place value decimal system (base 10).
Example 1: (1543)10 = 1x1000 + 5 x 100 + 4 x 10 + 3 = 1 x (10)^3 + 5 x
(10)^2 + 4 x (10) + 3.
Exercise 1: Expand the number as in example 1.
(20709)10 = _______________________________________________________________.
In binary system the base is 2. So you want to represent a number such that each
position in the number represents an increasing power of 2.
Example 2: (39)10 = 32 + 4 + 2 + 1 = (1 x 2^5) + (0 x 2^4) + (0 x 2^3) + (1
x 2^2) + (1 x 2^1) + (1x2^0)
Note that in each term the coefficient of the power of 2 is always 0 or 1.
How can we always write a decimal number in binary system?
Here is a method (also we call it algorithm) to do it.
Method 1:
We are trying to represent the number 39 as the sum of powers of two starting from
the largest. Find the largest power of 2 which is not more than 39. It is 32 = 2^5.
Subtract it: 39 - 32 = 7. Now we need to represent 7 as the sum of powers of 2.
Continue as before: the biggest power of two which is not more than 7 is 4 = 2^2.
Subtract it: 7 - 4 = 3. Now we need to represent 3 as the sum of powers of 2.
Continue as before: the largest power of two which is not more than 3 is 2 = 2^1.
Subtract it: 3 - 2 = 1. We can represent 1 as 2^0. We got that 39 = 2^5 + 2^2 +
2^1 + 2^0. This is the same as above.
Now attempt these two exercises using this algorithm and write their
expansions with base 2:

Exercise 2: (52)10 =
_____________________________________________________________________.
Exercise 3: (100)10 =
____________________________________________________________________.
In decimal system we have 10 symbols namely 0, 1, 2, 3, ,9 to represent a
number. In the same manner a binary system will have 2 symbols namely 0 and 1.
If we look at example 2, then we can represent the number 39 in decimal
as 100111 in binary. Note that they are nothing but the coefficients of the power
of 2 in the expansion of 39. The left most digit is the coefficient of highest power i.e
2^5 and rightmost digit is coefficient of 2^0 terms. They are respectively called as
Most Significant Bit and Least Significant Bit.

So now quickly write the binary representation of 52 and 100 as given in


exercise 2 and 3 respectively.
Exercise 4: (52)10 = (___________ ___________________________)2.
Exercise 5: (100)10 = (______________________________________)2.

Lets look at a more formal method to convert a number from decimal to


binary.
Method 2:
To convert a decimal number into a binary number, carry out successive divisions by
2 and use the reminders of the successive divisions.
Example 3: Convert the decimal number 39 (example 2) to a binary
number.
39 2 = 19, remainder = 1 , least significant bit, goes to the right.
19 2 = 9 , remainder = 1
9 2 = 4 , remainder = 1
4 2 = 2 , remainder = 0
2 2 = 1 , remainder = 0
2 1 = 0 , remainder = 1 , Most significant bit, goes to the left.
So we write the binary as 100111 from bottom remainder to topmost
remainder.
Now attempt these two exercises using this algorithm:

Exercise 6 : (123)10 = (___________ ___________________________)2.


Exercise 7 : (9034)10 = (___________ ___________________________)2.

Each of these representations has different number of 0s and 1s. In a computer we


use a fixed number of bits to represent a number. For example, lets say a computer
uses 8 bits (also called a byte) to represent an unsigned integer.
So can you tell how many different representations can be done using 8 bits? Of
course, it is easy! Each bit can store either 0 or 1 so can hold 2 different values,
hence 8 bits will be able to hold 2^8 different representations. So a byte can hold
unsigned integers 0 to 255 (2^8 -1 ).
Answer the following:
Exercise 8: If a computer uses 16 bits to hold an unsigned integer then it can handle
_____________ representations.
Exercise 9: Assume that there are about 400 students in your class. If every student
has to be assigned a unique bit pattern, what is the minimum number of bits
required to do this? ________________
Also, how many more students can be admitted to the class without requiring
additional bits for each students unique bit pattern?__________________.
Interestingly, we can represent the number 0 or 1 using 1 bit. Then how can we use
8 bits or 16 bits to store the same number.
Well (0)10 = (0)2 and (0)10 = (0000 0000)2 are representation of 0 using 1 bit and 8
bits respectively.

Lets convert 100 and 255 to binary using method 2. Make sure you use 8
bits to represent them.
Exercise 10: (100)10 = (___________ ___________________________)2.
Exercise 11: (255)10 = (___________ ___________________________)2.
Exercise 12:
Represent the following unsigned decimal numbers in binary using (a) minimum
number of bits, and (b) using 16-bit representation.
a) 102 : ____________________________________________(min bits)
________________________________________________(16 bits)
b) 65000 :__________________________________________ (min bits)

________________________________________________(16 bits)

How to convert a number given in binary to decimal? The answer lies in its
expansion. So for example 10110 in binary is 1x2^4 + 0 x2^3 + 1 x 2^2 + 1 x 2^1
+ 0 ^ 2^0 = 16 + 4 + 2 = 22 in decimal. You should try out all the binary
representations done above back to decimal to verify your answers.

Section 2 Signed integers:


We have seen how to represent an unsigned integer in binary. What about signed
integers i.e. numbers like -2, -15 etc.?
There are three ways to do it. The first method is called signed magnitude
representation.
Method 1 (Signed Magnitude Representation)
It uses one bit (usually the leftmost) to indicate the sign. "0" indicates a positive
integer, and "1" indicates a negative integer. The rest of the bits are used for the
magnitude of the number.
Example 4: Give the signed magnitude representation of -2410.
1001 1000
The leftmost 1 (underlined) represents that it is negative number. The
magnitude is 24 (in 7-bit binary).
So it is evident that we can represent, 0 to 127 using 7 bits and -0 to -127 using
these 7 bits. For the formal one, leftmost bit will be 0 and for the later one leftmost
bit will be 1. Note that 0 and -0 have different representations 0000 0000 and 1000
0000. This is not good for computation. We would rather prefer a unique
representation of 0.

Exercise 13: Represent the following numbers in sign-magnitude using


minimum number of bits and using 16 bits.
-95 (minimum number of bits) ____________________________________________________.
-95 (16 bits) ___________________________________________________________________.

-64 (minimum number of bits) ____________________________________________________.


-64 (16 bits) ___________________________________________________________________.
64 (minimum number of bits) ____________________________________________________.
64 (16 bits) ___________________________________________________________________.

The next method to represent negative numbers is called ones complement


representation.

Method 2 (Ones complement Representation)


If all bits in a byte are inverted by changing each 1 to 0 and each 0 to 1, we have
formed the ones complement of the number.
Example 5: Represent -24 in ones complement form.
First take the binary representation of 24. It is 0001 1000 using 8 bits.
Then invert every bit, so 1110 0111 is -24 in ones complement form.
Note that in this representation 0 is represented as 0000 0000 using 8 bits. But -0
will be represented as 1111 1111. So we still have the problem two different
representations of 0. Also note that we can represent number from -127, -126,,-0,
0, 1, 126, 127. The numbers with leftmost bit as 1 are negative numbers.
Exercise 14: Represent the following numbers in ones complement using
minimum number of bits and using 16 bits.
-95 (minimum number of bits) ____________________________________________________.
-95 (16 bits) ___________________________________________________________________.
-64 (minimum number of bits) ____________________________________________________.
-64 (16 bits) ___________________________________________________________________.
64 (minimum number of bits) ____________________________________________________.
64 (16 bits) ___________________________________________________________________.

Method 3 (Twos complement Representation)


Twos complement representation is the most common way to represent negative
integers. The way to represent a negative integer using two complement is given as
follows.
Start with an N-bit representation of an integer.
To calculate the N-bit representation of the negative integer:
1.

Reflect each bit of the bit pattern (change 0 to 1, and 1 to 0).

2.

Add one.

Example 6: Represent -24 in twos complement form.


First take the binary representation of 24. It is 0001 1000 using 8 bits.
Then invert every bit, so 1110 0111 is -24 in ones complement form. Then
add 1 to it.
1110 0111
+0000 0001
1110 1000

A simpler way to do it is as follows. Write the binary form of positive


integer. Then start traversing the bits from rightmost end till you hit first
1. Then invert all the bits to the left of it.
For example 24 in binary is 0001 1000. We traverse from right end till we
hit first 1. Then we invert everything to the left of it so we get 1110 1000.

Exercise 15: Represent the following numbers in twos complement using


minimum number of bits and using 16 bits.
-95 (minimum number of bits) ____________________________________________________.
-95 (16 bits) ___________________________________________________________________.

-64 (minimum number of bits) ____________________________________________________.


-64 (16 bits) ___________________________________________________________________.
64 (minimum number of bits) ____________________________________________________.
64 (16 bits) ___________________________________________________________________.

Given a number in 2s complement form, the magnitude of the corresponding


decimal can be easily obtained by taking the bit wise complement and adding 1 to
it. The sign is ve if the leftmost bit is 1 in the 2s complement otherwise it is 0. For
example 1110 1000 represents what number in decimal? So taking the invert gives
00010111 and adding 1 to it gives 00011000. So the magnitude is 24. Since the
leftmost bit of the 2s complement is 1 it is -24.
Exercise 16: Verify all the results you have obtained in exercise 16.

Section 3 Binary Arithmetic:


In order to understand binary addition, we need to understand 1 bit addition.
0+0=0
1+0=1
0+1=1
1 + 1 = 10
Note that 10 is the representation of 2 which we get as sum of 1 with itself.
Also

To add two N-bit (representations of) integers: Proceed from right-to-left,


column-by-column, until you reach the left-most column. For each column, perform
1-bit addition. Write the carry-out of each column above the column to its left. The
bit is the left column's carry-in.
So if we have to add say two 4 bits integers.
Carry out
0 0 0 1 (1)
0 0 1 1 (3)
0

1
0 0 0 1 (1)
0 0 1 1 (3)
00

1
0 0 0 1 (1)
0 0 1 1 (3)
100

0
0 0 0 1 (1)
0 0 1 1 (3)
0100

So lets say we want to subtract 7 -2. This is same as adding 7 + (-2). So lets see
what happens
Example 7:
0111 (7)
+1110 (-2)
10101
Ignore the last carry out i.e. 1 in this case. So the result is 0101 = 5 = 7 2.
Exercise 17: Compute the following

0 0 1 0 (2)

1 1 1 0 (-2)

+ 0 1 0 1 (5)

+ 1 1 1 0 (-2)

Lets say we want to compute 10 + 10. What happens then?


1010 (10)
+1010 (10)
10100
Now on ignoring the last carry out bit we get the answer as 0100 = 4 which is not
equal to 20. So what went wrong? The reason this happened is that we are using 4
bits to represent our numbers. But then the maximum unsigned integer that can be
stored using 4 bits is 15 which is less than 20. So we have an overflow.

Exercise 18: The following binary numbers are 4-bit 2's complement binary
numbers. Which of the following operations generate overflow?
1100
+0 0 1 1

Y/N

1100
+0100

Y/N

0111
+0001

Y/N

0111
+1001

Y/N

For overflow detection, other than the technique said in recorded lectures, the
following can also be used:
Suppose A and B are two numbers and A+B = C. Checking the sign of A, B, and C
can tell whether overflow has occurred or not. If the sign of A and B is same (either
positive or negative), overflow will occur if the sign of C is different from A and B.
This is because if two positive numbers are added together, result can never be
negative and vice versa. Check this out in Q6 above.
Exercise 19: What if the sign of A and B is different?

Birla Institute of Technology & Science, Pilani


Second Semester 2015-2016, Computer Programming [CS F111]
Week #1
Tutorial Sheet #2 Date:___/_____/________
Name: ______________ _____________ID: ______________________ Lec Sec/Lab
Sec:______/_____

Section 4: Sign Extensions


Given a number represented using n number of bits, how can we represent the
same number without changing its sign using m (> n) bits? This is achieved using
Sign extension operation. It is a very common operation required in computer

arithmetic. This is done by appending digits to the most significant side of the
number, following a procedure dependent on the particular signed number
representation used.

Example 8: Is six bits are used to represent the unsigned number 00


1001 (+9 in decimal) then it can be sign extended to 8 bits by replicating
the leftmost bit. So we can represent its as 0000 1001 after sign
extension. In order to increase it to 16 bits, replicate the leftmost bit i.e 0
required number of times, i.e 0000 0000 0000 1001. Thus, both the value
and the fact that the value was positive are maintained.

Example 9: If 6 bits are used to represent -9 using 2s complement form


representation as 11 0111 then it can be sign extended to 8 bits by
replicating the leftmost bit called sign bit required number of times i.e
1111 0111. You should verify that its still represents -9. The same can
be represented in 16 bits by doing a sign extension 1111 1111 1111
0111. Thus, by padding the left side with ones, the negative sign and the
value of the original number are maintained.

Exercise 20: Convert these 6 bits, signed integers represented in 2s


complement to 16 bits.
110110 = ______(value in decimal)=_____________________________________________(16
bits ext).
010110 = ______(value in
decimal)=_____________________________________________(16 bits ext).

Here one needs to be careful, when sign extending a negative number represented
using sign magnitude representation. Lets say 1000 1001 is the sign magnitude
representation of -9 using 8 bits. Then in order to sign extend it to 16 bits you will
have to write 1000 0000 0000 1001. In this manner the value as well as the sign in
sign magnitude representation will be preserved.

Section 5 Hexadecimal Representation:

Inside a computer, everything is stored as a sequence of bytes. Bytes are sequence


if eight bits. Thus rather than writing all the bits, perhaps for brevity, we would like
to use a more comprehensive notation. So we want to choose a set of bits and
quickly find an equivalent symbol in the alternate system. It so happens that we can
use octal system where 8 symbols will be used, which means 3 bits can be taken
together to give 2^3 = 8 representations and hence can be mapped to these 8
symbols. Another method is to use Hexadecimal System where 16 symbols will be
used, which means 4 bits can be taken together to give 2^4 = 16 representations
and hence can be mapped to these 16 symbols. One added advantage of using
Hexadecimal representation is that a byte contains 8 bits a multiple of 4 and hence
can be represented by two hexadecimal symbols juxtaposed to each other.
The symbols used by Hexadecimal notation is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E,
F. The mapping between 4 consecutive bits and a Hexadecimal notation is given by
0000 -> 0, 0001-> 1, 0010-> 2, 0011->3, 0100->4, 0101->5, 0110->6, 0111->7,
1000->8, 1001->9, 1010->A, 1011->B, 1100->C, 1101->D, 1110->E, 1111->F.

So, the way to convert an unsigned integer given in binary to Hexadecimal is


simple.
Starting from the right-most bit (least-significant bit), replace each group
of 4 bits by the equivalent hex digit (pad the left-most bits with zero/one if
necessary).
Example 8: Consider the given numbers stored in 16 bits and convert them
to Hexadecimal.
1001 0001 1100 1000 = 91C8
0011 1111 1101 0101 = 3FD5
You need to be careful because you may have to use appropriate sign extension
depending upon it is signed or unsigned number if the bit sequence is not using
numbers of bits as multiple of 4.
Example 9: Consider the unsigned number given as a bit sequence. Note
that total number of bits is not multiple of 4. In order to represent a bit
sequence in Hex, just pad it with 0s such that the total number of bits is a
multiple of 4 and then convert it to Hex.
1001001010 = 0010 0100 1010 = 24A
10001011001011 = 0010 0010 1100 1011 = 22CB

Example 10: Consider the signed number given in 2s complement form.


Say 11 0111 (-9) using 6 bits. So it should be sign extended to 8 bits i.e
1111 0111 and then converted to hexadecimal form F7.
Converting from Hexadecimal is also very straight forward. Corresponding to each
symbol write the corresponding bit sequence.
Example 11: Consider the given Hexadecimal numbers to Binary
representations.
91C8 = 1001 0001 1100 1000
3FD5 = 0011 1111 1101 0101
Exercise 21: Convert the following to hexadecimal if these are considered
to be represented in sign magnitude form
001
1111
=
______(value
decimal)=______________________________________(Hexadecimal).

in

11010 = ______(value in decimal)=__________________________________________


(Hexadecimal).

Exercise 22: Convert the following to hexadecimal if these are considered


to be represented in 1s complement form
001
1111
=
______(value
decimal)=______________________________________(Hexadecimal).

in

11010 = ______(value in decimal)=__________________________________________


(Hexadecimal).

Exercise 23: Convert the following to hexadecimal if these are considered


to be represented in 2s complement form
001
1111
=
______(value
decimal)=______________________________________(Hexadecimal).
11010 = ______(value in decimal)=__________________________________________
(Hexadecimal).

in

Practice some of the arithmetic problems using the different representations you
have learnt till now.
Exercise 24: Add A and B (using 2's complement representation). Check if
there is an overflow
a) A
=
-60,
B
=
-70
____________________________________________________Overflow : Y/N

b) A = 60, B = -70 : ___________________________________________________ Overflow


: Y/N
Exercise 25: Perform the following additions:
a) 7D96
+
F0A0
_______________________________________________________________

b) A397
+
A35D
_______________________________________________________________

Section
5:
(Fractions)

Representing

Floating

point

Numbers

First we need to understand how to convert a decimal number such as 15.125 to


binary representation.
So the method to do it is
1.
2.

Separate the integral and the fractional parts.


For the integral part, divide by 2, and collect the remainder in reverse order (as
you have already learnt)

3.

For the fractional part, multiply the fractional part by 2 repeatedly, and collect
the integral part in the same order.

Example 12: Convert the decimal number 15.125 to binary.


Step 1: Separate 15.125 to 15 and 0.125 .
Step 2: The binary representation of 15 is 1111.
Step 3: The binary representation of 0.165 is
0.125 x 2 = 0.25

Integer part is 0.

0.25 x 2 = 0.5
0.5 x 2 = 1.00

Integer part is 0.
Integer part is 1.

Again start with fraction part i.e


0.00 x 2 = 0.00

Integer part is 0

So no further computation is required because rest al the integer values will


be 1.
So we can write 15.125 = 1111.0010 = 1111.001

Lets practice some more exercises

Exercise 26 Convert the following decimal fraction to binary.

15.625

_________________________________________________________________________________________
___

0.0625
=_______________________________________________________________________________________
_____

50.9375

_________________________________________________________________________________________
___

Obviously we would like to learn, given a fractional number in binary how to convert it
into decimal.

So the method to do it is
1. Separate the integral and the fractional parts.
2.
3.

For the integral part, use the standard way of conversion.


For the fractional part, multiply the fractional part by powers of decresing powers
of 2 starting with -1.

Example 13: Convert 110.101 to decimal.


Step1: Separate out the integral part 110 and fractional part 101.
Step 2: Convert 110 to decimal = 1x 2^2 + 1 x 2^1 + 0 x 2^0 = 6
Step 3. Convert .101 to decimal = 1 x 2^(-1) + 0 x 2^(-2) + 1 x 2^(-3) = 0.5 +
0 + 0.125 = 0.625
So the final number is 6.625.

Lets practice few problems.

Exercise 27 Convert the following binary fraction to decimal.

=
_________________________________________________________________________________________
101.101

___

0.0001

=_______________________________________________________________________________________
_____

101010.1111

_________________________________________________________________________________________
__

Now we need to understand the normalization. Normalization means that before


decimal you will have only one non zero digit. You can normalize by multiplying it with
appropriate power of its base. For example in the case decimal numbers 245.65 can be
written as 2.4565 x 10^2 and 0.002 can be written as 2.0 x 10^-3. Similarly 110.1101
can be normalized as 1.1101101 x 2^2 and 0.0101 can be normalized as 1.01 x 2^(-2).
In Normalized form we can write a binary number as
1.FRACTION x 2^(EXPONENT)

Lets practice some Normalization problem.

Exercise 28 Normalize the following Decimal and Binary numbers.


16789.0001 = __________________________________________________________________________

0.010456 = ____________________________________________________________________________

11101.0010 = _________________________________________________________________________

0.000010101

_________________________________________________________________________

Over the years, a standard evolved for representing a floating point number called IEEE
754 standard. It has two formats called single precision and double precision. Single
precision format uses 32 bits whereas Double precision uses 64 bits.
In 32-bit single-precision floating-point representation:

The most significant bit is the sign bit (S), with 0 for positive numbers and 1 for
negative numbers.
The following 8 bits represent exponent (E).
The remaining 23 bits represents fraction (F).

Let's illustrate with an example, suppose that the 32-bit pattern is


1 1000 0001 011 0000 0000 0000 0000 0000, with:
S=1
E = 1000 0001
F = 011 0000 0000 0000 0000 0000
In the normalized form, the actual fraction is normalized with an implicit leading 1 in
the form of 1.F. In this example, the actual fraction is 1.011 0000 0000 0000 0000
0000 = 1 + 12^-2 + 12^-3 = 1.375.
The sign bit represents the sign of the number, with S=0 for positive and S=1 for
negative number. In this example with S=1, this is a negative number, i.e., 1.375D.
In normalized form, the actual exponent is E-127 (called bias-127). This is because
we need to represent both positive and negative exponent. With an 8-bit E, ranging
from 0 to 255, the excess-127 scheme could provide actual exponent of -127 to 128. In
this example, E-127=129-127=2.
Hence, the number represented is -1.3752^2=-5.5.
Lets practice some problems now.
Exercise 29: Convert the following 32-bit patterns given IEEE 754 single
precision to their respective decimal form
1011

1101

0100

0000

0000

0000

0000

0000:

0101
0101
0110
0000
0000
________________________________________________

0000

0000

0000:

1100

0000

0000

0000:

________________________________________________

0001

1111

0000

0000

________________________________________________

How to convert a fractional value in decimal to IEEE 754 single precision


format?
First part is sign. If the number is +ve put 0 for the sign bit i.e the leftmost bit,
otherwise put 1.
Next get the fraction part. Already we have seen that how to convert a decimal number
to binary and then Normalize it. For example 6.25 in binary is 110.01 and after
normalization it becomes 1.1001 x 2^2. So the Fraction part is 1001. Now we need to
represent it using 23 bits. So pad 0s to the right i.e. Fraction field becomes 1001 0000
0000 0000 0000 000.
The last part is the exponent which is 2 in this case A bias of the value 127 is added to
it. Thus exponent field becomes 127+2 = 129. Using 2s complement representation,
we get 1000 0001 as the 8 bit representation of 129.
So the final IEEE 754 single precision representation of 6.25 is
0 1000 0001 1001 0000 0000 0000 0000 000.

Lets practice some problems.


Exercise 30: Convert the following decimal fractions to IEEE 754 single
precision format.

a)

55

23
64

_______________________________________________________________________________
b) 64000 _________________________________________________________________________
c) 128_____________________________________________________________________________
d) 0.0625 _________________________________________________________________________

Anda mungkin juga menyukai