1 Numbering Systems

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

2

The Decimal System


 Consider value 234, there are:
Numbering Systems  2 hundreds
 3 tens
 4 units
Ver. 1.4
that is: 2 x 100 + 3 x 10 + 4 x 1
but 100, 10, and 1 are all powers of 10
so value 234 can be written as:
2 x 102 + 3 x 101 + 4 x 100
© 2010 - Claudio Fornaro

3 4

The Decimal System The “base-n” System


 That recurring number 10 is called the  The base of a numbering system can be
base of the numbering system any integer value greater than 1
 The usual numbering system is then  Digits can have values from 0 to
called base 10 or decimal n –1 (e.g. the base-10 system has 10
 The decimal system is a positional possible digits, from 0 to 9, for bases
numbering system, this means that the >10 see the hexadecimal system)
position of each digit implies the  When not clear from the context, the
multiplication to a corresponding power base of a value must be indicated with a
of the base (the weight of that position) subscript value: 23410
5 6

The “base-n” System The Binary System


 Base-n systems are positional  Binary means base-2
 The same value is expressed in  There are only 2 digits: 0 and 1
different ways depending on the  The digits of a binary value are called
numbering system used bits (bit comes from “BInary digiT”)
 We want to convert a value from a  A value expressed in the binary system
generic base n to base 10 because we is a sequence of zeroes and ones:
are acquainted only with the latter 1001012
 Some base-n numbering systems are  The binary system is suited for
more suited for special purposes computers

7 8

The Binary System The Binary System


write powers right to left on
 What (decimal) value correspond to each digit starting from unit
543210
1001012?  1001012 = 1 x 25 +  32 +
 From the definition of positional 0 x 24 +  0+
numbering system: to convert a base-n 0 x 23 +  0+
value to base 10, each digit must be 1 x 22 +  4+
multiplied by the power of the base 0 x 21 +  0+
(2 in this case) corresponding to the 1 x 20 =  1=
digit position -----
3710
9 10

The Binary System The Binary System


 A “bit” is a small quantity (!), so many  Other bit groupings are:
bits are grouped together to build a  A nibble is composed of 4 bits
more significant entity  A word is composed of 2 bytes (16 bits)

 A byte is a sequence of 8 bits  A double word (dword) is composed of 2

 10100011 words (4 bytes, 32 bits)


 A quad word (qword) is composed of 4
 00101001
words (8 bytes, 64)
The term word has a completely different
meaning when dealing with computer
architecture

11 12

The Binary System The Octal System


 For every bit sequence:  There are 8 digits:
 MSB: Most Significant Bit from 0 to 7 (no 8 nor 9 digits!)
The leftmost bit is the most important of
the sequence because it multiplies the  Example
highest power of the base  36428
10010010…10101001  Conversion to base 10
3210
 LSB: Least Significant Bit  36428= 3 x 83 + 6 x 82 + 4 x 81 + 2 x 80
The rightmost bit is the less important of = 195410
the sequence because it multiplies the
lowest power of the base (0)  3848= NOT AN OCTAL NUMBER (8?)
10010010…10101001
13 14

The Hexadecimal System The Hexadecimal System


 16 digits: 0 to F (subscript 16 or H)
3210
 The first 10 digits are the same as the  C1B8H = 12 x 163 + 1 x 162 + 11 x 161
decimal system, the other 6 digit + 8 x 160 = 4959210
symbols are taken from the alphabet:
AH  1010 (digit “A”, NOT letter “A”)
BH  1110
CH  1210
DH  1310
EH  1410
FH  1510

15 16

Doubling and Dabbling Doubling and Dabbling


 Fast conversion to base 10 of small  Example
binary values (it could be used with 1001012
other bases and big values, but memory  leftmost bit (MSB) is 1
computation is not such effective):  1x2=2 +0=2
 multiply the first (leftmost) bit (MSB) by  2x2=4 +0=4
the base (2) and add the second bit
 4x2=8 +1=9
 multiply the value just calculated by the
 9 x 2 = 18 + 0 = 18
base and add the third bit
 18 x 2 = 36 + 1 = 37
 continue until the LSB is added
17 18

Conversion from base 10 to n Conversion from base 10 to n


 For integer numbers only:  Example: convert 3710 to base 2
 Divide the value by the target base, 37 2 divisor dividend
computing an integer division, you get a 1 18 2
quotient (result) and a remainder remainder quotient
0 9 2
 Divide the previous quotient by the target 1 4 2
re
base, you get a quotient and a remainder m
ai
nd 0 2 2
Continue until the quotient is zero er

s 0 1 2
 Write (from left to right) the remainders 1 0
from the last computed to the first one 100101

19 20

Conversion from Base n to m Conversion Exercises


 Given a number in base n, to convert it  Convert the values as requested
to base m:  10610 ()2  478 ()10
 convert the number from base n to base 10  3510 ()2  7310 ()8
 convert the just calculated number from  6410 ()2  447 ()3
base 10 to base m  456 ()2  1010012 ()10
 It is always possible to pass through the  177 ()2  111112 ()10
intermediate base 10, but sometimes  238 ()2  100002 ()10
this is not the easiest and fastest way  20710 ()2  100002 ()4
 B216 ()10  16110 ()16
21 22

Conversion Exercises Conversion from Bases 2n


 Convert the values as requested  To convert a number from base n to
 10610 11010102  478 3910 base m, when BOTH n and m are
 3510 1000112  7310 1118 powers of 2 (e.g. 2,4,8,16), it is easier
 6410 10000002  447 10123 and faster to pass through base 2
 456 111012  1010012 4110  Every digit in a 2x base requires at least
 177 IMP.  111112 3110 x bits (possibly starting with zeroes):
 238 100112  100002 1610 58 = 1012 28 = 0102
 20710 110011112  100002 1004 B16 = 10112 216 = 00102
 B216 17810  16110 A116

23 24

Conversion from Bases 2n Conversion from Bases 2n


 A number in base-2x can be converted  A number in base 2 can be converted to
to base 2 by simply substituting each of base-2x by grouping its bits from right
its digits with the corresponding binary to left (each group composed of x bits)
value (each composed of x bits): and substituting each group with the
528 = [101][010]2 = 1010102 corresponding base-2x digit:
B216 = [1011][0010]2 = 101100102 1010102 = [101][010]2 = 528
101100102 = [1011][0010]2 = B216
25 26

Conversion from Bases 2n Conversion from Bases 2n


 If the leftmost group has less than x  Example
bits, an appropriate number of zeroes Convert number 3CB21F16 to base 8
are added to the left part: 3 C B 2 1 F
100102 = [0 10][010]2 = 228 0011 1100 1011 0010 0001 1111
1100102 = [00 11][0010]2 = 3216 0011 1100 1011 0010 0001 1111
1 7 1 3 1 0 3 7

171310378

27 28

Conversion from Bases 2n Exercises on Conversions


 The most important base in Computer  Convert the values as requested
Science is 2, but it is difficult and long to  10010100101001012 ()8
write and read long binary values  10010100101001012 ()H
 Base 16 and 8 are so often used because  33258 ()2
it is simple and immediate to convert a  33258 ()4
value between them and base 2 and  33258 ()16
because of the compact notation:
13348 ()16
it is much easier to read and write a 32

A11616 ()8
bit value like 95DBA6CF instead of 

10010101110110111010011011001111  133648 ()16


29 30

Exercises on Conversions Powers of 2


 Solutions  20=110 =12 29=512
 001|001|010|010|100|1012 1122458  21=210 =102 210=1024
 22=410 =1002 211=2048
 1001|0100|1010|01012 94A5H
 23=810 =10002 212=4096
 33258 0110110101012
 24=1610 =100002 213=8192
 33258 1231114
 25=3210 =1000002 214=16384
 33258 6D516  26=6410 =10000002 215=32768
 13348 2DC16  27=12810 =100000002 216=65536
 A11616 1204268  28=25610 =1000000002
 133648 16F416  Note that 2n in binary is 1 followed by n zeroes

31 32

Range Number of bits required


 With n bits, just a limited subset of  Given a decimal value N, the same
values can be represented value converted to binary requires
 There are 2n different combinations of a minimum of bits equal to:
n bits, each one is a binary number: n  log 2 ( N  1)
from 000...000  0
... ... 2n numbers where the ceiling operator a 
to 111...111  2n –1 returns the minimum integer greater
then or equal to a (e.g. 2.1  3,
Thus the range of a binary number 2.9  3, 2.0  2)
composed of n bits is: 0  2n –1
33 34

Number of bits required Number of bits required


 A simpler approach uses (wise) trial:  Example - How many bits are required
 count the digits (d ) of the decimal num. N for number 400?
 each digit requires about 3 bits (good Estimate:
approximation up to 20-30 bit numbers), so  3 decimal digits  3x3 = 9 bits
the approximate number of bits is: x = 3d
Check values:
 compare N to powers 2n with n ranging
 9 bits  range: 0  29 –1 = 511  400 OK
from (x-1) to (x+1) to find the minimum n
so that 2n  N Verify if a smaller value is also good:
 Note: if needed, consider extending n to  8 bits  range: 0  28 –1 = 255 < 400 NO
(x –2) or to (x +2) Answer: at least 9 bits

35 36

Number of bits required Number of bits required


 Exercises  Solutions
How many bits are needed to represent How many bits are needed to represent
the following values? the following values?
 47  422  47  6  422  9
 137  15  137 8  15  4
 1412  444  1412  11  444  9
 128  1024  128  8  1024  11
 884  1023  884  10  1023  10
 1  6443  1 1  6443  13
37 38

Fractional Numbers Conversion Fractional Numbers Conversion


 Conversion from any base n to base 10  Conversion from base 10 to any base n
just requires the application of the requires 2 steps:
positional numbering system definition  conversion of the integral part (already
 Example seen)
1011.1012  ()10  conversion of the fractional part (to be
seen)
3 2 1 0 -1-2-3
1011.101 = 1x23 + 0x22 + 1x21 + 1x20 The two parts are then juxtaposed
+ 1x2-1 + 0x2-2 + 1x2-3 = (added)
= 8 + 2 + 1 + 0.5 + 0.125 = 11.62510

39 40

Fractional Numbers Conversion Fractional Numbers Conversion


 Conversion of the fractional part  Example, convert 0.687510 to binary
(i.e. a value in the form 0.xxxx) 0.6875 x 2 = 1.3750
 multiply the number to the target base 0.3750 x 2 = 0.750
(e.g. 2) 0.750 x 2 = 1.50
write down the integer part of the result

0.50 x 2 = 1.00
and THEN set it to 0
 repeat until result is 0 or as otherwise 0.00 STOP
required (more on this later) 0.10112
 write left to right “0.” followed by the
integer parts in the order they have been  Example, convert 12.6875 to binary:
calculated Result: 1100.10112
41 42

Fractional Numbers Conversion Fractional Numbers Conversion


 To convert a number with a fractional  Regrouping must start from the point so
part from one base to another, it is that unity remains on the digit at left of
always possible to pass through the the point, if required, zeroes must be
intermediate base 10 added on the right of the fractional part
 When BOTH bases are powers of 2, it  Example x 20
is easier and faster to pass through C4B2.D6H  ()8
base 2 1100 0100 1011 0010.1101 01102
001 100 010 010 110 010 . 110 101 100
1 4 2 2 6 2 . 6 5 48

43 44

Fractional Numbers Conversion Exercises on Conversions


 A value with a finite number of  Convert the values as requested
fractional digits in one base may require  56.228 ()16
an infinite number of fractional digits in  CC559.9B116 ()8
another base (often periodic in binary)  1001.112 ()10
 E.g. 2.31 10.01001100110011001...  11101.0112 ()10
 When converting values with an  1000.00012 ()10
unlimited fractional part, the number of  33.2510 ()2 (3 fractional digits)
fractional digit to calculate must be  13.3410 ()2 (5 fractional digits)
known in advance (in some way)  256.2210 ()2 (6 fractional digits)
45 46

Exercises on Conversions Approximation Errors


 Convert the values as requested  When we have to limit the number of
 56.228 2E.4816 fractional digits, the value resulting
 CC559.9B116 3142531.46618 from conversion is not the same as the
 1001.112 9.7510 original value
 11101.0112 29.37510  This means that if the resulting value is
 1000.00012 8.062510 converted back to the original base, it is
 33.2510 100001.0102 slightly different
 13.3410 1101.010102  An error is introduced
 256.2210 100000000.0011102

47 48

Approximation Errors Approximation Errors


 Absolute precision: the smallest  Examples
(positive) quantity that can be written  In decimal, with 5 fractional digits the
by using a given number of fractional smallest positive quantity is
digits 0.0000110=1/105 = 0.0000110
1
  In binary, with 5 fractional digits the
where: bn smallest positive quantity is
0.000012 =1/25 = 0.0312510
b is the numbering base
In octal, with 5 fractional digits the
n is the number of the fractional digits

smallest positive quantity is


N is the given number 0.000018 =1/85 = 0.00003051757812510
49 50

Approximation Errors Approximation Errors


 0.4 has 1 fractional digit, so its absolute  An absolute precision value may have
precision  is 1/101= 0.1 different significance with respect to the
 10.4 has 1 fractional digit, so its value it is computed for
absolute precision  is 1/101= 0.1  Relative precision: the absolute
 Is =0.1 a good or a bad precision? precision compared to the given value
It depends on the value itself: you have (usually as a percentage)
to compare the absolute error to the 
given value  100%
N

51 52

Approximation Errors Approximation Errors


 Complete  Solutions
N   N  
0.410 1/101=0.1 0.1/0.4*100 = 25% 0.410 0.1 25%
10.410 10.410 0.1 0.96%
0.10112 1/24=0.0625 0.10112 0.0625 9.09%
10010 10010 1 1%
53 54

Approximation Errors Approximation


 In a base conversion, a given error  Example 1 - Convert value N=0.21 to
margin (also called precision or base 2 with o=1/32 (i.e. 0.03125)
approximation) o must not be  Compute how many fractional bits are
exceeded needed: because the required o=1/32
must be equal to the theoretic =1/2n, then
 Errors can be used to establish how 1/32 = 1/2n  32=2n  25=2n  n = 5
many fractional digits to use  Calculate the first 5 fractional bits
 Write the result: 0.001102
 The trailing zero is required: without it the 
would be 1/24 =1/16 and not 1/32

55 56

Approximation Approximation
 Note that 0.001102 = 0.187510 and this  Example 2 - Convert value N=0.21 to
is NOT the given value 0.2110 base 2 with absolute error o=1/100
 However the absolute difference  Compute how many fractional bits are
between them (the introduced error) is needed: because the required o=1/100
less than or equal to the maximum must be equal to the theoretic =1/2n, then
1/100 = 1/2n  100=2n  n = ?
allowed error 1/32 (0.03125):
|0.21 – 0.1875| = 0.0225  0.03125 For solving this equation we can use
logarithms, but we can use the trial method:
n=6 bits  2n = 64 < 100 not enough!
n=7 bits  2n = 128  100 OK!
 Calculate the first 7 fractional bits
57 58

Approximation Exercises on Conversions


 Note that an error of exactly 1/100  Convert the values as requested
cannot be obtained because 100 is not  33.22510 ()2 (o =1/64)
a power of 2  13.3410 ()2 (o =1/1000)
 Instead of 1/100 we use its nearest  256.2210 ()2 (o =0.001%)
(smaller) power of 2, resulting in an  12.7110 ()2 (preserve the same  of
error smaller than the one requested, so the decimal value)
that the requirements are fulfilled
 12.7110 ()2 (preserve the same )
 Having now a power of 2, the exponent
can be easily found

59 60

Exercises on Conversions Exercises on Conversions


 Solutions  Solutions
 33.22510  256.2210
1 1 ε0 ε0
ε  n 2n  64 n =6 η0  100  0.001  100 ε 0  0.0025622
64 2 N 256.22
100001.001110 1 1
ε n  ε  n  0.0025622
 13.3410 2 2
1 1 2  390.3
n

ε  n 2n  1000 n =10 2 n  390.3


1000 2
1101.0101011100 n  9 bit
61 62

Exercises on Conversions BCD Encoding


 Solutions  In many cases, the approximation
 12.7110 involved with conversion to base 2 is
1 1 1 not acceptable
ε 2  ε10  2
  n n =7
10 100 2  The most prominent case is currency
1100.1011010  The only way is to not convert to
 12.7110 binary, but digital computers do need
η10 
ε10
100 η2 
ε2
100 information stored as bits…
12.71 12.71
 What to do?
10= 2  10= 2  same ex. as before

63 64

BCD Encoding BCD Encoding


 Binary-Coded Decimal is an encoding  Example – Convert value 23.19 to BCD
for decimal numbers in which each digit Every decimal digit is converted to the
is represented by its own binary value corresponding 4-bit binary value:
 Each binary value is composed of 4 bits 2 3 . 1 9
 Only 10 groups corresponding to values 0010 0011 . 0001 1001BCD
from 0 to 9 (from 0000 to 1001) are 00102 x 101 + 00112 x 100 +
allowed + 00012 x 10-1 + 10012 x 10-2
 This is NOT an equivalent way to Note the base used is 10, just the
convert to/from base 2! decimal digits are expressed in binary
65 66

BCD Encoding BCD Encoding


 Comparison – Convert value 126.625 to  BCD values are stored in different ways
both base 2 and BCD on different machines:
 1111110.1012  one byte for each digit, the higher nibble
 000100100110.011000100101BCD can be set to:
The two sequences of bits are quite  0000 or 1111
different, the only way to transform one  0011 (in this case, the resulting value is the
ASCII code of the value, e.g. BCD digit 0010, if
into the other is through base 10 stored preceded by 0011 becomes: 00110010
 Other types of BCD encoding exist, the that is the ASCII value for character ‘2’, i.e. 50)
one just seen is called Simple BCD  one byte for two digits (packed BCD )
(SBCD) or BCD 8421  other compressed ways

67 68

BCD Encoding Exercises on BCD


 BCD operations are slower than binary  Convert the values as requested
operations  123.2110 ()BCD
 BCD circuits are bigger  82.C16 ()BCD
 Space is wasted (unused bit sequences)  12.216 ()BCD
000100100110.10010001BCD ()10
 No approximation errors 

 000100100110.10010001BCD ()2
 Easy scaling of a factor of 10
 100100110.100100012 ()BCD
 Rounding at a decimal boundary is easy
69 70

Exercises on BCD Binary Prefixes


 Solutions  The physical quantities use prefixes as
 123.2110  000100100011.00100001BCD multipliers, their values are powers of 10
 82.C16  130.7510   In the binary notation the same prefixes
 000100110000.01110101BCD
are used, but as powers of 2
 12.216  18.12510 
 00011000.000100100101BCD Prefix K M G T P
 000100100110.10010001BCD126.9110 Name kilo mega giga tera peta
 000100100110.10010001BCD1111110.11…2
Physics value 103 106 109 1012 1015
 100100110.100100012 294.566...10 
Binary value 210 220 230 240 250
 001010010100.010101100110BCD

71 72

Binary Prefixes Binary Addition


 An attempt to define separate prefixes  Usual rules apply:
for powers of 2 lead to the definition of  0+0 = 0
the (seldom used) following prefixes  0+1 = 1+0 = 1
 1+1 = 0 with carry = 1 to the following
power of 2 (that is: 102)
Prefix Ki Mi Gi Ti Pi
 1+1+1= 1 with carry = 1 (that is: 112)
Name kibi mebi gibi tebi pebi
 It is useful to add column by column,
Value 210 220 230 240 250 writing carries on top of the next
column
73 74

Binary Addition Exercises on Addition


 Example  Complete:
 0+1=
1111 non null carries
10110 +  1+1=
1011 =  10+1=
100001  11+1=
 100+1=
 101+1=
 111+1=
 1000+1=
 11111+1=

75 76

Exercises on Addition Binary Subtraction


 Solution:  Usual rules apply:
 0+1=1  0–0 = 0
 1+1=10  1–0 = 1
 10+1=11  1–1 = 0
 11+1=100
 0–1= 1 with borrow = 1 (borrowed from
 100+1=101 the nearest 1 leftmost)
 101+1=110
Remember that the 1 that gives its value
 111+1=1000 becomes 0 and any intermediate 0
 1000+1=1001 becomes 1 (the highest digit in base 2)
 11111+1=100000
77 78

Binary Subtraction Exercises on Add & Sub


 Examples  Complete:
01
1011 – The nearest leftmost 1 gives  1010 + 10010=
110 = its value and becomes 0  11 + 11=
The borrowing 0 becomes 10 11011 + 1001=
0101 

 1101 + 111=
0 1 1 11 Intermediate 0s becomes 1s  10000 – 10=
110000 – Remember, in base 10 (the
highest digit is 9) we have:
 11010 – 10101=
11 = 0 9 91
1000 –  10010 – 1111=
101101
1=  10101 – 10101=
0999  10000 – 111=

79 80

Exercises on Add & Sub Overflow


 Solutions :  The binary numbering system we used
 1010 + 10010 = 11100 until now does not take into account
 11 + 11 = 110 any limitation to the number of bits that
 11011 + 1001 = 100100 can be used
1101 + 111 = 10100

 When binary numbers are stored in a
10000 – 10 = 1110
digital computer, the number of bits

11010 – 10101 = 101


available is an architectural, fixed, and

 10010 – 1111 = 11
limiting characteristic
 10101 – 10101 = 0
 10000 – 111 = 1001
81 82

Overflow Overflow
 It is not possible to store a number that  Example
requires more bits than those provided Consider a computing machine where
by the hardware in use (it is out of numbers are stored in 8-bit variables
range) 10011001 +
11001100 =
 When a non-storable number results
101100101
from a calculation (e.g. an addition), it
Note that the result requires 9 bits, the
is not a correct value (must be machine cannot store it and then
discarded) and there is an Overflow signals an Overflow error condition
error condition

83 84

Shift Operations Shift Operations


 Simple multiplication and division by a  Left shift («): a zero is added to the
power of 2 is achieved by shifting the right
number bits 10102  1010
 “Shifting” means moving each bit either 101002  2010
1010002  4010
to the right (right shift) or to the left
(left shift)  Each left shift doubles the value,
 Symbols « and » are used to identify (actually it multiplies the value by the
the shift operation, they are followed by base, in base 10: 12«1 = 120)
the number of shifts to perform  n left shifts  multiplication by 2n
85 86

Shift Operations Exercises on Shifts


 Right shift (»): for integer values, the  Calculates the following op. by using
LSB is discarded, for fractional values shifts on the binary integer notation
the LSB goes beyond the radix point  124 / 8 e.g. 1111100»3  1111
10102  1010  22 * 4
1012  510
128 * 16
102  210 (10.12=2.510 for fract. val.)

131 / 2
Each right shift halves the value, for


integer values it is an integer division  28 * 8
with truncation of the fractional part  47 * 2
 n right shifts  division by 2n  12 / 16

87

Exercises on Shifts
 Solutions

 124 / 8 1111100»3  1111


 22 * 4 10110«2  1011000
 128 * 16 10000000«4  100000000000
 131 / 2 10000011»1  1000001
 28 * 8 11100«3  11100000
 47 * 2 101111«1  1011110
 12 / 16 1100»4  0

You might also like