0% found this document useful (0 votes)
35 views63 pages

PPS - Ch-1.Number System

The document discusses different number systems including binary, octal, decimal, and hexadecimal. It covers techniques for converting between these number systems as well as arithmetic operations like addition, subtraction, multiplication, and division in binary. The document also discusses signed and unsigned number representation.

Uploaded by

2203051050594
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
35 views63 pages

PPS - Ch-1.Number System

The document discusses different number systems including binary, octal, decimal, and hexadecimal. It covers techniques for converting between these number systems as well as arithmetic operations like addition, subtraction, multiplication, and division in binary. The document also discusses signed and unsigned number representation.

Uploaded by

2203051050594
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 63

Fundamental of Programming

Prof. Digvijaysinh Mahida, Assistant Professor


Computer Science & Engineering
CHAPTER-1
Number system
Common number system
Used by Used in
System Base Symbols humans? computers?
Decimal 10 0, 1, … 9 Yes No
Binary 2 0, 1 No Yes
Octal 8 0, 1, … 7 No No
Hexa­ 16 0, 1, … 9, No No
decimal A, B, … F
Counting
Hexa-
Decimal Binary Octal decimal

0 0 0 0
1 1 1 1
2 10 2 2
3 11 3 3
4 100 4 4
5 101 5 5
6 110 6 6
7 111 7 7
Conversion Among Bases
• The possible conversion

Decimal Octal

Binary Hexadecimal
Binary to Decimal

Decimal Octal

Binary Hexadecimal
Technique for conversion
• Multiply each bit by 2n, where n is the “weight” of the bit
• The weight is the position of the bit, starting from 0 on
the right
• Add the results
Example
.

1010112 => 1 x 20 = 1
1 x 21 = 2
0 x 22 = 0
1 x 23 = 8
0 x 24 = 0
1 x 25 = 32
4310
Octal to Decimal

Decimal Octal

Binary Hexadecimal
Technique for conversion

• Multiply each bit by 8n, where n is the “weight” of


the bit
• The weight is the position of the bit, starting from
0 on the right
• Add the results
Example

7248 => 4 x 80 = 4
2 x 81 = 16
7 x 82 = 448
46810
Hexadecimal to Decimal

Decimal Octal

Binary Hexadecimal
Technique for conversion
• Multiply each bit by 16n, where n is the “weight” of the
bit
• The weight is the position of the bit, starting from 0 on
the right
• Add the results
Example

ABC16 => C x 160 = 12 x 1 = 12


B x 161 = 11 x 16 = 176
A x 162 = 10 x 256 = 2560
274810
Decimal to Binary

Decimal Octal

Binary Hexadecimal
Technique for conversion
• Divide by two, keep track of the remainder
• First remainder is bit 0 (LSB, least­significant bit)

• Second remainder is bit 1

• Etc.
Example

12510 = ?

12510 = 11111012
Octal to Binary

Decimal Octal

Binary Hexadecimal
Technique for conversion
• Convert each octal digit to a 3­bit equivalent binary
representation

macOS Source: Google


Example

7058 = ?2

7 0 5

111 000 101

7058 = 1110001012
Hexadecimal to Binary

Decimal Octal

Binary Hexadecimal
Technique for conversion
• Convert each hexadecimal digit to a 4­bit equivalent
binary representation
Example
10AF16 = ?2

1 0 A F

0001 0000 1010 1111

10AF16 = 00010000101011112
Decimal to Octal

Decimal Octal

Binary Hexadecimal
Technique of conversion
• Divide by 8
• Keep track of the remainder
Example
123410 = ?8

8 1234
8 154 2
8 19 2
8 2 3
0 2

123410 = 23228
Decimal to Hexadecimal

Decimal Octal

Binary Hexadecimal
Technique of conversion

• Divide by 16
• Keep track of the remainder
Example
123410 = ?16

16 1234
16 77 2

16 4 13 = D
0 4

123410 = 4D216
Binary to Octal

Decimal Octal

Binary Hexadecimal
Technique for conversion

• Group bits in threes, starting on right


• Convert to octal digits
Example
10110101112 = ?8

1 011 010 111

1 3 2 7

10110101112 = 13278
Binary to Hexadecimal

Decimal Octal

Binary Hexadecimal
Technique for conversion
• Group bits in fours, starting on right
• Convert to hexadecimal digits
Example

10101110112 = ?16

10 1011 1011

2 B B

10101110112 = 2BB16
Octal to Hexadecimal

Decimal Octal

Binary Hexadecimal
Technique for conversion
• Use binary as an intermediary
10768 = ?16

1 0 7 6

001 000 111 110

2 3 E

10768 = 23E16
Hexadecimal to Octal

Decimal Octal

Binary Hexadecimal
Technique for conversion

• Use binary as an intermediary


Example
1F0C16 = ?8
1 F 0 C

0001 1111 0000 1100

1 7 4 1 4

1F0C16 = 174148
Binary Addition
• Two 1­bit values
A B A+B
0 0 0
0 1 1
1 0 1
1 1 10
Binary Addition
• Two n­bit values
– Add individual bits

– Propagate carries

– E.g.,
10101 21
+ 11001 + 25
101110 46
Multiplication

• Binary, two 1­bit values

A B A×B
0 0 0
0 1 0
1 0 0
1 1 1
Example of multiplication
• Binary, two n­bit values
– As with decimal values
1110
– E.g., x 1011
1110
1110
0000
1110
10011010
Binary Subtraction
Have previously looked at the subtraction operation. A quick review.
Just like subtraction in any other base
Minuend 10110
Subtrahand ­ 10010
Difference 00100
And when a borrow is needed. Note that the borrow gives us 2 in the current bit position.
Binary Division
Binary division is also performed in the same way as we perform decimal division.
Like decimal division, we also need to follow the binary subtraction rules while
performing the binary division. The dividend involved in binary division should be
greater than the divisor. The following are the two important points, which need to
be remembered while performing the binary division.

• If the remainder obtained by the division process is greater than or equal to the

divisor, put 1 in the quotient and perform the binary subtraction.

• If the remainder obtained by the division process is less than the divisor, put 0 in the

quotient and append the next most significant digit from the dividend to the
remainder
Binary Division
Perform the binary division of the decimal numbers 18 and 8.
The equivalent binary representation of the decimal number 18 is 10010.
The equivalent binary representation of the decimal number 8 is 1000.

1 0 0 0 ) 1 0 0 1 0 ( 1 0 Quotient
1 0 0 0
0 0 0 1 0
0 0 0 0 0
0 0 0 1 0 Remainder
Signed /Unsigned Number

Sign bit

0 0 1 1 0 0 1 0 50

Sign bit

1 0 1 1 0 0 1 0 ­50
Signed/Unsigned Numbers
It is very easy to
represent and
Advantages of understand
the signed­ positive as well
magnitude as negative
representation: numbers using
this
representation.
Represent equal
The binary number of
multiplication and positive and
the binary division negative
of the signed quantities that
binary numbers makes it a very
can be easily symmetrical
performed. method of
representation.
Signed/Unsigned Numbers

1 2 3
Disadvantages of the signed­ It is not an easy task to It provides two different
magnitude representation: perform the binary addition representations of zero, one
and the binary subtraction for plus zero and another for
using this representation. negative zero but actually they
are the same values. This
could lead to some confusion
while performing various
arithmetic operations.
Complements of Binary Numbers
The complement system can also be used to represent the signed binary numbers
apart from the signed­magnitude representation method.
In the complement system, the positive integers are represented in a similar manner as
they are represented in the signed­magnitude representation. The following are the two
most popular complement methods used in the computer system:

One’s complement
Two’s complement
One’s Complement
One’s complement method can be used to represent
negative binary numbers.

A negative number can be represented using one’s


complement method by first computing the binary equivalent
of the number and then changing all the zeros with ones and
all the ones with zeros.
For example, the binary equivalent of the decimal number 15
is 00001111. Therefore, ­15 can be represented using one’s
complement method as 11110000.
One’s Complement

The one’s complement method also uses the left


most bit as the sign bit to indicate the sign of the
number.

Sign bit

1 1 1 1 0 0 0 0 ­15
One’s Complement
Integers One’s complement representation
-7 1000
-6 1001 The one’s complement method
-5 1010
-4 1011
of representing signed numbers
-3 1100 also has two different
-2 1101
-1 1110
representations for the number,
-0 1111 zero.
+0 0000
+1 0001
+2 0010
+3 0011
+4 0100
+5 0101
+6 0110
+7 0111
One’s Complement

Now, change all the


zeros to ones and all the Therefore, the one’s
The equivalent binary
ones to zeros in order to complement
representation of 25 is
obtain the ones representation of ­25 is
in byte size is 00011001.
complement 11100110.
representation: 11100110
Two’s Complement
Two’s complement is the most widely used method for representing
negative numbers in the computer system.

The two’s complement of the given integer can be obtained by


adding 1 to the one’s complement of that number.

For example, the two’s complement representation of ­15 can be


obtained by adding 1 to 11110000, which is the one’s complement
representation of ­15. Therefore, the two’s complement
representation of ­15 is 11110001.
Two’s Complement

The two’s complement method also uses the left most bit as the sign bit to indicate
the sign of the number.

Sign bit

1 1 1 1 0 0 0 1 ­15
Two’s Complement
Integers Two’s complement representation
-7 1001
-6 1010
-5 1011
-4 1100
-3 1101
-2 1110
-1 1111
-0 0000
+0 0000
+1 0001
+2 0010
+3 0011
+4 0100
+5 0101
+6 0110
+7 0111
Two’s Complement
Now, change all the
zeros to ones and all
The equivalent binary the ones to zeros in
representation of 33 in order to obtain the
a byte is 00100001. one’s complement
representation:
11011110.

Therefore, the two’s


complement
Add 1 to the 11011110.
representation of ­33 is
11011111.
Fraction

• Binary to decimal

10.1011 => 1 x 2-4 = 0.0625


1 x 2-3 = 0.125
0 x 2-2 = 0.0
1 x 2-1 = 0.5
0 x 20 = 0.0
1 x 21 = 2.0
2.6875
Fraction
• Decimal to binary
.14579
x 2
3.14579 0.29158
x 2
0.58316
x 2
1.16632
x 2
0.33264
x 2
0.66528
x 2
1.33056
11.001001...
etc.
www.paruluniversity.ac.in

You might also like