art of programming - ce101aop.files.wordpress.com of programming number systems prof ... •step 1 -...
TRANSCRIPT
Art of Programming
Number Systems
Prof. Vishal Parikh
CSE Department
Institute of Technology
Nirma University
Number System
• A number system defines how a number can be
represented using distinct symbols.
• A number can be represented differently in
different systems.
• For example, the two numbers (2A)16 and (52)8
both refer to the same quantity, (42)10, but their
representations are different
• A computer can understand positional number
system where there are only a few symbols called
digits and these symbols represent different values
depending on the position they occupy in the
number.
• A value of each digit in a number can be
determined using:
₋ The digit
₋ The position of the digit in the number
₋ The base of the number system
Radix or Base
• The number of digits require to represent all the
numbers in a particular number system is called
radix/base.
• For example, the radix of binary system is 2
because in binary number only 2 digits are used
i.e. 0 and 1
• To distinguish two numbers in different number
system we write it as (10)₂ or (10)₁₀
• Positional Notation
N = (an-1an-2 ... a1a0 . a-1a-2 ... a-m)r where
. = radix point
r = radix or base
n = number of integer digits to the left of the
radix point
m = number of fractional digits to the right of
the radix point
an-1 = most significant digit (MSD)
a-m = least significant digit (LSD)
• Interpreted as:
N = an-1rⁿ-1 + an-2r
n-2... a1r 1 + a0r
0 + a-1r -1 + a-2 r
-2…
a-m r -m
• The symbols used in above representation should
be one of the r symbols allowed in the system
i.e. 0 to r-1
Number Base Conversion
1. Other base system to Decimal System:-
• Step 1 - Determine the position value of each
digit
• Step 2 - Multiply the obtained column values (in
Step 1) by the digits in the corresponding
columns.
• Step 3 - Sum the products calculated in Step 2.
The total is the equivalent value in decimal.
For example:-
1) (11010.11)2 = 1 x 24 + 1 x 23 + 0 x 22 + 1 x 21 +
0 x 20 + 1 x 2-1 + 1 x 2-2
= (26.75)10
2) (11010.11)2 = 1 x 24 + 1 x 23 + 0 x 22 + 1 x 21 +
0 x 20 + 1 x 2-1 + 1 x 2-2
= (26.75)10
3) (B65F)16 = 11 x 163 + 6 x 162 + 5 x 161 +
15x160 = (46,687)10
2. Decimal to Other Base System:-
For integers:
• Step 1 - Divide the decimal number to be
converted by the value of the new base.
• Step 2 - Get the remainder from Step 1 as the
rightmost digit (least significant digit) of new
base number.
• Step 3 - Divide the quotient of the previous
divide by the new base.
• Step 4 - Record the remainder from Step 3 as the
next digit (to the left) of the new base number.
• Decimal Number: 2910
• Calculating Binary Equivalent:
Step Operation Result Remainder
Step 1 29/2 14 1
Step 2 14/2 7 0
Step 3 7/2 3 1
Step 4 3/2 1 1
Step 5 1/2 0 1
ANSWER is (11101)2
For fractions:
• Step 1- Identify fractional part and consider 0 in
integer part
• Step 2 - Multiply the decimal number to be
converted by the value of the new base.
• Step 3 - Record the integer part from Step 2 as
the next digit of the new base number.
• Step 4 -Repeat steps(1,2and 3)
• Decimal number (0.1245)10 to binary
• Calculating Binary Equivalent:
Fraction Integer Coefficient
0.1245*2=0.249
0.249 0 a-1
0.249*2=0.498
0.498 0 a-2
0.498*2=0.996
0.996 0 a-3
0.996*2=1.992
0.992 1 a-4
0.992*2=1.984
0.984 1 a-5
0.984*2=1.968
0.968 1 a-6
ANSWER is (0.000111)2
3. Shortcut method - Binary to Octal
• Step 1 - Divide the binary digits into groups of three (starting from the right).
• Step 2 - Convert each group of three binary digits to one octal digit.
• Binary number (10101)2
• Calculating Octal Equivalent:
Step Binary Number Octal Number
Step 1 101012 010 101
Step 2 101012 28 58
Step 3 101012 258
ANSWER is (25)8
4. Shortcut method - Octal to Binary
• Step 1 - Convert each octal digit to a 3-digit
binary number (the octal digits may be treated as
decimal for this conversion).
• Step 2 - Combine all the resulting binary groups
(of 3 digits each) into a single binary number.
• Octal Number: 258
• Calculating Binary Equivalent:
Step Octal Number Binary Number
Step 1 258 210 510
Step 2 258 0102 1012
Step 3 258 0101012
ANSWER is (10101)2
5. Shortcut method - Binary to Hexadecimal
• Step 1 - Divide the binary digits into groups of
four (starting from the right).
• Step 2 - Convert each group of four binary
digits to one hexadecimal symbol.
• Binary Number: 101012
• Calculating hexadecimal Equivalent:
Step Binary Number Hexadecimal Number
Step 1 101012 0001 0101
Step 2 101012 110 510
Step 3 101012 1516
ANSWER is (15)16 or (15)H
6. Shortcut method - Hexadecimal to Binary
• Step 1 - Convert each hexadecimal digit to a 4-
digit binary number (the hexadecimal digits may
be treated as decimal for this conversion).
• Step 2 - Combine all the resulting binary groups
(of 4 digits each) into a single binary number.
• Hexadecimal Number: 1516
• Calculating Binary Equivalent:
Step Hexadecimal Number
Binary Number
Step 1 1516 110 510
Step 2 1516 00012 01012
Step 3 1516 000101012
ANSWER is (10101)2
Exercises:
1. Convert the following to decimal: ANSWERS
(i) (10101)2 (iii) (28)16
(ii) (4307)8 (iv) (BC12)16
2. Convert into binary: ANSWERS
(i) (1998)10 (iii) (4E)16
(ii)(30)8 (iv) (4A01)16
Exercises:
3. Convert the following to Octal: ANSWER
(i) (23)10 (iii) (1A03)16
(ii) (11011)2
4. Convert into Hexadecimal: ANSWER
(i) (0100101000000001)2
(ii) (29)10 (iii) (37)8
Answers to que.1:
(i) (21)10
(ii) (2247)10 Back to Questions
(iii) (40) 10
(iv) (48146)10
Answers to que.2:
(i) (11111001110)2
(ii) (11000)2 Back to Questions
(iii) (01001110)2
(iv) (0100101000000001)2
Answers to que.3:
(i) (27)8
(ii) (33)8
(iii) (15003)8 Back to Questions
Answers to que.4:
(i) (4A01)16
(ii) (1D)16 Back to Questions
(iii) (1F)16
Representation of Characters in
Memory
1. BCD (Binary Coded Decimal)
2. ASCII (American Standard Code for
Information Interchange)
3. EBCDIC (Extended Binary Coded Decimal
Interchange Code)
BCD Code
• BCD Codes are used to send the decimal
information into digital system or to get decimal
information from digital system.
• Ex: 8421, 7421, 6311, 5311 etc.
• 8421 is most popular.
• In 8421 each decimal digit is expressed by its 4
bit binary equivalent.
(429)10 = (0100 0010 1001)BCD
Decimal BCD (8421) Code
5311 Code Binary
0 0000 0000 0000
1 0001 0001 0001
2 0010 0011 0010
9 1001 1001
10 0001 0000 1010
ASCII Code
• Most popular in Data Communication
• ASCII-7 (7 bits)
• ASCII-8 (8 bits)
• ASCII-7 represents 2^7 = 128 different
combinations
• ASCII – 8 represents 2^8 = 256 different
combinations
EBCDIC
• 8-bit character coding by IBM mainframes