You are on page 1of 47

Introduction To Computer

Programming
Saqib Rizwan

Computer Science Department


Number Systems
 Decimal Number System
 Binary Number System
 Octal Number System
 Hexadecimal Number System
Decimal Number System
Base is 10
Use 10 symbols 0,1,2,3,4,5,6,7,8,9
Each place is weighted by the power of 10
E.g. 4,309
Binary Number System

Base is 2 or ‘b’ or ‘B’ or ‘Bin’


Two symbols: 0 and 1
Each place is weighted by the power of 2
Example:
10112 or 1011 B
Converting Decimal to Binary
 For example convert 13 to Base 2

2 13

2 6 ---- 1
2 3 ---- 0

1 ---- 1

(1101)2
Converting decimal to binary
Example: 162:

162 / 2 = 81 rem 0
81 / 2 = 40 rem 1
40 / 2 = 20 rem 0
20 / 2 = 10 rem 0
10 / 2 = 5 rem 0
5/2 = 2 rem 1
2/2 = 1 rem 0
1/2 = 0 rem 1
So, 16210 = 101000102
Some Exercises
13D = (?) B

1101B

23D = (?) B
10111B

72D = (?)
1001000B
B
Some Exercises
59D = (?) B

111011B

145 D = (?) B
10010001 B
Conversion from binary number
system to decimal system
Hexadecimal Number System
Base is 16 or ‘Hex’ or ‘H’
16 symbols: {0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F}
Each place is weighted by the power of 16

Example:
(3AB)H
Conversion from Decimal to
Hexadecimal
Same as that of Decimal to Binary
Repeatedly divide by 16

16 93

5 ---- D
Conversion from Hexadecimal to
Decimal
(3AB4)H to Base 10

(3AB4) = 3x163+10x162+11x161+4x 160


= 15028
Hexadecimal to Binary (Direct
Method)
Hexadecimal to Binary
Expand each hexadecimal digit to 4 binary
bits.
Example: (E29)16 = (1110 | 0010 | 1001)2

Binary to Hexadecimal
Combine every 4 bits into one hexadecimal
digit
Octal Number System
Base is 8
8 symbols: {0,1,2,3,4,5,6,7}
Each place is weighted by the power of 8

Example:
(345)8
Conversion from Decimal to Octal
Repeatedly divide by 8

Conversion from Octal to


Hexadecimal
The conversion is made in two steps using binary as an
intermediate base. Octal is converted to binary and then
binary to hexadecimal, grouping digits by fours, which
correspond each to a hexadecimal digit.

For instance, convert octal 1057 to hexadecimal


To binary:
1 0 5 7
001 000 101 111

then to hexadecimal:
0010 0010 1111
2 2 F
Therefore, 10578 = 22F16.
Octal to Binary
Expand each octal digit to 3 binary bits.
Example: (725)8 = (111 | 010 | 101)2

Binary to Octal
Combine every 3 bits into one octal digit
Example: (110 | 010 | 011)2 = (623)8
Some important Conversions to
Remember
Base 10 Base 2 Base 16
0 0 0
1 1 1
2 10 2
3 11 3
4 100 4
5 101 5
6 110 6
7 111 7
8 1000 8
9 1001 9
10 1010 A
11 1011 B
12 1100 C
13 1101 D
14 1110 E
15 1111 F
Unsigned Binary (UB)
Rep.
With word size of k bits, we can represent
exactly 2k integers, ranging from 0 to 2k - 1
Example (for k = 2):
 UB number Dec. number
 00 0
 01 1
 10 2
 11 3

19
Signed number
representation
In ordinary arithmetic, a negative number is
indicated by a minus sign and a positive
number by a plus sign
In computers, the convention is to represent
the sign with an extra bit placed in the
leftmost position (the sign bit) and make the
sign bit 0 for positive and 1 for negative
We’ll look at three different representations
for negative numbers

20
Signed-Magnitude (SM) Rep
Idea: The leftmost bit represents the sign (0
= plus, 1 = minus) and the rest of the bits
represent the number
Example:
 Sign bit = 0 = +
 5 magnitude bits
 (+22)10 = (0 1 0 1 1 0)SM
 (-22)10 = (1 1 0 1 1 0)SM

21
SM Numbers (for k = 3)
 SM number Dec. number
 0000 Number Range:
 0 0 11 - (2k-1 - 1) to
0 1 02
+ (2k-1 - 1)

 0 1 13
 1 0 0 -0
 1 0 1 -1
 1 1 0 -2
 1 1 1 -3

22
1’s complement (1C)
representation
Like SM, 1C representation also has a sign
bit with 0 for plus and 1 for minus
Positive-number representations same as
with SM but negative-number
representations different from SM
To change the sign of a number, all the bits
in the number (not just the sign bit) are
“flipped”

23
1C Example
 6-bit 1C representation of decimal 22:
 Sign bit = 0 = +
 5 magnitude bits = 22
 (+22)10 = (0 1 0 1 1 0)1C
6-bit 1C representation of decimal -22:
 (-22)10 = - (0 1 0 1 1 0)1C
 = (1 0 1 0 0 1)1C

 Sign bit = 1 = minus
 1’s complement of (0 1 0 1 1 0)

24
1C Numbers (for k = 3)
 1C number Dec. number
 0000 Number Range:
 0 0 11 - (2k-1 - 1) to
0 1 02
+ (2k-1 - 1)

 0 1 13
 1 0 0 -3
 1 0 1 -2
 1 1 0 -1
 1 1 1 -0

25
2’s complement (2C)
representation
Like SM, 2’s complement also has a sign bit
with 0 used for plus and 1 for minus
Negative-number representations different
from SM and 1C
To change the sign of a number, all the bits
in the number are flipped (1’s
complement), then a 1 is added to the
result

26
2C Example
6-bit 1C representation of decimal 22:
 Sign bit = 0 = +
 5 magnitude bits
 (+22)10 = (0 1 0 1 1 0)2C
 (-22)10 = 101001 2’s complement of
 + 1 of (0 1 0 1 1 0)
 = (1 0 1 0 1 0)2C

 sign bit = 1 = -ve

27
2C Numbers (for k = 3)
 2C number Dec. number
 0000 Number Range:
 0 0 11 - 2k-1 to
0 1 02
+ (2k-1 - 1)

 0 1 13
 1 0 0 -4
 1 0 1 -3
 1 1 0 -2
 1 1 1 -1

28
Single bit Binary Addition
Addition table for binary digits:
 0 0 1 1
 +0 +1 +0 +1
 00 01 01 10
 sum bit
 carry bit

29
Multi-bit binary addition
When adding binary numbers if a carry is
generated, it is carried one position to the
left, just as in decimal arithmetic
Example 1 1 1
 X 173 1 0 1 0 1 1 0 1
 +Y 44 0 0 1 0 1 1 0 0
 217 1 1 0 1 1 0 0 1

30
Addition in 1C and 2C
Suppose we want to add decimal 3 and decimal -2
using 3-bit 1C and 2C
 Decimal 1’s complement 2’s complement
 3 0 1 1 0 1 1
 + (-2) +1 0 1 + 1 1 0
 + 1 carry 1 0 0 0 10 0 1
 1

31
Number Rep. Revisited (k
= 3)
Think of the differences

 Bit pattern UB SM 1C 2C
 000 0 0 0 0
 0 0 1 1 1 1 1
 0 1 0 2 2 2 2
 0 1 1 3 3 3 3
 1 0 0 4 -0 -3 -4
 1 0 1 5 -1 -2 -3
 1 1 0 6 -2 -1 -2
 1 1 1 7 -3 -0 -1

32
Example for Two’s Complement
Notation
Two’s complement notation
Consider a machine with 32-bit integers.
Suppose int value = 13;
The 32-bit representation of value is
00000000 00000000 00000000 00001101
To form the negative of value we first form its
one’s complement by applying C bitwise
complement operator (~)
onesComplementOfValue = ~value;

33
Two’s Complement Notation

Internally, ~value is now value with each of


its bits reversed----ones become zeros and
zeros become ones as follows:
 value:
00000000 00000000 00000000 00001101
~value:
11111111 11111111 11111111 11110010

34
Two’s Complement Notation

For two’s complement, we simply add one to the


value’s one’s complement. Thus
11111111 11111111 11111111 11110011
Now if this is in fact equal to –13, we should be able
to add it to binary 13 and obtain a result of 0. Lets
try:
00000000 00000000 00000000 00001101
+ 11111111 11111111 11111111 11110011
00000000 00000000 00000000 00000000

35
Computer’s Addition
x = a – value;
x = a + (~value + 1);
Suppose a = 27 and value is 13:
00000000 00000000 00000000 00011011
11111111 11111111 11111111 11110011
00000000 00000000 00000000 00001110
Which is indeed equal to 14.

36
Floating-Point Notation
In contrast to the storage of integers,
the storage of a value with a fractional
part requires that we store not only the
pattern of 0s and 1s representing its
binary representation but also the
position of the radix point ‘.’.
How are fractions represented in binary?
A method based on scientific notation is
called floating-point notation.

37
Representation for Floating-Point
Floating-point representation
mb e

exponent
(integer)
mantissa
(fraction) base
(10 or 8 or
e.g. 156.78 is 0.15678x103 2) e.g. 0.0294 is 0.294x10-1

38
Binary Data Representation

39
Decimal to Binary Mantissa
To convert decimal fractions to binary fraction, continually
multiply fraction by base (2). The integer parts of the results
give the digits from right to left.
0.6875
 0.10112
2
1.375
2
 0.750
2
1.500
2
1.000
40
Example
12.3910
Separate the integer and fraction part
Convert the integer part 0.39
1210 = 11002 * 2
Convert the fractional part 0.78
* 2
0.3910 = 01102 1.56
* 2
12.3910 = 1100.01102 1.12
* 2
41
0.24
Number Representation (16-bit
example)
Sign of Note: limited number of
number digits to represent
mantissa
00 1 1 11 1 1 0 0 0 0 0 0 0 0 0 0
exponent mantissa
(with sign)

0.0 1.0 real numbers


Mantissa is always between 0 and 1
42
IEEE 754 single precision format
(32-bit format) for Floating
Point numbers
• 1-bit for the sign
• 8-bits for the exponent (including its
sign)
• 23-bits for the mantissa (between 0
& 1)
Exponent(23-30) Fraction (0:22)

Sign (31)

43
IEEE 754 single precision format (32-bit format)
for Floating Point numbers

The Exponent is represented as Excess Notation


For 8-bits exponent, Excess is 2 8-1 -1=127
How to get 8-bit Exponent in excess notation?
1.Determine the decimal value of exponent
2.Add 127
3.Convert to its binary equivalent

44
Example: 8.125
Exponent(23-30) Fraction (0:22)

Sign (31)

Write 8.125 as a binary number: 1000.001 = 1.000001•23

Sign Bit: 0 Mantissa: .000001

Exponent: 3+127 = 130 =10000010


1 bit 8 bit 23 bit

Result: 01000001000000100000000000000000

45
Example: −16.25
Exponent(23-30) Fraction (0:22)

Sign (31)

Write 16.25 as a binary number: 10000.01 = 1.000001•24

Sign Bit: 1 mantissa: .000001

Exponent: 4+127 = 131 =10000011


1 bit 8 bit 23 bit

Result: 11000001100000100000000000000000

46
Floating Point Representations
• If stored exponent is not zero, mantissa is
interpreted as having a leading 1
1 10000111 01000000000000000000000
• This is a negative number
• Because sign is 1
– Exponent ??
• 135-127=8
– Mantissa is = .012 = 0.2510
– Becomes = 1.012 = 1.2510
– Value is: -1.25 * 28 = -32010
47

You might also like