You are on page 1of 42

Computer Programming

A.Baskar
BITS Pilani, K. K. Birla Goa Campus

25 January 2016

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

1 / 28

Summary

Unsigned

A.Baskar (BITS-Pilani)

13

MinBits

8 Bit representation

1101

0000 1101

Computer Programming

25 January 2016

2 / 28

Summary
MinBits

8 Bit representation

Unsigned

13

1101

0000 1101

Sign-Magnitude Form
Sign-Magnitude Form

+13
-13

0 1101
1 1101

0000 1101
1000 1101

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

2 / 28

Summary
MinBits

8 Bit representation

Unsigned

13

1101

0000 1101

Sign-Magnitude Form
Sign-Magnitude Form

+13
-13

0 1101
1 1101

0000 1101
1000 1101

1s Complement Form
1s Complement Form

+13 0 1101
-13 1 0010

0000 1101
1111 0010

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

2 / 28

Summary
MinBits

8 Bit representation

Unsigned

13

1101

0000 1101

Sign-Magnitude Form
Sign-Magnitude Form

+13
-13

0 1101
1 1101

0000 1101
1000 1101

1s Complement Form
1s Complement Form

+13 0 1101
-13 1 0010

0000 1101
1111 0010

2s Complement Form
2s Complement Form

+13 0 1101
-13 1 0011

0000 1101
1111 0011

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

2 / 28

Summary
Representation
of 0

Total Number
of Integers

Range

Unsigned

00 . . . 0

2n

O to 2n 1

Sign-Magnitude
Form

10 . . . 0
00 . . . 0

2n 1

(2n1 1) to
+(2n1 1)

1s Complement
Form

00 . . . 0
11 . . . 1

2n 1

(2n1 1) to
+(2n1 1)

2s Complement
Form

00 . . . 0

2n

2n1 to
+(2n1 1)

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

3 / 28

Converting to decimal number: Sign Magnitude


Form

If the MSB is 0, find the decimal value in the unsigned form of


the rest of the bits and append it with plus sign
If the MSB is 1, find the decimal value in the unsigned form of
the rest of the bits and append it with minus sign

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

4 / 28

Questions

Convert the following binary numbers in sign magnitude form to


decimal
1010
0101 1010
1 1111 1110
001 1001 0010 0011

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

5 / 28

Converting to decimal number: 1s complement


form

If the MSB is 0, find the decimal value of it in the unsigned form


and append it with plus sign
If the MSB is 1, flip every bit, find the decimal value of it in the
unsigned form and append it with minus sign

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

6 / 28

Questions

Convert the following 1s complement binary numbers to decimal


1010
0101 1010
1 1111 1110
001 1001 0010 0011

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

7 / 28

Converting to decimal number: 2s complement


form

If the MSB is 0, find the decimal value of it in the unsigned form


and append it with plus sign
If the MSB is 1, flip every bit, add 1, find the decimal value of it
in the unsigned form and append it with minus sign

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

8 / 28

Questions

Convert the following 2s complement binary numbers to decimal


1010
0101 1010
1 1111 1110
001 1001 0010 0011

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

9 / 28

2s Complement Form: Sign Extension

Suppose we have a binary number in 2s complement form and


we want to extend the number of bits without changing the value
If the MSB is 0, add 0s before the MSB
If the MSB is 1, add 1s before the MSB

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

10 / 28

Questions

Without changing their values convert the following 2s complement


binary numbers into 8-bit 2s complement numbers.
1010
01 1001
01
11 1111 1000
10 0001 1000

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

11 / 28

2s Complement Form: Addition

If the number of bits are same, then do component wise addition


If they dont have same number of bits, do sign extension and
then add
Leave the carry out from the MSB

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

12 / 28

Questions

Add the following 2s complement binary numbers. Also express the


answer in decimal
1001+1011
01+1011
11+01010101
0101+110
01+10

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

13 / 28

Overflow in 2s complement form

What if the carry out from MSBs is 1? Can we ignore it or not?


Will there be any problem, if the carry out from MSBs is 0?

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

14 / 28

Overflow in 2s complement form

What if the carry out from MSBs is 1? Can we ignore it or not?


Will there be any problem, if the carry out from MSBs is 0?
Consider Addison of following pairs of numbers in 2s
complement form using 8 bit representation
(-30, -90); (40, 90) ; (-40, -90)

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

14 / 28

Overflow in 2s complement form

What if the carry out from MSBs is 1? Can we ignore it or not?


Will there be any problem, if the carry out from MSBs is 0?
Consider Addison of following pairs of numbers in 2s
complement form using 8 bit representation
(-30, -90); (40, 90) ; (-40, -90)
Arithmetic and Logic Unit (ALU) should be able to detect that
there are some problem in the second and third pairs
If the carry in and carry-out of MSBs are different, then overflow
has occurred (the value is too large to represent using 8 bits)

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

14 / 28

Questions

The following binary numbers are 4-bit 2s complement binary


numbers. Which of the following operations generate overflow
1100 + 0011
1100 + 0100
0111 + 0001
0111 + 1001

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

15 / 28

Converting Decimal Fractions to Binary

Decimal Fractions: Integral part . Fractional part


Convert integral part and fractional part separately to binary
number
To convert fractional part, first multiply it by 2n ( n depends on
how much precision we want)

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

16 / 28

Converting Decimal Fractions to Binary

Decimal Fractions: Integral part . Fractional part


Convert integral part and fractional part separately to binary
number
To convert fractional part, first multiply it by 2n ( n depends on
how much precision we want)
and then convert integral part to binary and divide by 2n

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

16 / 28

Questions
Convert following binary numbers to decimal numbers.
.1001
11.1001
101.1111
Convert the following decimal numbers to binary numbers
0.5
0.250
0.875
0.1
15.1
0.365
A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

17 / 28

Floating Point Number Representation

32 bits to represent floating point numbers: Very large numbers


and very tiny numbers
Convert the floating point to scientific notation
1 bit for sign, 8 bits for range (exponent), 23 bits for precision
(fraction)

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

18 / 28

Floating Point Number Representation

32 bits to represent floating point numbers: Very large numbers


and very tiny numbers
Convert the floating point to scientific notation
1 bit for sign, 8 bits for range (exponent), 23 bits for precision
(fraction)
The actual exponent is the unsigned number of 8 binary digits
minus 127

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

18 / 28

Questions
Write the floating point representation for the following binary
numbers
-.1001
11.1001
-101.1111
Write the floating point representation for the following decimal
numbers
6 58
3.1415927
64000
-15.365

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

19 / 28

Questions

Write the decimal equivalents for these floating point numbers.


0 0111 1011 0000 0000 0000 0000 0000 000
0 1000 0000 0000 0000 0000 0000 0000 000
1 1000 0011 0001 0000 0000 0000 0000 000

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

20 / 28

Hexadecimal and Octal numbers

Hexadecimal is used as a convenience for humans


Each digit is in hex (0,1,2,...,9, A,B,C,D,E,F) instead of binary
(0,1)
It reduces the number of digits by 4

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

21 / 28

Hexadecimal and Octal numbers

Hexadecimal is used as a convenience for humans


Each digit is in hex (0,1,2,...,9, A,B,C,D,E,F) instead of binary
(0,1)
It reduces the number of digits by 4
Group them as 4 bits and write the equivalent hex digits for each
of the 4 bits

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

21 / 28

Conversions among various representations

Binary Octal Decimal

Hexadecimal

Binary
Octal
Decimal
Hexadecimal

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

22 / 28

Questions

Convert the following hexadecimal numbers to binary:


x10, x801, xF731, xBCAD

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

23 / 28

Questions

Convert the following hexadecimal numbers to binary:


x10, x801, xF731, xBCAD
Convert the following unsigned binary numbers to hexadecimal:
1101 0001 1010 1111, 001 1111, 1, 1110 1101 1011 0010

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

23 / 28

Questions

Convert the following hexadecimal numbers to binary:


x10, x801, xF731, xBCAD
Convert the following unsigned binary numbers to hexadecimal:
1101 0001 1010 1111, 001 1111, 1, 1110 1101 1011 0010
Convert the following to hexadecimal and octal number system
if these are considered to be represented in (a) sign magnitude
form, (b) 1s complement form, and (c) 2s complement form.
001 1111
11010

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

23 / 28

Questions

Convert the following hexadecimal representations of 2s


complement binary numbers to decimal numbers:
xF0, x7FF, x8000, x16

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

24 / 28

Questions

Convert the following hexadecimal representations of 2s


complement binary numbers to decimal numbers:
xF0, x7FF, x8000, x16
Convert the following decimal numbers to hexadecimal
representations of 2s complement numbers:
256, 111, -44

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

24 / 28

Characters

8 bit code to represent characters: ASCII


A is represented using 65, a represented using 97
0 is represented using 48

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

25 / 28

Logical Operations in Binary Numbers

And operation, Or operation, Not operation, Exclusive or


operation
Bitwise operations

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

26 / 28

Logical Operations in Binary Numbers

And operation, Or operation, Not operation, Exclusive or


operation
Bitwise operations
Bitmask

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

26 / 28

Questions
101 OR 110
0101 0111 OR 1101 0111
0101 OR (1100 OR 1101)

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

27 / 28

Questions
101 OR 110
0101 0111 OR 1101 0111
0101 OR (1100 OR 1101)
101 AND 110
0101 0111 AND 1101 0111
0101 AND (1100 AND 1101)

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

27 / 28

Questions
101 OR 110
0101 0111 OR 1101 0111
0101 OR (1100 OR 1101)
101 AND 110
0101 0111 AND 1101 0111
0101 AND (1100 AND 1101)
NOT(1011)
NOT(1011) OR NOT(1100)
NOT(NOT(1101))

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

27 / 28

Questions

It is required to extract out least significant two digits of a 8-bit


binary number [Hint: Use masks, i.e. X AND 0000 0011 = Y].
How to find whether two bit patterns are identical [Hint: Take
XOR of the two numbers and check the result for all zeroes]

A.Baskar (BITS-Pilani)

Computer Programming

25 January 2016

28 / 28

You might also like