Information Representation and Number System
Information Representation and Number System
information representation
ENP/1CP/informatique1
L. Djaafri
Introduction : Decimal system
2 1 0
185 (10) = 100 + 80 + 5
= 1 × 100 + 8 × 10 + 5 × 1
= 1 × 102 + 8 × 101 + 5 × 100
ENP/1CP/cours informatique1
2
L.Djaafri
Principle
ENP/1CP/cours informatique1
3
L.Djaafri
Principle
• Any integer N is represented in base B by a
sequence of digits :
N = an an-1... a2 a1 a0
= 8 +4 +0 +1
1101(2) = 13
ENP/1CP/cours informatique1
5
L.Djaafri
Binary number system
0 0 9 1001
1 1 10 1010
2 10 11 1011
3 11 12 1100
4 100 13 1101
5 101 14 1110
6 110 15 1111
7 111 16 10000
8 1000 .. ..
ENP/1CP/cours informatique1
6
L.Djaafri
Binary number system
1 20 1
10 21 2
100 22 4
1000 23 8
10000 24 16
100000 25 32
.. .. ..
10…00 (n 0) 2n
ENP/1CP/cours informatique1
7
L.Djaafri
Octal number system
Set of 8 symbols: {0, 1, 2, 3, 4, 5, 6, 7}
• Exemple :
2 1 0
613 (8) = 6 × 82 + 1 × 81 + 3 × 80
= 6 × 64 + 1 × 8 + 3 × 1
= 384 +8 +3
613 (8) = 395
ENP/1CP/cours informatique1
8
L.Djaafri
Hexadecimal number system
= 14 × 256 +0 +9
E09 (16) = 3593
ENP/1CP/cours informatique1
9
L.Djaafri
Binary, hexa and octal roles
• Binary system is used to treat and stock information
on computer
• Binary representation of information is very verbose,
so hexadecimal and octal numbers system are used
to reduce the binary representation
• Example:
A same value represented in base 2, 8 an 16
binary 101 0110 0000 0111 1011 1101
octal 25403675
hexadecimal 5607BD
ENP/1CP/cours informatique1
10
L.Djaafri
Conversion from any base x to decimal
• Method1:
1. number each digit from right to left starting from 0
2. Multiply each digit by the base-x raising to the position number
obtained in step 1
3. Sum all values obtained in step 02
• Example
20213 33 32 31 30
2 0 2 1
ENP/1CP/cours informatique1
12
L.Djaafri
Conversion from Decimal to any base x
• Method 1
1. Successive division on x
2. Take the remainders from the last to the first
3. Stop when quotient equals 0.
• Example
4 4 10= ( ? ) 2
44 2 = 22 rem 0
22 2 = 11 rem 0
11 2 = 5 rem 1
5 2= 2 rem 1
2 2= 1 rem 0
1 2= 0 rem 1
4 4 10= ( 1 0 1 1 0 0 ) 2
ENP/1CP/cours informatique1
13
L.Djaafri
Conversion from Decimal to any base x
• Method 2 : need power of x values
• Example
4410=(?)2 25 24 23 22 21 20
32 16 8 4 2 1
1 0 1 1 0 0
44 = 32+8+4
= 125 + 024 + 123 + 122 + 021 + 020
44 10=1011002
ENP/1CP/cours informatique1
14
L.Djaafri
binary to Hex & Hex to binary
Need to use a look up table
• Example
101101100110110011=(?)16
2 D 9 B 3
1011011001101100112= 2D9B316
ENP/1CP/cours informatique1
16
L.Djaafri
Conversion from Hexa to Binary
• Method
Break up each hexadecimal digit into 4 binary digit. Right to left
• Example
1DF8316 = (?)2
1 D F 8 3
ENP/1CP/cours informatique1
17
L.Djaafri
Binary to Octal & Octal to Binary
Need to use a look up table
Decimal Octal Binary
0 0 000
1 1 001
2 2 010
3 3 011
4 4 100
5 5 101
6 6 110
7 7 111
ENP/1CP/cours informatique1
18
L.Djaafri
Conversion from Binary to Octal
• Method
Replace each 3 binary digits with its octal equivalent
• Example
• 1011011001101100112= (?)8
5 5 4 6 6 3
• 1011011001101100112= 5546638
ENP/1CP/cours informatique1
19
L.Djaafri
Conversion from Octal to Binary
• Method
Break up each octal digit into 3 binary digit. Right to left
• Example
74038 = (?)2
7 4 0 3
74038 = 1111000000112
ENP/1CP/cours informatique1
20
L.Djaafri
Summary conversion base 2, base 8
and base 16
Binary
Octal Hexa
Example
127038 =(?)16
1→001
2→010
127038 → 7→111 →001 010 111 000 0112
0→000
3→011
0001→1
0101→5
001 0101 1100 0011 → 1100→C
→15C316
0011→3
127038 =15C316 ENP/1CP/cours informatique1
21
L.Djaafri
Conversion from Base-x to Base-y
• Method
Use conversion from base-x to Decimal, then from Deciaml to Base-y
• Example
314 5= (?)2
Base-5 to decimal:
3145 = 352 + 151 + 450 = 84
Decimal to base-2
8410 = 64+16+4
= 26 + 24 + 22
= 126 + 025 + 124 + 023 + 122 + 021 + 020
8410 = 10101002
ENP/1CP/cours informatique1
22
L.Djaafri
Arithmetic with binary base system
Rules
0 0 1 1 11..
+ + + + +
0 1 0 1 1..
---- ---- ---- ---- ----
=0 =1 =1 =10 =11
carry
1000+10+1=
+ 1 0 1 1 23+21+20 11
= 1 0 0 1 0 0 1 72
1 1 1 1 1 0 + 1ENP/1CP/cours
0 1 1 = informatique1
1001001
24
L.Djaafri
Substraction
1 1 1 1 100000+1000+100
1 0 1 1 0 0 =25+23+22 44
10000+100+10+1
- 1 1 10 11 11 1 =24+22+21+20
23
10000+100+1
= 0 1 0 1 0 1 =25+22+20 21
1000+10+1
- 1 10 11 1 = 25+22+20 11
= 1 1 0 0 1 1 51
1 0 1 1 0 0 - 1 0 1 1 1 =ENP/1CP/cours
1 0 1 0informatique1
1 25
L.Djaafri
Multiplication
1 0 1 1 0 0 4 4
1 1 3
1 1
1 10 1 1 0 0 1 3 2
+ 1 0 1 1 0 0 .
= 1 0 0 0 0 1 0 0
101100 11 = 10000100
ENP/1CP/cours informatique1
26
L.Djaafri
Division
10111011 101
0 1 1 0 0 0 1 0 0 10 1
111
10
187 5 = 37 rem 2
ENP/1CP/cours informatique1
27
L.Djaafri
Binary « with comma » to decimal
1 0 1 1 , 1 0 1 2 = ?10
3 2 1 0 -1 -2 -3
1 0 1 1 , 1 0 1 2 = 1 × 23 + 0 × 22 + 1 × 21 + 1 × 20 + 1 × 2-1 + 0 × 2-2 + 1 × 2-3
= 8 +0 +2 + 1 + 1/2 +0 + 1/8
= 8 + 2 + 1 + 0.5 + 0.125
1011,1012 = 11.62510
ENP/1CP/cours informatique1
28
L.Djaafri
Decimal« with comma » to binary
4 1 , 6 8 7 5 10 = ? 2 = 1 0 1 0 0 1, 1 0 1 1 2
4 1 10 = 32+8+1 = 100000+1000+1 = 1010012
0 , 6 8 7 5 10 = ? 2 = 0 , 1 0 1 1
0,68752 = 1,3750
0,37502 = 0,75
0,75 2 = 1,5
0,5 2 = 1,0
Stop when equals 0
ENP/1CP/cours informatique1
29
L.Djaafri
Decimal« with comma » to binary
2 3 , 1 5 10 = ?2 = 10111 ,0010012
2 3 10 = 1 6 + 4 + 2 + 1 = 1 0 0 0 0 + 1 0 0 + 1 0 + 1 = 1 0 1 1 1 2
0 , 1 5 10 = ? 2 =0,001001
0,15 2 = 0,3
0,3 2 = 0,6
0,6 2 = 1,2
0,2 2 = 0,4
0,4 2 = 0,8
0,8 2 = 1,6
Stop when fractional part already exist
ENP/1CP/cours informatique1
30
L.Djaafri
Memory measurement units
• Bit (Binary Digit) : is the smallest unit of data that a computer can
process and store. A bit is a binary value, and takes only 0 or 1.
• Byte : a byte is a unit of data that is eight binary digits long
0 2n-1 infinate
representable values unrepresentable values
ENP/1CP/cours informatique1
32
L.Djaafri
Signed number representation
• In mathematics:
▪ negative numbers uses minus signe « - »
• In computing:
▪ signed number representations are required to
encode negative number in binary number
systems.
• Three methods are used :
▪ Sign-absolute value called Sign-magnitude
▪ One’s complement
▪ Two’s complementENP/1CP/cours informatique1
33
L.Djaafri
Sign and absolute value
• If we have an n-bit number, then the most significant
bit (MSB) is used to indicate the sign:
▪ 1: negative sign
▪ 0: positive sign
▪ The other bits (n-1) designate the absolute value of the
number
• Exemple : sur 4 bits .
1001 0001
sign absolute value sign absolute value
ENP/1CP/cours informatique1
34
L.Djaafri
Sign and absolute value
Exemple : All values with 3 bits jn SAV presentation:
ENP/1CP/cours informatique1
35
L.Djaafri
Sign and absolute value
• Rule :
→ range of possible values with n bits in Sign absolute
value representation:
[ - ( 2 (n-1) - 1 ) , + ( 2 (n-1)- 1 ) ]
→ 0 is represented twice :
100..00 = -0
000..00 = +0
ENP/1CP/cours informatique1
36
L.Djaafri
Addition in SAV representation
Example
perform the following operations on 4 bits:
Response:
• 10110110 is a negative number because his MSB=1
• 10110110C1 = - C1(10110110)
=- 010010012
=- (26+23+20)
10110110C1 = -73
ENP/1CP/cours informatique1
39
L.Djaafri
One’s complement
What numbers can be represented on 04 bits?
C1 Binary Number C1 Binary Number
0000 +0000 +0 1000 -0111 -7
0001 +0001 +1 1001 -0110 -6
0010 +0010 +2 1010 -0101 -5
0011 +0011 +3 1011 -0100 -4
0100 +0100 +4 1100 -0011 -3
0101 +0101 +5 1101 -0010 -2
0110 +0110 +6 1110 -0001 -1
0111 +0111 +7 1111 -0000 -0
[ - ( 2 n-1 - 1 ) , + ( 2 n-1 - 1 ) ]
ENP/1CP/cours informatique1
41
L.Djaafri
Two’s complement
• Rules
→ C2(N)=C1(N)+1
→ The most significant bit (MSB) is used to represent the sign of the number :
→ 1 for negative
→ 0 for positive
Response:
• 10110110 is a negative number because his MSB=1
• 10110110C2 = - C2(10110110)
= - 01001010 2
= - (26+23+22)
10110110C2 = -74
ENP/1CP/cours informatique1
43
L.Djaafri
Two’s complement
Question : what is the decimal value represented by the
value 101010 in C2 on 6 bits?
Response:
101010 is a negative number because his MSB=1
101010C2-1= 101001C1= - 0101102= -2210
Question : what is the C2 representation of -15 on 6 bits?
Response:
-1510= -001111 2 = 110000C1+1 =110001C2
ENP/1CP/cours informatique1
44
L.Djaafri
Two’s complement
What numbers can be represented on 04 bits?
ENP/1CP/cours informatique1
46
L.Djaafri
Two’s complement
• in general, with n bits, rang of
values represented in C2 is:
[ - ( 2 n-1) , + ( 2 n-1 - 1 ) ]
• Example:
• n=2 → range:[-21, 21-1] = [-2, 1]
• n=3 → range:[-22, 22-1] = [-4, 3]
• n=4 → range:[-23, 23-1] = [-8, 7]
• n=8 → range:[-27, 27-1] = [-128, 127]
ENP/1CP/cours informatique1
47
L.Djaafri
Addition in two’s complement
• It is based on the following principle:
→ with C2 the addition of 2 numbers
works without having to separate the
sign bits
→ If there is a carry generated by the
sign bit, it is DISCARED and the result is in
C2
→ Remember: −2n−1 ≤ result ≤ 2n−1 − 1
ENP/1CP/cours informatique1
48
L.Djaafri
Addition in two’s complement
• Example: perform operations on 5 bits in C2:
+9 0 1 0 0 1
+4 + 0 0 1 0 0
= + 13 = 0 1 1 0 1
Positive result : = 0 1 1 0 1 C2
=+011012
= + 1 3 10
ENP/1CP/cours informatique1
49
L.Djaafri
Addition in two’s complement
• Example: perform operations on 5 bits in C2:
1
+9 0 1 0 0 1
+ -4 + 1 1 1 0 0
= +5 = 1 0 0 1 0 1
+9 0 1 0 0 1
+ -14 + 1 0 0 1 0
= -5 = 1 1 0 1 1
- 1 4 10= - 0 1 1 1 0 2= 1 0 0 1 0 C2
Negative result = 1 1 0 1 1 c2
= -001012
= - 5 10
ENP/1CP/cours informatique1
51
L.Djaafri
Addition in two’s complement
• Example: perform operations on 5 bits in C2:
1 1
+4 0 0 1 0 0
+ -1 + 1 1 1 1 1
= +3 = 1 0 0 0 1 1
- 1 10= - 0 0 0 0 1 2= 1 1 1 1 1 C2
Positive result = 0 0 0 1 1 c2
Discared
carry = +000112
= + 3 10
ENP/1CP/cours informatique1
52
L.Djaafri
Addition in two’s complement
• Example: perform operations on 5 bits in C2:
1 1 1
-10 1 0 1 1 0
+ -6 + 1 1 0 1 0
= -16 = 1 1 0 0 0 0
- 1 0 10= - 0 1 0 1 0 2= 1 0 1 1 0 C2
Discared Negative result = 1 0 0 0 0 C2
- 6 10= - 0 0 1 1 0 2= 1 1 0 1 0 C2 carry
= -100002
= -16
ENP/1CP/cours informatique1
53
L.Djaafri
Addition in two’s complement
• Example: perform operations on 5 bits in C2:
1 1
-3 1 1 1 0 1
+ -2 + 1 1 1 1 0
= -5 = 1 1 1 0 1 1
1 1 0 0 1 C2= - C 2 ( 1 1 0 0 1 ) = - 0 0 1 1 1 = - 7 r e s u l t i s w r o n g
ENP/1CP/cours informatique1
57
L.Djaafri
Fixed point (m, n) coding
• In this format, a real number is represented as
follows :
ENP/1CP/cours informatique1
58
L.Djaafri
Fixed point (m, n) coding
• Example : code (-2.625) in fixed point (8,8)
-210 = - 000000102 =11111110C2
0.625= ?2 = 0.101
0.6252= 1.25
0.25 2= 0.5
0.5 2= 1.0 (end)
• Then:
-2.62510= 1111111010100000fp(8,8)
ENP/1CP/cours informatique1
59
L.Djaafri
Alphanumeric characters coding
• ASCII Table
• The American Standard Code for Information
Interchange, (pronounced aski), is a computer
standard for character encoding.
• By adopting the same coding, computer systems
designed by any manufacturer can exchange text,
numbers, punctuation marks and many other
symbols.
• Only english alphabetic characters (in the 1960s)
• it is the core of modern character tables
ENP/1CP/cours informatique1
60
L.Djaafri
Alphanumeric characters coding
• The ASCII code uses 7 bits, it allows you to define
128 digital codes, therefore 128 characters.
• the main groups of characters are:
▪ 0 to 31, are not printable characters but "control"
characters.
▪ 32 to 48: punctuation character and symbols
▪ 48 to 57: the numbers from 0 to 9
▪ 65 to 90: from A to Z
▪ 97 to 122: from a to z
ENP/1CP/cours informatique1
61
L.Djaafri
Alphanumeric characters coding
ASCII Table
ENP/1CP/cours informatique1
62
L.Djaafri
Alphanumeric characters coding
ENP/1CP/cours informatique1
63
L.Djaafri
Alphanumeric characters coding
• This is ‘Hello’ in ASCII encoding gives the
following sequence of bytes:
▪ ‘H’ coded the value 72 = 1001000
▪ ‘e’ coded the value 101 =1100101
▪ ‘l’ coded the value 108 = 1101100
▪ ‘l’ coded the value 108 = 1101100
▪ ‘o’ coded the value 111 = 1101111
ENP/1CP/cours informatique1
64
L.Djaafri