Understanding Computers: Today and Tomorrow, 13th Edition Understanding Computers: Today and Tomorrow, 13th Edition Numbering systems
Understanding Computers: Today and Tomorrow, 13th Edition
Understanding Computers: Today and Tomorrow, 13th Edition
Numbering systems
Understanding Computers: Today and Tomorrow, 13th Edition2
Learning Objectives
Understand and identify a few of the coding systems .
Understanding Computers: Today and Tomorrow, 13th Edition3
Data and Program Representation
• Digital computers: Can only understand two states, off and on (0 and 1)
• Digital data representation: The process of representing data in digital form so it can be understood by a computer
Understanding Computers: Today and Tomorrow, 13th Edition4
Digital Data Representation
• Bit: The smallest unit of data that a binary computer can recognize (a single 1 or 0)
• Byte = 8 bits , Nibble =4 bits
• Byte terminology used to express the size of documents and other files, programs, etc.
• Prefixes are often used to express larger quantities of bytes: kilobyte (KB), megabyte (MB), gigabyte (GB), terabyte (TB), etc.
Understanding Computers: Today and Tomorrow, 13th Edition
Quiz
I have a 2 Gb Flash memory. How many 512Kb files can I store on the Flash memory?
Answer
Convert 2Gb to Kb
2x1024=2048Mb
Convert from Mb to Kb
2048x1024= 2097152kb
Number of files= 2097152/ 512 = 4096 file
5
Note Mb is in common use for Megabit e.g. 200Megabit = 200Mb
MB is in common use for Megabyte , e.g. 200MegaByte=200MB
Understanding Computers: Today and Tomorrow, 13th Edition
Numbering Systems
• A numbering system is a way of representing numbers.
• A number is represented as a string of digits, e.g., a number N with n digits represented by sequence dn-1, …, d1,d0
Understanding Computers: Today and Tomorrow, 13th Edition
Numbering Systems
• Positional notation– A system of expressing numbers in which the
digits are arranged in succession, the position of each digit has a place value, and the number is equal to the sum of the products of each digit by its place value .
• Determination of a number require the following :– The digits or symbols;– The positional of the digit in the number;– The base of the number system.
Understanding Computers: Today and Tomorrow, 13th Edition
Numbering Systems
• Base ( radix ) – The base of a number system is simply the number of
different digits, including zero that exist in the number system.
– A number system of radix r, typically has a set of r allowed digits belongs to {0,1,…, r-1}
• We write a number N in the number system of radix r by expressing it in the form (N)r or Nr – (12)10 1 and 2 are digits in base 10,
325 2 and 3 are digits in base 5 i.e., belongs to {0,1,..,4}
Understanding Computers: Today and Tomorrow, 13th Edition
Numbering Systems
• a radix point is the symbol used in numerical representations to separate the integer part of a number (to the left of the radix point) from its fractional part (to the right of the radix point). – Ex. 13.62510,
• 13 is the integer to the left of the radix point, and
• 625 (i.e. 625/1000) is the fractional part to the right.
Understanding Computers: Today and Tomorrow, 13th Edition
Understanding Decimal Numbers
• The numbering system we commonly use called the decimal numbering system because it uses 10 digits - symbols - (0,1,2,…,9) to represent all possible numbers.
• Why do we use 10 digits, anyway?
• Numbers greater than nine such as 7216 represented using combination of these 10 digits.
Understanding Computers: Today and Tomorrow, 13th Edition
Understanding Decimal Numbers
• Decimal numbers are made of decimal digits: (0,1,2,3,4,5,6,7,8,9)
• But how many items does a decimal number represent?– 8653 = 8x103 + 6x102 + 5x101 + 3x100
• What about fractions?– 97654.35 = 9x104 + 7x103 + 6x102 + 5x101 + 4x100 +
3x10-1 + 5x10-2
– In formal notation -> (97654.35)10
Understanding Computers: Today and Tomorrow, 13th Edition
Understanding Octal Numbers
• Octal numbers are made of octal digits: (0,1,2,3,4,5,6,7)• How many items does an octal number represent?
– (4536)8 = 4x83 + 5x82 + 3x81 + 6x80 = (1362)10
• What about fractions?– (465.27)8 = 4x82 + 6x81 + 5x80 + 2x8-1 + 7x8-2
• Octal numbers don’t use digits 8 or 9
Understanding Computers: Today and Tomorrow, 13th Edition
Understanding Binary Numbers
• Binary numbers are made of binary digits (bits): – 0 and 1
• How many items does an binary number represent?– (1011)2 = 1x23 + 0x22 + 1x21 + 1x20 = (11)10
• What about fractions?– (110.10)2 = 1x22 + 1x21 + 0x20 + 1x2-1 + 0x2-2
Understanding Computers: Today and Tomorrow, 13th Edition
Understanding Hexadecimal Numbers
• Hexadecimal numbers are made of 16 digits: – (0,1,2,3,4,5,6,7,8,9,A, B, C, D, E, F)
• How many items does an hex number represent?– (3A9F)16 = 3x163 + 10x162 + 9x161 + 15x160 = 1499910
• What about fractions?– (2D3.5)16 = 2x162 + 13x161 + 3x160 + 5x16-1 = 723.312510
• Note that each hexadecimal digit can be represented with four bits.– (1110) 2 = (E)16
• Groups of four bits are called a nibble.– (1110) 2
Understanding Computers: Today and Tomorrow, 13th Edition
Representing Numerical Data
• Least Significant Digit (LSD) is the right most digit has the lowest value
• Most Significant Digit (MSD) is the left most digit has the highest value
• The most significant symbol can not be zeroExample:
Show how the value of the number (9375)10 is estimated
0 1 2 3 position
100 101 102 103 Weight
5 7 3 9 Digit
9375 5x1 7x10 3x100 9x1000 Value
5 is the least significant digit (LSD)9 is the most significant digit (MSD)
Understanding Computers: Today and Tomorrow, 13th Edition
Representing Numerical Data
Commonly used numbering systemsA. Decimal number system
base =10, Ten allowed digits{0,1,…,9}
B. Binary number system
base= 2, Two allowed digits {0,1}
A. A Binary digit is referred to as Bit
B. Most significant Bit (MSB) is the leftmost bit has the highest value
C. Least significant Bit (LSB) is the rightmost bit has the lowest value
C. Octal number system
base =8, Eight allowed digits {0,1,2,…,7}
D. Hexadecimal number system
base = 16, Sixteen allowed digits {0,1,…,9, A,B,C,D,E,F}
Where, A= ten , B= eleven, C=Twelve, D= thirteen, E=fourteen, F= fifteen
Q. Why is the digit following 9 assigned the character A not 10 ?
Understanding Computers: Today and Tomorrow, 13th Edition
Check point
Q. What is the largest value can be represented in three integral digits?-------------------------------------------------------------
For the decimal system, it is (- - - )10
For the octal system, it is (- - - )8
For the hex system, it is (- - - )16
For the binary system, it is (- - - )2
-------------------------------------------------------------Hint (The largest value results when all 3 positions are filled with the largest
digit in the number system)
Answer • For the decimal system, it is ( 999 )10
• For the octal system, it is ( 777 )8
• For the hex system, it is ( fff )16
• For the binary system, it is ( 111 )2
Understanding Computers: Today and Tomorrow, 13th Edition
Conversion Between Number Bases
Hexadecimal
(base16)
Decimal(base 10)
Octal(base 8)
Binary(base 2)
° Learn to convert between bases.
Understanding Computers: Today and Tomorrow, 13th Edition
Representing Numerical Data
Converting between numbering systems
1. Converting from decimal to any base
2. Converting from any base to decimal
3. Converting from any base to any base
4. A special conversion case number bases that are related (Shortcut method to/from binary )
Understanding Computers: Today and Tomorrow, 13th Edition
Convert an Integer from Decimal to Another Base
1. Divide decimal number by the base (e.g. 2)
2. The remainder is the lowest-order digit
3. Repeat first two steps until no divisor remains.
For each digit position:
Example for (13)10:
IntegerQuotient
13/2 = 6 + ½ a0 = 1 6/2 = 3 + 0 a1 = 0 3/2 = 1 + ½ a2 = 1 1/2 = 0 + ½ a3 = 1
Remainder Coefficient
Answer (13)10 = (a3 a2 a1 a0)2 = (1101)2
LSB
MSB
Understanding Computers: Today and Tomorrow, 13th Edition
Repeated Division-by-r
To convert a whole number to the new base r, use successive division by r until the quotient is 0. The remainders form the answer, with the first remainder as LSB or LSD and the last as MSB or MSD.
Example: (43)10 = (?)2 2 432 21 rem 1 LSB2 10 rem 12 5 rem 02 2 rem 12 1 rem 0
0 rem 1 MSB
(43)10 = (101011)2
Understanding Computers: Today and Tomorrow, 13th Edition
Convert an Fraction from Decimal to Another Base
For each digit position:
1. Multiply decimal number by the base (e.g. 2)
2. The integer is the highest-order digit
3. Repeat first two steps until fraction becomes zero.
Example for (0.625)10:
Integer
0.625 x 2 = 1 + 0.25 a-1 = 10.250 x 2 = 0 + 0.50 a-2 = 00.500 x 2 = 1 + 0 a-3 = 1
Fraction Coefficient
Answer (0.625)10 = (0.a-1 a-2 a-3 )2 = (0.101)2
MSB
LSB
Understanding Computers: Today and Tomorrow, 13th Edition
Repeated Multiplication-by-r
To convert decimal fractions to binary, repeated multiplication by r is used, until the fractional product is 0 (or until the desired number of digits of accuracy obtained). The carried digits, or carries, produce the answer, with the first carry as the MSB or MSD, and the last as the LSB or MSD.
Example: (0.3125)10 = (.0101)2
Carry
0.31252=0.625 0 MSB
0.6252=1.25 1
0.252=0.50 0
0.52=1.00 1 LSB
Understanding Computers: Today and Tomorrow, 13th Edition
Note
For a number that has both integral and fractional parts,
• conversion is done separately for both parts, and
• then the result is put together with a system point in between both parts.
Understanding Computers: Today and Tomorrow, 13th Edition
2-Any base r-to-Decimal Conversion
• Base-r to decimal: multiply digits with their corresponding weights.
• Examples (1101.101)2 = 123 + 122 + 120 + 12-1 + 12-3 =
8 + 4 + 1 + 0.5 + 0.125
= (13.625)10
(572.6)8 = 582 + 781 + 280 + 68-1
= 320 + 56 + 2 + 0.75 = (378.75)10
Convert 3b216 to decimal.
N = 3*162 + 11*161 + 2*160 = 94610
Convert 3708 to decimal.
N = 3*82 + 7*81 + 0*80 = 192 + 56 + 0 = 24810
Understanding Computers: Today and Tomorrow, 13th Edition
3-Conversion between Bases
In general, conversion between bases can be done via decimal:
First convert given number to decimal then convert decimal number to the new base.
Base-2 Base-2
Base-3 Base-3
Base-4 Decimal Base-4
… ….
Base-R Base-R
Understanding Computers: Today and Tomorrow, 13th Edition
Converting Between Base 16 and Base 2
° Conversion is easy!
Determine 4-bit value for each hex digit
° Note that there are 24 = 16 different values of four bits
° Easier to read and write in hexadecimal.
° Representations are equivalent!
3A9F16 = 0011 1010 1001 11112
3 A 9 F
Understanding Computers: Today and Tomorrow, 13th Edition
Converting Between Base 16 and Base 8
1. Convert from Base 16 to Base 2
2. Regroup bits into groups of three starting from right
3. Ignore leading zeros
4. Each group of three bits forms an octal digit.
352378 = 011 101 010 011 1112
5 2 3 73
3A9F16 = 0011 1010 1001 11112
3 A 9 F
Understanding Computers: Today and Tomorrow, 13th Edition
A special conversion case number bases that are related (Shortcut method to/from binary )
• Grouping of several digits in the smaller number base corresponds exactly to single digit in the larger number base
• Examples Binary Octal: Partition in groups of 3
(10 111 011 001 . 101 110)2 = (2731.56)8
Octal Binary: reverse
(2731.56)8 = (10 111 011 001 . 101 110)2
Binary Hexadecimal: Partition in groups of 4
(101 1101 1001 . 1011 1000)2 = (5D9.B8)16
Hexadecimal Binary: reverse
(5D9.B8)16 = (101 1101 1001 . 1011 1000)2
Understanding Computers: Today and Tomorrow, 13th Edition
Arithmetic operation on numbering systems
• Addition• Subtraction• Multiplication• division
Understanding Computers: Today and Tomorrow, 13th Edition
Decimal Addition Example
3 7 5 8
+ 4 6 5 7
1) Add 8 + 7 = 15Write down 5, carry 1
1
8
1 1
4 1 5 4) Add 3 + 4 + 1 = 8Write down 8
3) Add 7 + 6 + 1 = 14Write down 4, carry 1
2) Add 5 + 5 + 1 = 11Write down 1, carry 1
Add 3758 to 4657:
33
Understanding Computers: Today and Tomorrow, 13th Edition
Decimal Addition Explanation
1 1 1
3 7 5 8
+ 4 6 5 7
8 4 1 5
What just happened?
1 1 1 (carry)
3 7 5 8
+ 4 6 5 7 8 14 11 15 (sum)
- 10 10 10 (subtract the base)
8 4 1 5
So when the sum of a column is equal to or greater than the base, we subtract the base from the sum, record the difference, and carry one to the next column to the left.
34
Understanding Computers: Today and Tomorrow, 13th Edition
Binary Addition Rules
Rules:– 0 + 0 = 0– 0 + 1 = 1– 1 + 0 = 1 (just like in decimal)
1 + 1 = 210
= 102 = 0 with 1 to carry
1 + 1 + 1 = 310
= 112 = 1 with 1 to carry
35
Understanding Computers: Today and Tomorrow, 13th Edition
Binary Addition Example 1
1 1 0 1 1 1 + 0 1 1 1 0 0
1
1
111
0 1 0 0 11
Example 1: Add binary 110111 to 11100
Col 1) Add 1 + 0 = 1 Write 1
Col 2) Add 1 + 0 = 1 Write 1
Col 3) Add 1 + 1 = 2 (10 in binary) Write 0, carry 1
Col 4) Add 1+ 0 + 1 = 2 Write 0, carry 1
Col 6) Add 1 + 1 + 0 = 2 Write 0, carry 1
Col 5) Add 1 + 1 + 1 = 3 (11 in binary) Write 1, carry 1
Col 7) Bring down the carried 1 Write 1
36
Understanding Computers: Today and Tomorrow, 13th Edition
Binary Addition Explanation
1 1 0 1 1 1 + 0 1 1 1 0 0 - .
1
1
111
0 1 0 0 11
In the first two columns, there were no carries.
In column 3, we add 1 + 1 = 2 Since 2 is equal to the base, subtract
the base from the sum and carry 1.
In column 4, we also subtract the base from the sum and carry 1.
In column 6, we also subtract the base from the sum and carry 1.
In column 5, we also subtract the base from the sum and carry 1.
In column 7, we just bring down the carried 1
22 2 23
222
What is actually happened when we carried in binary?
37
Understanding Computers: Today and Tomorrow, 13th Edition
Binary Addition Verification
Verification
1101112 5510
+0111002 + 2810
8310
64 32 16 8 4 2 1
1 0 1 0 0 1 1
= 64 + 16 + 2 +1
= 8310
1 1 0 1 1 1 + 0 1 1 1 0 0 1 0 1 0 0 11
You can always check your answer by converting the figures to decimal, doing the addition, and comparing the answers.
38
Understanding Computers: Today and Tomorrow, 13th Edition
Binary Addition Example 2
Verification
1110102 5810
+0011112 + 1510
7310
64 32 16 8 4 2 1
1 0 0 1 0 0 1
= 64 + 8 +1
= 7310
1 1 1 0 1 0 + 0 0 1 1 1 1
1
1
11
0 0 1 0 10
Example 2:Add 1111 to 111010.
11
39
Understanding Computers: Today and Tomorrow, 13th Edition
Decimal Subtraction Example (Borrow method)
8 0 2 5- 4 6 5 7
Subtract 4657 from 8025:
7 9 11
11
8633
1) Try to subtract 5 – 7 can’t.Must borrow 10 from next column.
4) Subtract 7 – 4 = 3
3) Subtract 9 – 6 = 3
2) Try to subtract 1 – 5 can’t. Must borrow 10 from next column.
But next column is 0, so must go to column after next to borrow.
Add the borrowed 10 to the original 0. Now you can borrow 10 from this column.
Add the borrowed 10 to the original 5.Then subtract 15 – 7 = 8.
Add the borrowed 10 to the original 1..Then subract 11 – 5 = 6
40
Understanding Computers: Today and Tomorrow, 13th Edition
Decimal Subtraction Explanation
So when you cannot subtract, you borrow from the column to the left. The amount borrowed is 1 base unit, which in decimal is 10. The 10 is added to the original column value, so you will be able to
subtract.
8633
8 0 2 5- 4 6 5 7
41
Understanding Computers: Today and Tomorrow, 13th Edition
Binary Subtraction Example 1
1 1 0 0 1 1- 1 1 1 0 0
Example 1: Subtractbinary 11100 from 110011
20 0 2
12
1101
Col 1) Subtract 1 – 0 = 1
Col 5) Try to subtract 0 – 1 can’t. Must borrow from next column.
Col 4) Subtract 1 – 1 = 0
Col 3) Try to subtract 0 – 1 can’t. Must borrow 2 from next column.
But next column is 0, so must go to column after next to borrow.
Add the borrowed 2 to the 0 on the right. Now you can borrow from this column
(leaving 1 remaining).
Col 2) Subtract 1 – 0 = 1
Add the borrowed 2 to the original 0.Then subtract 2 – 1 = 1
1
Add the borrowed 2 to the remaining 0.Then subtract 2 – 1 = 1
Col 6) Remaining leading 0 can be ignored.
42
Understanding Computers: Today and Tomorrow, 13th Edition
Binary Subtraction Verification
Verification
1100112 5110
- 111002 - 2810
2310
64 32 16 8 4 2 1
1 0 1 1 1
= 16 + 4 + 2 + 1
= 2310
1 1 0 0 1 1- 1 1 1 0 0
20 0 2
12
1101 1
Subtract binary 11100 from 110011:
43
Understanding Computers: Today and Tomorrow, 13th Edition
• Note
X = (1151)10 , y = (1102)10 which one is greater than the other ?
Ans. Compare each digit from left to right
digit 1 in x = digit 1 in y goto the next digit
digit 2 in x = digit 2 in y goto the next digit
digit 3 in x > digit 3 in y stop x greater than y
Quiz: (110101)2 , (11001)2 which one is greater than the other ?
44
Understanding Computers: Today and Tomorrow, 13th Edition
Binary multiplication
• Binary multiplication uses the same technique as decimal multiplication.
• Example: multiplying 1102 by 102.
110x 10--------- 000+ 110 --------1100
6x 2------12
45
Understanding Computers: Today and Tomorrow, 13th Edition
Division
• Division rule– Set quotient to zero – Repeat while dividend is greater than or equal to
divisor • Subtract divisor from dividend • Add 1 to quotient
– End of repeat block – quotient is correct, dividend is remainder – STOP
46
Understanding Computers: Today and Tomorrow, 13th Edition
Binary division
• Division as repeated subtractionExample in decimal 84 ÷ 21 = ??
I subtracted 21 four times, so 84 ÷ 21 = 4
84− 21------ 63− 21------- 42− 21 ------ 21− 21------- 0
1
1
1
1
----4
47
Understanding Computers: Today and Tomorrow, 13th Edition
• Example: Find 1100 ÷ 0101 • Ans.
1100 - 0101 = 111
111-101= 010
Hence result is 2 and with remainder 2 ( (10)2 )
48
quotient 0
1
1-----2
Understanding Computers: Today and Tomorrow, 13th Edition
ONE’S COMPLEMENT AND TWO’S COMPLEMENT REPRESENTATIONS
Understanding Computers: Today and Tomorrow, 13th Edition
One’s Complement Representation
• The one’s complement of a binary number involves inverting all bits.
• 1’s comp of 00110011 is 11001100
• 1’s comp of 10101010 is 01010101
Understanding Computers: Today and Tomorrow, 13th Edition
Two’s Complement
• Algorithm 1 – Simply complement each bit and then add 1 to the result.– Finding the 2’s complement of (01100101)2 and of
its 2’s complement…
N = 01100101 [N] = 10011011
10011010 01100100
+ 1 + 1
--------------- ---------------
10011011 01100101
Understanding Computers: Today and Tomorrow, 13th Edition
Two’s Complement Representation
Algorithm 2 – Starting with the least significant bit, copy all of the bits up to and including the first 1 bit and then complementing the remaining bits.
N = 0 1 1 0 0 1 0 1[N] = 1 0 0 1 1 0 1 1
Understanding Computers: Today and Tomorrow, 13th Edition
Logical OR Rules
OR Operations
• OR Results in 1 if either or both of the operands are 1.
• OR Table
0 OR 0 = 0
0 OR 1 = 11 OR 0 = 11 OR 1 = 1
54
Understanding Computers: Today and Tomorrow, 13th Edition
Logical OR Operation
To perform the OR operation, take one column at a time and perform the OR operation using the OR table.
Ex 1: 1 0 0 1 0 0 1 1
OR 0 0 0 0 1 1 1 1
1 0 0 1 1 1 1 1
55
Understanding Computers: Today and Tomorrow, 13th Edition
Logical XOR Rules
XOR Operations • The exclusive OR. Similar to OR except that
it now gives 0 when both its operands are 1.
Rules.0 XOR 0 = 00 XOR 1 = 11 XOR 0 = 11 XOR 1 = 0
56
Understanding Computers: Today and Tomorrow, 13th Edition
Logical XOR Examples
Ex 1: 1 0 0 1 1 0 0 1
XOR 0 0 0 0 1 1 1 1
1 0 0 1 0 1 1 0
Ex 2: 0 1 1 1
XOR 0 0 1 0
0 1 0 1
57
Understanding Computers: Today and Tomorrow, 13th Edition
Logical AND Rules
AND Operations
• AND yields 1 only if both its operands are 1.
Rules.0 AND 0 = 00 AND 1 = 01 AND 0 = 01 AND 1 = 1
58
Understanding Computers: Today and Tomorrow, 13th Edition
Logical AND Examples
Ex 1: 1 1 0 1 0 0 1 1
AND 0 0 0 0 1 1 1 1 0 0 0 0 0 0 1 1
Ex 2: 0 1 1 1 AND 1 0 0 1 0 0 0 1
59
Understanding Computers: Today and Tomorrow, 13th Edition
Logical NOT
NOT Operations
• NOT is a separate operator for flipping the bits.
Rules.NOT 0 = 1NOT 1 = 0
Example. NOT 1 0 1 0 = 0 1 0 1
60