Professional Documents
Culture Documents
Information representation in
binary
Architecture of microprocessor and microcontroller systems
Dr. Samer Lahouar (samer.lahouar@crmn.rnrt.tn)
Class Notes: https://shorturl.at/arAEI
2 Introduction
The decimal numbering system (i.e., digits 0 to 9) is
not adequate to process and store information on a
PC
It’s unpractical to store numbers using the digits 0 to
9 (10 states) directly on an electronic circuit
It’s difficult to design electronic circuits that can do
operations (arithmetic or others) with the digits 0 to 9
Examples:
B=2: (100101)2 =1x25+0x24+0x23+1x22+0x21+1x20
= 32+4+1= (37)10
B=16: (A3D)16 = 10x162+3x161+13x160 = (2621)10
B=8: (1025)8 = 1x83+0x82+2x81+5x80 = (533)10
B=7: (1025)7 = 1x73+0x72+2x71+5x70 = (362)10
S. Lahouar
8 Conversion: Base 10 (decimal)→ Base B
Digits ai should be determined.
The polynomial is divided by B:
𝑁10 𝑎𝑛−1 𝐵𝑛−1 + 𝑎𝑛−2 𝐵𝑛−2 + 𝑎𝑛−3 𝐵𝑛−3 + ⋯ + 𝑎1 𝐵 + 𝑎0
=
𝐵 𝐵
a0 < B N10 B
Remainder a0 𝑄1 = 𝑎𝑛−1 𝐵𝑛−2 + 𝑎𝑛−2 𝐵𝑛−3 + 𝑎𝑛−3 𝐵𝑛−4 + ⋯ + 𝑎1
Q1 B
a1 𝑄2 = 𝑎𝑛−1 𝐵𝑛−3 + 𝑎𝑛−2 𝐵𝑛−4 + 𝑎𝑛−3 𝐵𝑛−5 + ⋯ + 𝑎2
Q2 B
a2 𝑄3 = 𝑎𝑛−1 𝐵𝑛−4 + 𝑎𝑛−2 𝐵𝑛−5 + 𝑎𝑛−3 𝐵𝑛−6 + ⋯ + 𝑎3
Qn B
S. Lahouar
an-1 0
9 Conversion: Base 10 (decimal)→ Base B
Example 1: (95)10 = (?)2
95 2
1 47 2
a0
1 23 2
(95)10 = (1011111)2
1 11 2
1 5 2
1 2 2
0 1 2
1 0
a6
S. Lahouar
10 Conversion: Base 10 (decimal)→ Base B
Example 2: (95)10 = (?)8
95 8
7 11 8 (95)10 = (137)8
a0
3 1 8
1 0
a2
Example 3: (95)10 = (?)16
95 16
15 5 16 (95)10 = (5F)16
a0 5 0
S. Lahouar a
11 Conversion Table: decimal, binary, octal and hexadecimal
Decimal Binary Octal Hexadecimal
0 0000 0 0
1 0001 1 1
2 0010 2 2
3 0011 3 3
4 0100 4 4 Octal Digits
5 0101 5 5
6 0110 6 6
7 0111 7 7 Hex Digits
8 1000 10 8
9 1001 11 9
10 1010 12 A
11 1011 13 B
12 1100 14 C
13 1101 15 D
14 1110 16 E
S. Lahouar
15 1111 17 F
12 Conversion from 2 → 8 or 16
Based on the previous table:
1 octal digit is represented by 3 bits (8=23)
1 hexadecimal digit is represented by 4 bits (16=24)
Therefore, to convert:
binary to octal, bits are grouped by 3 (starting from a0)
binary to hexadecimal, bits are grouped by 4 (starting from
a0)
Then, the previous table is used to make the conversion.
Examples: (1011111)2 = (?)8 et (1011111)2 = (?)16
(001011111)2 = (137)8
(01011111)2 = (5F)16
S. Lahouar
13 Conversion from 8 or 16 → 2
Using previous table, octal or hexadecimal digits are
converted to their equivalent binary representations:
1 octal digit: 3 bits
1 hexadecimal digit: 4 bits
Examples: (137)8 = (?)2 and (5F)16 = (?)2
(1 3 7)8=(001 011 111)2
S. Lahouar
14 Conversion between 8 16
The Binary system is used as intermediary system:
Examples: (271)8 = (?)16 and (2FA)16= (?)8
(271)8 = (010 111 001)2 = (0 1011 1001)2=(B9)16
S. Lahouar
15 Signed numbers in binary
Signed number: Number which can be > 0 or < 0
In binary, the sign “-” does not exit, only “0” and “1”
bits are used.
A sign bit is added to the left of the number to
indicate its sign:
Sign bit = 0 N is Positive (>0)
Sign bit = 1 N is Negative (<0)
N= SB Absolute Value of N
Advantages:
Easy representation of signed numbers
Easy conversion between a number and its opposite
S. Lahouar
17 Signed Binary Numbers: Sign-Absolute value
Representation
N= SB Absolute Value of N
Drawbacks:
Double representation of 0:
For 5 bits: (0 0000)2 = 0 and (1 0000)2 = 0
Wrong results of arithmetic Operations:
For 5 bits: 14+(-14) = (?)2
1 1 1
14 = 0 1110
+ -14 = 1 1110
0 ≠10 1100
S. Lahouar
18 Signed Binary Numbers: 2’s complement
Representation
A negative number can be represented in binary
using 2’s complement defined as:
C2(N) = 2n – N
where n: number of bits of N.
Therefore, C2(N)+ N = 2n=0 (if only n bits are used for
number representations) C2(N) =– N
2’s complement is found as follows:
Invert all the bits of N (1’s complement)
Add 1 to the least significant bit (the bit to the right)
S. Lahouar
19 Signed Binary Numbers: 2’s complement
Representation
Examples: for n=5 bits
(9)10 = (0 1001)2
(-9)10=C2(9)=C1(9)+1=1 0110+1= (1 0111)2
(15)10 = (0 1111)2
(-15)10=C2(15)=C1(15)+1=1 0000+1= (1 0001)2
(0)10 = (0 0000)2
(-0)10=C2(0)=C1(0)+1=1 1111+1= (0 0000)2 with 5 bits
A unique representation of 0
(14)10 = (0 1110)2
(-14)10=C2(14)=C1(14)+1=1 0001+1= (1 0010)2
1 1 1
14 = 0 1110
+ -14 = 1 0010
S. Lahouar 0 =10 0000 0 with 5 bits
20 Signed binary numbers in 2’s complement: conversion
to decimal (Base 10)
Binary → Decimal conversion of a signed number in C2:
n-bits signed number is represented by:
N2 = (an-1an-2an-3…a0)2 where an-1: sign bit
Decimal value of N is obtained using the polynomial:
𝑁10 = −𝑎𝑛−1 2𝑛−1 + 𝑎𝑛−2 2𝑛−2 + 𝑎𝑛−3 2𝑛−3 + ⋯ + 𝑎0 20
Examples: For n=5 bits
(1 0001)2=-1x24+0x23+0x22+0x21+1=-16+1=-15
(1 0010)2=-1x24+0x23+0x22+1x21+0=-16+2=-14
(1 0111)2 =-1x24+0x23+1x22+1x21+1=-16+4+2+1=-9
(0 1111)2 =-0x24+1x23+1x22+1x21+1=8+4+2+1=15
S. Lahouar
21 Min and Max values of an n-bit signed number
S. Lahouar
22 Real numbers representation in binary (1)
A real number R is represented in binary as:
(R)2 = (an-1an-2an-3…a0,a-1a-2a-3…a-m)2
𝑅 = 𝑎𝑖 2𝑖 = 𝑎𝑛−1 2𝑛−1 + 𝑎𝑛−2 2𝑛−2 + 𝑎𝑛−3 2𝑛−3 + ⋯ + 𝑎0 20 + 𝑎−1 2−1 + 𝑎−2 2−2 + ⋯ + 𝑎−𝑚 2−𝑚
𝑖=−𝑚
S. Lahouar
Integer Part Decimal Part
23 Real numbers representation in binary (2)
(R)2 = (an-1an-2an-3…a0,a-1a-2a-3…a-m)2
𝑛−1
𝑅 = 𝑎𝑖 2𝑖 = 𝑎𝑛−1 2𝑛−1 + 𝑎𝑛−2 2𝑛−2 + 𝑎𝑛−3 2𝑛−3 + ⋯ + 𝑎0 20 + 𝑎−1 2−1 + 𝑎−2 2−2 + ⋯ + 𝑎−𝑚 2−𝑚
𝑖=−𝑚
Example:
(1101.1101)2=1x23+1x22+0x21+1x20+1x2-1+1x2-2+0x2-3+1x2-4
= 8+4+0+1+1/2+1/4+0/8+1/16
= 13+0.5+0.25+0.0625
= 13.812510
S. Lahouar
24 Real numbers representation in binary (3)
To find the digits ai of the fractional part, it should be multiplied
successively by 2:
R = a-1 2-1+a-2 2-2+a-3 2-3+ … +a-m 2-m
2R = a-1 +a-2 2-1+a-3 2-2+ … +a-m 2-m+1
Integer Decimal Part = R1
(0.23)10 = (?)2
0.23 × 2 = 0.46 a-1 = 0 et R1 = 0.46
0.46 × 2 = 0.92 a-2 = 0 et R2 = 0.92
0.92 × 2 = 1.84 a-3 = 1 et R3 = 0.84
0.84 × 2 = 1.68 a-4 = 1 et R4 = 0.68
0.68 × 2 = 1.36 a-5 = 1 et R5 = 0.36
0.36 × 2 = 0.72 a-6 = 0 et R6 = 0.72
0.72 × 2 = 1.44 a-7 = 1 et R7 = 0.44
Therefore : 0.2310 = 0.0011101…2: Infinite number of bits.
Typically, we take 4 or 5 bits or as many as available bits
S. Lahouar
26 Real numbers: fixed-point representation
A fixed number of bits n is used to represent the integer part and a fixed
number of bits m is used to represent the decimal part
Examples: Represent 15.25 and 0.23 using fixed-point representation with
n=4 bits for the integer part and m=4 bits for the decimal part:
15.2510 = (1111.01)2=(11110100)FP4,4
0.2310 = (0.0011101)2=(00000011)FP4,4
Drawbacks: since n and m are fixed, it’s difficult to represent with this
format big numbers and small numbers at the same time.
15.25: even though the decimal part has 2 bits 0 at the end, we can’t
use these bits to represent bigger numbers in the integer part: we are
limited to 15, which is 11112
0.23: even though the integer part is 0, we can’t use it to represent the
decimal part more accurately. In fact, we used only the first 4 bits of
the decimal part, and we ignored the rest
S. Lahouar
27 Real numbers: Floating-point representation
A number R is represented in a floating-point format as:
R=(-1)S × 1.M × 2e
with:
S: sign bit (0R Positive and 1R Negative)
M: mantissa: represents the bits after the decimal point
e: exponent: represents the position of the decimal point in R
Examples:
15.2510 = 1111.012 = (-1)0 × 1.11101 × 23 S=0, M=11101, e=3
S. Lahouar
28 Standard representation IEEE 754 single precision (FLOAT)
S. Lahouar
29 Standard representation IEEE 754 single precision (FLOAT)
Examples:
0.7510=0.112=(-1)0 × 1.1 × 2-1 S=0 ; M=1 ; e=-1
E=e+127=126=011111102
FLOAT: 0.7510= 0 01111110 10000000000000000000000
S. Lahouar
30 Standard representation IEEE 754 double precision(DOUBLE)
S. Lahouar
31 Standard representation IEEE 754 double precision(DOUBLE)
Examples:
0.7510=0.112=(-1)0 × 2-1 × 1.1 S=0 ; M=1 ; e=-1
E=e+1023=1022= 011111111102
Double: 0.7510= 0 01111111110 10000000000000000000…0
S. Lahouar
32 IEEE 754 standard representations: Special numbers
S. Lahouar
33 Addition in binary (1)
For 1 bit, the addition is performed as follows:
0+0 = 0 0+1 = 1 1+0 = 1 1+1 = 10: (1: Carry)
For multiple bits, the bits at the same order are added
together along with the Carry (if present)
The sum of 2 numbers of n bits each is written using
n+1 bits.
S. Lahouar
34 Addition in binary (2)
Examples:
1 1 1 1 1 1 1 1 1 1
0011 11111 111101
+ + +
0101 10101 011110
1000 110100 1011011
S. Lahouar
35 Binary subtraction (1)
The subtraction of 2 binary numbers (a-b) is performed by
adding C2(b) to a Addition:
a-b=a+C2(b)= a+C1(b)+1
Subtraction algorithm:
Add C2(b) to a
Check if there is an overflow:
Case of an extra bit: ignore the extra bit. The number found (positive
in this case) is the result of a-b.
Case of no extra bit: the result of a-b is negative. The polynomial
could be applied to find the decimal equivalent.
S. Lahouar
Binary subtraction (2)
36
Examples: for n=5 bits:
910-510 = ? (0 1001)2 – (0 0101)2 = ?
C2(0 0101) = 1 1011 0 1001+1 1011 = 1 0 0100
Extra bit (6 bits): ignored
The result is positive and correct: (0 0100)2=4
510-910 = ? (0 0101)2 – (0 1001)2 = ?
C2(0 1001) = 1 0111 0 0101+1 0111 = 1 1100
No extra bit (5 bits)
The result is negative and correct (1 1100)2 =-16+8+4=-4
-910-510 = ? C2(0 1001)+C2(0 0101) = ?
1 0111+ 1 1011 =1 1 0010
Extra bit (6 bits) ignored The result is negative and correct: 1 0010=-16+2=-14
-910-910 = ? C2(0 1001)+C2(0 1001) = ?
1 0111+ 1 0111 =1 0 1110
Extra bit (6 bits) ignored The result is positive and incorrect : 0 1110: We have
an overflow (the result can’t be written with 5 bits)
910+910 = ? (0 1001)2+(0 1001)2 = 1 0010
No extra bit. The result is negative and incorrect : 1 0010: We have an overflow
(the result can’t be written with 5 bits)
S. Lahouar
37 Hex Addition/Subtraction
Addition/Subtraction performed digit by digit
Addition: there is a carry if the result> F ⇒ Carry should be
added to next digit
Subtraction: there is a borrow if b>a while calculating a-b
Examples:
1 1 1 1
A39 F53C F53C
+ + - 1
1AB A2C4 A24 B
BE4 1 98 0 0 52F1
S. Lahouar
38 Addition of 2 floating-point numbers
Calculate: A+B=(-1)Sa MA2Ea+(-1)Sb MB2Eb
This computation is done in the computer by the FPU
(Floating Point Unit) and not the microprocessor
Algorithm:
Align MA and MB according to Ea > Eb or Ea < Eb
Find the sum of the aligned mantissas (if A or B < 0, the C2
should be used instead of the number)
Normalize the result under the form (-1)S 1.M x 2e
S. Lahouar
39 Addition of 2 floating-point numbers: Example
Find the sum of A = 1.1*22 and B = 1.1011*23
A+B = 1.1*22 + 1.1011*23
Computation steps:
Alignment of the mantissas based on the largest
exponent (3 in this case) ⇒ A = 0.11*23
Therefore: A+B = (0.11+1.1011)*23
Sum of the aligned mantissas: 0.11+1.1011=10.0111
Normalization: A+B=10.0111 * 23 =1.00111 * 24
S. Lahouar
40 Multiplication of 2 floating-point numbers
Calculate: A x B = (-1)Sa MA2Ea x (-1)Sb MB2Eb
⇒ A x B = (-1)Sa+Sb (MA*MB) 2Ea+Eb
This computation is done in the computer by the FPU
(Floating Point Unit) and not the microprocessor
Algorithm:
Calculate the sign of the result: (-1)Sa+Sb
Calculate (MA*MB) and (Ea+Eb)
Normalize the result under the form 1.M x 2e
S. Lahouar
41 Multiplication of 2 floating-point numbers
Find the product of A = 1.1*22 and B = 1.1011*23
A*B = 1.1*22 * 1.1011*23
Therefore: A*B= 1.1 * 1.1011 * 22+3
Computation steps: 1.1011
* 1.1
Calculate: MA*MB=10.10001
+1 11011
1 1 1
S. Lahouar
42 Units used in binary
1 bit ⇒ 1 Unit = 0 or 1
1 Byte = 8 bits
S. Lahouar
43 Binary Coding
Binary Coding is the operation of attributing a group of bits to
numeric or alphanumeric values according to the required
application
The objective of Binary Coding is to be able to store and process
information, in binary format, on a Computer
There are many types of binary codes that are used for different
applications:
Natural Binary Coding
BCD Coding
Gray Coding
ASCII Coding
Error detection and correction Coding
….
S. Lahouar
44 Binary Coded Decimal: BCD
S. Lahouar
45 ASCII Coding
S. Lahouar