Chapter 2: Encoding Scheme and number System
Let’s assume
Character or Special → code (ASCII) → Binary value
Eg
A → 65 → 01000001
Numeric Value: Number System
- Method representing a number
- Reading a number
- Calculation
- Computation
Number System
1.Decimal Number System
2.Binary Number System
3.Octal Number System
4.Hexa-Decimal Number System
Decimal Number system
Deci - 10
Number System 10 digits - 0,1,2,3,4,5,6,7,8,9
Base - 10 (number of digits in a number systems)
9
+ 1
--------
0 1 carry
1 1
9 9
+ 1
-------------------------- -------------------------- ---------------------------
1 0 0
If there is a place, if not then overflow
A cell or a block or a place can have only 1 digit
9 being last number it will 0, 1 will carry forward
Face Value and place value
Face value is the number itself
Place value is the position of the number, Increase from right to
left, base raise to power the number
100
Eg 768
Eg
78.95
Calculate 7982/10
Division rule:
m/n
Remainder should 0 to n-1
If m greater than and then remainder can be 0(if divisible), 1
to n-1
If m is equal to n then remainder will be 0
If m is less than n then the remainder will be m
Add 1 Successor
Subtract 1 predecessor
1+1=2+1=3+1=4+1=5+1=6+1=7+1=8+1=9+1=0 1 carry
75123-22398
10 11 13
7 5→ 4 1→ 0 2→ 1 3
2 2 3 9 8
-
5 2 7 2 5
While Borrowing the numbers becomes 2 digit number
irrespective of place value
7896+7654
78+34 78+34
7 1 carry 8 7 8
3 4 3 4
1 12→ 2 1 1 2
Answer is 12 Answer is 112
1 in overflow
Decimal number system
0-9 0-9 0-9 0-9 0-9 0-9 0-9
7 2 3 5 4 6 9
78
106 105 104 103 102 101 100
0-9 0-9 0-9 0-9 0-9 0-9 0-9 0-9
8 7 2 3 5 4 6 9
107 106 105 104 103 102 101 100
Binary Number System
Base : 2
Digits: 0, 1
Addition
0+0=0
0+1=1
1+1
1 carry
1
+1
1 10→ 0
Answer = 10
If the block is not available answer 0, 1 will overflow
1+1+1
1 carry
1 10
+1
+1 +1
1 11→ 1
Answer: - 11
Counting - 4 bits (nibble) or 8 bits(byte) 16 bits (2 bytes)
0 is the first number of a number system
Decimal Binary
0 0000 (00000000)
+1= 1 0000
+1
-------
0001
+1=2
1 (carry)
0 0 0 1
+1
0 0 1 10→ 0
0010
+1=3 0011
4
1 1
0 0 1 1
+1
0 1 10 → 10 →
0 0
0100
5 0101
6 0110
7 0111
8 1000
9 1001
10
1
1 0 0 1
+1
1 0 1 10→ 0
1010
11 1011
12 1100
13 1101
14 1110
15 1111
16 00010000
1 1 1 1
0 0 0 0 1 1 1 1
+1
0 0 0 1 0 0 0 0
If it is 4 bit value then answer will
be 0000
If it is 8 bit then answer will be
00010000
17 00010001
18 00010010
PLACE VALUE is BASE Raised to power position
Eg
A number system having base value as 9
Digits - 0 to 8
7324
7x93+ 3x92 +2x91+4x90
Decimal number: exponential form
76321 = 7x104+ 6x103+3x102+2x101+1X100
7 6 3 2 1
104 103 102 101 100
4 3 2 1 0
Ten thousands hundreds tens Ones
thousand
Seventy six thousand
Binary number
20
Convert Decimal to Binary
- Divide decimal number by the base of number in which we
want to convert (for binary divide by 2, octal divide by 8 so
on)
- 3 column for divisor (base of the number in which you are
converting) , quotient, remainder
- Continue till quotient is 0
- BASE HELPS TO IDENTIFY THE NUMBER SYSTEM
Number is represented as
(number)base
Eg
(14)8 octal number
(234)10 Decimal number
(20)10 to (?)2
Convert decimal number to binary number
(35)10 to (?)2
Decimal number to binary number
(100011)2
(44)10 TO (?)2
Solve (65)10 to (?)2
(1000001)
(54)10 to (?) 2
Answer (110110)
Sign LSB
Bit
Or
MSB
MSB- MOST SIGNIFICANT BIT if it 1 the number is
negative
LSB - LEAST SIGNIFICANT BIT If 1 then number is
odd if 0 then even
(35)10 to (?)2
Decimal number to binary number
(100011)2
Example
(20)10 to (?)2
Answer (00010100)2
0 represents positive number
(-20) to (?)2
Answer (10010100)2
1 represents negative number
(10001111) Negative odd
(01001000) Positive even
(11110000) Negative even
(10000001) Negative odd
(10000000) -128
(00000000) Valid zero
(011111100) Positive even
(10010111) Negative odd
(140)10 to (?)2
(10001100)2
1 byte is given to store this data
Answer : -12
2 bytes are given to store this data
Answer: 140
1 byte can store -128 to 127
2 bytes can store -32758 to 32767
Conversion Binary to decimal
X3
+5
X2
Answer : 22
22/2 =11
11-5=6
6/3=2
Each face value will be multiplied with the place value.
Remember : place value of any number system is
base raise to power the position
(101011)
0 0 1 0 1 0 1 1
27 26 25 24 23 22 21 20
128 64 32 16 8 4 2 1
BODMAS
READ this X3
American X of 3
Computer or Britisher X exponent 3 , X raise to
power 3 (BEDMAS)
Decimal to Binary
Binary to Decimal
(101001)2 to (?)10
=1x25+0x24+1x23+0x22+0x21+1x20
=32+0+8+0+0+1
= 41
Real number representation
Decimal number 345.67
3 4 5 . 6 7
100 10 1 .1 .01
102 101 100 10-1 10-2
.6 means 6/10
.07 means 7/100
Real number conversion to Binary
1.Convert before decimal - long division
2.Convert after decimal
Eg
Convert 20.65 to binary
1 convert 20 to binary using long division
2. Convert .65 to binary
20 -
20 = (10100)2
After decimal point conversion
(20.65)10 = (10100.101)2
(345.67)10 =(101011001.1010)2
Q Solve the following:
(45.45)10 to (?)2
ANSWER: - (101101.011)2
(55.62)10 to (?)2
(110111.1001)2
(101101.011)2
1 0 1 1 0 1 . 0 1 1
25 24 23 22 21 20 2-1 2-2 2-2
32 16 8 4 2 1 0.5 0.25 0.125
=1X32+0x16+1x8+1x4+0x2+1x1+0x0.5+1X0.25+1x0.125
=32+0+8+4+0+1+0+0.25+0.125
=44.375
(101010.1011)2 to (?)10
=1x25 +0x24+1x23 +0x22 +1x 21+0x20 +1x 2-1+0x2-2+1x2-3 +1x2-4
=32+0+8+0+2+0+0.5+0+0.125+0.0625
=42.6875
(1010111.01101)2 to (?)10
=87.40625
OCTAL NUMBER SYSTEM
Base -8
Digits - 0 to 7
7+1=0 1 carry
0,1,2,3,4,5,6,7,0 1 carry
Programming language consider a number beginning with 0 as
octal number, in Python 0o
It will become an octal number then converted to decimal
and then display
Counting of octal number
Decimal number octal binary
0 000 0000
1 001 0001
2 002 0010
3 003 0011
4 004 0100
5 005 0101
6 006 0110
7 007 0111
1 (carry)
007
+1
-----
010
8 011
9 012
10 013
:::
020
::::077
1
077
+1
----
1
0100
Octal 7 is represented as 3 bits in binary (111)
Maximum number of bits required to store an octal
number is 3
0o1 (1 in octal) is equal to 001 in binary
0o3 ( 3 in octal ) is equal to 011 in binary
0o7 ( 7 in octal) is equal to 111 in binary
0o45 is 2 digit octal number
4 = 100
5=101
(100101)2
(327)8.45
(456.45)10 to (?)8
(710.3463)8
Octal to Decimal
(451)8 to (?)10
4 5 1
82 81 80
64 8 1
4x64+5x8+1x1=297
(345.123)8 to (?)10
3 4 5 . 1 2 3
82 81 80 8-1 8-2 8-3
64 8 1 0.125 0.0156 0.0019
3x64+4x8+5x1+1x0.125+2x0.0156+3x0.0019
=229.1621
HIGHEST OCTAL NUMBER 7 CAN BE REPRESENTED IN 3
BITS 7 - 111
Where octal is taking 1 bit there binary is taking 3 bits
There is no LSB in Octal
Octal to binary
Example 1:
(345)8 to (?)2
3=011
4=100
5-101
(011100101)2
Example 2:
Q why we make a group of 3 bits for octal to binary
conversion or vice versa?
Answer:
because the maximum number of octal is 7 which
can be represented in 3 bits in binary therefore we
need 3 bits to represent an octal number.
Binary to octal
Make a group of 3 bits from right to left
We will octal equivalent of 3 bits group
(1001000100)2 to (?)8
(1010101001011)2 to (?)8
0 0 1 0 1 0 1 0 1 0 0 1 0 1 1
-------------- ------------- ------------- ------------- --------------
1 2 5 1 3
(12513)8
(100010100)2 to (?)8
(100010100)
100 = 4
010=2
100=4
(424)8
From octal to binary write binary equivalent of each octal
digit
From binary to octal make a group of 3 bits from right to left
then write octal number of each 3 bit group.
Hexa-Decimal number System
Base : 16 (hexa+deci=6+10=16)
Digit - 0 to 9 A(10),B(11), C(12), D(13),E(14),F(15)
0 to 9 , A to F
0+1=1
1+1=2
9+1=A(10)
A+1=B(11)
B+1=C(12)
C+1=D(13)
D+1=E(14)
E+1=F(15)
F+1=0 1 carry forward
In Programming language indicate 0x it is a hexa-decimal
value
Computer uses hexa-decimal value for memory address
decimal Hexadecimal BINARY
0 0x00 0000
1 0x01 0001
2 0x02 0010
3 0x03 0011
4 0x04 0100
5 0x05 0101
6 0x06 0110
7 0x07 0111
8 0x08 1000
9 0x09 1001
10 0x0A 1010
11 0x0B 1011
12 0x0C 1100
13 0x0D 1101
14 0x0E 1110
15 0x0F 1111
16 0x10
17 0x11
18 0x12
19 0x13
20 0x14
21 0x15
22 0x16
23 0x17
24 0x18
25 0x19
26 0x1A
27 0X1B
28 0X1C
29 0X1D
30 0x1E
21 0x1F
22 0x20
0xFF next number will be 0x100
0xFFF next number will be 0x1000
0xEFF next number will be 0XF00
1 0
Conversion of decimal number to hexadecimal
Divide by 16
(789)10 to (?)16
(1342)10 to (?)16
16 1342
16 83 14 ( E)
16 5 3
(0x53E)
(234.456)10 to (?)16
answer decimal integral
.456 x16 7.296 .296 7
.296x16 4.736 .736 4
.736x16 11.776 .776 11( B)
(.74B)16
Hexadecimal to decimal
(A2E)16 to (?)10
A 2 E
162 161 160
256 16 1
AX256+2X16+EX1
= 10X256+2X16+14X1
=2560+32+14
=2606
(A2E.7AF)
A 2 E . 7 A F
162 161 160 1/16= 1/256 1/4096
16-1 16-2 16-3
256 16 1 0.0625 0.0039 0.00244
=AX256+2X16+eX1+7X0.0625+AX0.0039+FX0.00244
=2560+32+14+0.4375+0.039+0.0366
=2606.4801
HEXADECIMAL TO BINARY
- GROUP 4 BITS
Converting Binary to octal and Vice Versa
- Before Group 3 bits from right to left
- Write octal equivalent
- After decimal group3 bits from left right
- -write octal equivalent
Example
1.(1010110)2 to (?)8
For 3rd group place leading 0
001010110
001=1
010=2
110=6
(126)8
2.(1110011)2 to (?)8
001110011
(163)8
3.(110111.111010)
4.(101010101.1101001)
101010101.110100100
(525.644)8
5.(10101.11001)
(25.62)8
23 22 21 20
8 4 2 1
Converting Binary to hexadecimal
- Before Group 4 bits from right to left
- Write octal equivalent
- After decimal group 4 bits from left right
- write octal equivalent
1.(111010101001)2 to (?)16
111010101001
23 22 21 20
8 4 2 1
1 1 1 0
1 0 1 0
1 0 0 1
EA9
(EA9)16
(1101)
(8+4+1)=13
Eight four two one
8 4 2 1
(1100101010110.110101)
0001100101010110.11010100
=1956.D4
Binary Addition
Rules:
1.0+0=0
2.0+1=1
3.1+0=1
4.1+1=0 carry 1
5.1+1+1= 1 carry 1
(10101)+(11001)
=(101110)2
(111001)+(1110101)
If one number have less binary
digits than the other add 0 on
left
1 1 1 1
0 1 1 1 0 0 1
1 1 1 0 1 0 1
1 0 1 0 1 1 1 0
(10101110)2
(11001.110)+(10101.111)
(101111.101)
1 1 1 1
1 1 0 0 1 . 1 1 0
1 0 1 0 1 . 1 1 1
1 0 1 1 1 1 1 0 1
(101111.101)2
(10101.111)+(1010.01)
1 1 1 1 1 1 1
1 0 1 0 1 . 1 1 1
0 1 0 1 0 . 0 1 0
1 0 0 0 0 0 0 0 1
(100000.001)
Character data
‘A’, ‘0’,’!’,”a”,’<’
Coding schemes are used for
characters
1.ASCII
American Standard Code for
Information Interchange
Code is given each character
It is a 7 bit coding scheme
27=127 to -128
Then converted to 8 bit data
Eg
A ASCII value 65 (decimal
number)
ASCII values to learn
A- 65
a - 97
0 - 48
Give binary equivalent of CAT
C=67=01000011
A=65=01000001
T=84=01010100 (64+16+4)
CAT=(010000110100000101010100)
128 64 32 16 8 4 2 1
Give binary equivalent of BacK
B=66=01000010
a=97=01100001
c=99=01100011
K=75=01001011
SHORTCUT
128 64 32 16 8 4 2 1
Binary equivalent of 85
128 64 32 16 8 4 2 1
1
85-64=21
128 64 32 16 8 4 2 1
1 1
21-16=5
128 64 32 16 8 4 2 1
1 1 1
5-4=1
128 64 32 16 8 4 2 1
1 1 1 1
Put zero in remaining blocks
128 64 32 16 8 4 2 1
0 1 0 1 0 1 0 1
85 = (01010101)
--------------------------------------------------------------------
Convert 92 in binary
128 64 32 16 8 4 2 1
0 1 0 1 1 1 0 0
(01011100)
2.ISCII - Indian Script Code
for Information Interchange
8 bit coding scheme
28=256
3.UNICode- standard coding
scheme for all the characters
Full form Universal Coding
Format:
UTF-8,UTF-16,UTF-32
UTF - 8
-Coding scheme in 8 bits
-Called octet form
8,16,24,32
Unicode point Unicode point Number of
in decimal in octet
hexadecimal
U0 - U127 U+00 - U+07F 1
Can be
represented in
8 bits or 1
byte
U128 - U2047 U+80 - U+7FF 2 octets
(16 bits)
U2048-U65535 U+800 - 3 octets
U+FFFF (24 bits)
U65536 - U U+10000 - 4 octets
2097151 U+1FFFFF (32 bits)
Eg
(120)10 in UTF-8
120=1111000
0 1 1 1 1 0 0 0
(200)10 in UTF-8
200=00000001 01001000
10000001 01001000
How data is stored in UTF-8
Step 1: in how many bits the data can be
represented?
Note: in 1 byte or 8 bits you can have
maximum 7 bits
Suppose it can be represented in 7 bits first
bit will have 0 and we use 1 byte
(1011010)
0 1 0 1 1 0 1 0
Suppose data is of 2 bytes (more than 7
bits)
Represent (1110011110) in UTF-8
Step 1: count number of bits=10 bits
Step 2: find 1 byte or 2 bytes?
(1110011110)
1 1 0 0 1 1 1 0 1 0 0 1 1 1 1 0
110 in 1st byte and 10 in second byte
indicates that data need 2 bytes
Note: 2 bytes can maximum represent 11 bits
Represent in 3 bytes
NOTE:- if data is more than 11 bits then we
need 3 bytes(24 bits)
(1101101101101101)
(1101 101101 101101)
11101101 10101101 10101101
Number First second Third Fourth Data
of bytes
1 0 7 bits
2 110 10 11 bits
3 1110 10 10 16 bits
4 11110 10 10 10 21 bits
4 bytes:
11110 3 bits for data
10 6 bits for data
10 6 bits for data
10 6 bits for data
UTF-16
- 16 bits
(1001010)
0 0 0 0 0 0 0 0 0 1 0 0 1 0 1 0
But read all 16 bits together
UTF-32
0000000000000000000000000010001010
NOTE: ASCII hex=Unicode