Download Lecture 1 – Introduction, Numbers and Number System

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Ethnomathematics wikipedia , lookup

Mathematics of radio engineering wikipedia , lookup

Georg Cantor's first set theory article wikipedia , lookup

Infinity wikipedia , lookup

Infinitesimal wikipedia , lookup

History of logarithms wikipedia , lookup

Approximations of π wikipedia , lookup

Large numbers wikipedia , lookup

Real number wikipedia , lookup

Location arithmetic wikipedia , lookup

Arithmetic wikipedia , lookup

Addition wikipedia , lookup

Positional notation wikipedia , lookup

Elementary mathematics wikipedia , lookup

Transcript
Lecture 1 – Introduction, Numbers and Number System
1.1. Contents
Number Systems (Appendix B) .................................................................................................................................. 2 1. Example 1.1 – Converting to Base 10 ......................................................................................................................... 2 1.2. Number Representation ............................................................................................................................................... 2 1.3. Number Conversion .................................................................................................................................................... 3 1. To convert a number N expressed in base 10 to base B .............................................................................................. 3 2. Example 1.2 – Converting Decimal Number to base 8 ............................................................................................... 3 3. Example 1.3 – Converting Decimal Number to base 8 ............................................................................................... 3 4. Example 1.4 – Converting Decimal Number to base 16 ............................................................................................. 3 5. Example 1.5 – Converting Decimal Number to base 2 ............................................................................................... 3 6. Binary-to-hexadecimal (4 bits = 1 hex digit) .............................................................................................................. 4 7. Binary-to-octal (3 bits = 1 octal digit) ......................................................................................................................... 4 8. Hexadecimal-to-binary ................................................................................................................................................ 4 9. Octal-to-binary ............................................................................................................................................................ 4 10. Example 1.6 – Converting Number ........................................................................................................................ 4 1.4. Signed Binary Number ................................................................................................................................................ 4 1.5. Two’s Complement Numbers ..................................................................................................................................... 4 1. Example 1.7 – Two’s Complement Numbers ............................................................................................................. 5 2. Example 1.8 – Two’s Complement Numbers ............................................................................................................. 5 3. Find Decimal Value of Signed Binary Number .......................................................................................................... 5 1.6. Negating two’s Complement Numbers ....................................................................................................................... 6 1. Example 1.9 – Negating in Two’s Complement System............................................................................................. 6 2. Negating Octal and Hexadecimal Numbers ................................................................................................................ 6 a. Example 1.10 – Negating Hexadecimal Numbers in Two’s Complement System ................................................. 7 1.7. Addition in Two’s Complement System ..................................................................................................................... 7 1.8. Subtraction in Two’s Complement System ................................................................................................................. 7 1.9. Examples on Two’s Complements .............................................................................................................................. 8 1. Example 1.11 – Addition and Subtraction in Two’s Complement Systems................................................................ 8 Lecture 1 – Introduction, Numbers, and Number System
Page 1 of 8
1.1.
Number Systems (Appendix B)
Representation of an integer number on base B is done by creating a set of coefficients an …
a1a0 with weights specified by powers of the base B. The coefficients in base B are numbers
that can take values 0 through B-1. A number N can be written in base B as
N   an ...a2a1a0  B  an B n  ...  a2 B 2  a1B1  a0 B 0
Fractional numbers can be written by introducing negative powers of the base. A fractional
number N can be written in base B as
N   am ...a2a1a0 .a1a2a3 ...a n  B
 am B m  ...  a2 B 2  a1B1  a0 B 0  a1B 1  a2 B 2  a3 B 3  ...  a n B  n
There are four common number systems: decimal, hexadecimal, octal, and binary.
1. Example 1.1 – Converting to Base 10
Find the equivalent numbers in the decimal base system of the following numbers:
 (1204.12)10 = 1204.12
 (1204.12)8
= 1*83 + 2*82 + 0*81 + 4*80 + 1*8-1 + 2*8-2 = 644.15625
 (1A2F.11)16 = 1*163 + 10*162 + 2*161 + 15*160 + 1*16-1 + 1* 16-2
= 6703.06640
 (10000010.10)2= 1*27 + 0*26 + 0*25 + 0*24 + 0*23 + 0*22 + 1*21 + 0*20 + 1*2-1 +0*2-2
= 130.5
1.2.
Number Representation
Decimal
0.00
1.00
2.00
3.00
4.00
5.00
6.00
7.00
8.00
9.00
10.00
11.00
12.00
13.00
14.00
15.00
16.00
17.00
18.00
19.00
20.00
Binary
00000000
00000001
00000010
00000011
00000100
00000101
00000110
00000111
00001000
00001001
00001010
00001011
00001100
00001101
00001110
00001111
00010000
00010001
00010010
00010011
00010100
Lecture 1 – Introduction, Numbers, and Number System
Octal
00
01
02
03
04
05
06
07
10
11
12
13
14
15
16
17
20
21
22
23
24
Page 2 of 8
Hex
00
01
02
03
04
05
06
07
08
09
0A
0B
0C
0D
0E
0F
10
11
12
13
14
1.3.
Number Conversion
1. To convert a number N expressed in base 10 to base B
 Separate integer part of number from fractional part
 Divide integer part by B continuously and save the remainder at each step.
 Multiply fractional part by B continuously and save integer part at each step.
N   a2a1a0 .a1a2  B  a2 B 2  a1B1  a0 B 0  a1B 1  a2 B 2
2. Example 1.2 – Converting Decimal Number to base 8
Converting the following decimal numbers into base 8: (75.32)10
Remainder
Fraction
8  75
3
0.32 * 8
8 9
1
0.56 * 8
8 1
1
0.48 * 8
0.84 * 8
0.72 * 8
(75.32)10 ≈ (113.24365)8
Result
2.56
4.48
3.84
6.72
5.76
3. Example 1.3 – Converting Decimal Number to base 8
Converting the following decimal number into base 8: (256.347)10
Remainder
Fraction
8  256
0
0.347 * 8
8  32
0
0.776 * 8
84
4
0.208 * 8
0.664 * 8
0.312 * 8
0.496 * 8
(256.347)10 ≈ (400.261523)8
Result
2.776
6.208
1.664
5.312
2.496
3.968
4. Example 1.4 – Converting Decimal Number to base 16
Converting the following decimal number into base 6: (420.625)10
Remainder
Fraction
16  420
4
0. 625 * 16
16  26
A
16  1
1
(420.625)10 ≈ (1A4.A)16
5. Example 1.5 – Converting Decimal Number to base 2
Converting the following decimal number into base 6: (53.375)10
Remainder
Fraction
0.375 * 2
1
2  53
0.75 * 2
0
2  26
0.50 * 2
1
2  13
0
26
1
23
1
21
(53.375)10 ≈ (110101.011)2
Lecture 1 – Introduction, Numbers, and Number System
Page 3 of 8
Result
10.0
Result
0.75
1.50
1.00
6. Binary-to-hexadecimal (4 bits = 1 hex digit)
Group binary digits into groups of four and assign each group a hex digit.
1011
1110
0101
0011
B
E
5
3
7. Binary-to-octal (3 bits = 1 octal digit)
101
111
010
5
7
2
001
1
8. Hexadecimal-to-binary
C
2
1100
0010
8
1000
E
1110
9. Octal-to-binary
5
101
2
010
1
001
6
110
10. Example 1.6 – Converting Number
 (5CB.2F)16
 (147.62)8
 (1101 1110 1010. 1010 1001)2
 (101 110 010.101 001 011 100)2
1.4.
= (0101 1100 1011.0010 1111)2
= (001 100 111.110 010)2
= (DEA.A9)16
= (562.5134)8
Signed Binary Number
For signed numbers, in the binary system, the sign of the number is denoted by the left-most
bit:
0 = positive
1 = negative
For a n-bit number, the remaining n-1 bits represent the magnitude.
For signed binary system, there is a duplicate representation of 0 such as +0 (0000 0000) and –
0 (1000 0000). As a result, there is one less combination of bits to represent signed numbers.
1.5.
Two’s Complement Numbers
All modern computers use the two’s complement number system to represent negative
numbers.
 1’s Complement of N: complement individual bits (0 1 and 1  0)
 2’s Complement of N: complement individual bit and add 1 to it.
 In the two’s complement system, positive numbers are identical to binary numbers
with the sign bit is 0.
Lecture 1 – Introduction, Numbers, and Number System
Page 4 of 8

Range of number in a system of n bits:
 Unsigned number:
0 ≤ N ≤ 2n – 1
 1’s complement:
–2n – 1 + 1 ≤ N ≤ 2n – 1 – 1
 2’s complement:
–2n – 1
≤ N ≤ 2n – 1 – 1
 The 2’s complement of N is used to represent –N. It can represent integers in the range
between –2n-1 to 2n-1-1, where n is the number of bits available for representing N (for
unsigned numbers N, the range would be 0 to 2n-1).
1. Example 1.7 – Two’s Complement Numbers
Find the two’s complement of the following numbers in 8-bit system: –10, –8, –4, +7, and
–14.
8-bit
1’s Compl.
+1
2’s Compl.
–10
+10
00001010
11110101
11110110
–8
+8
00001000
11110111
11111000
–4
+4
00000100
11111011
11111100
+7
+7
00000111
00000111
–14
+14
00001110
11110001
11110010
2. Example 1.8 – Two’s Complement Numbers
Find the equivalent decimal numbers of the following number (2’s complement) in 6-bit
system:
6-bit
1’s Compl.
+1
Magnitude
Numbers
110110
001001
001010
–10
101110
010001
010010
–18
001011
001011
+11
110101
001010
001011
–11
111010
000101
000110
–6
3. Find Decimal Value of Signed Binary Number
Observe that the 2’s complement of a negative number is the corresponding positive
number. For example,
-510=10112 and 2’s complement of 10112 is 01012
Procedure
1) Find the 2’s complement of the number, i.e. its positive value
2) Convert the positive binary number to decimal
3) Add a negative sign to the decimal value
Two’s Complement Numbers in 6-bit system
+15
+14
+13
+12
+11
+10
Signed System
001111
001110
001101
001100
001011
001010
Lecture 1 – Introduction, Numbers, and Number System
1’s Complement
001111
001110
001101
001100
001011
001010
Page 5 of 8
2’s Complement
001111
001110
001101
001100
001011
001010
+9
+8
+7
+6
+5
+4
+3
+2
+1
0
–1
–2
–3
–4
–5
–6
–7
–8
–9
–10
–11
–12
–13
–14
–15
1.6.
001001
001000
000111
000110
000101
000100
000011
000010
000001
000000
100001
100010
100011
100100
100101
100110
100111
101000
101001
101010
101011
101100
101101
101110
101111
001001
001000
000111
000110
000101
000100
000011
000010
000001
000000
111110
111101
111100
111011
111010
111001
111000
110111
110110
110101
110100
110011
110010
110001
110000
001001
001000
000111
000110
000101
000100
000011
000010
000001
000000
111111
111110
111101
111100
111011
111010
111001
111000
110111
110110
110101
110100
110011
110010
110001
Negating two’s Complement Numbers
To negate a number in the Two’s Complement system:
 Invert all bits of the number
 Add 1.
1. Example 1.9 – Negating in Two’s Complement System
Find the opposite numbers of the followings numbers (2’s complement) in 6-bit system:
–10
–8
–4
+7
–14
2’s Compl.
1’s Compl.
110110
111000
111100
000111
110010
001001
000111
000011
111000
001101
+1
2’s Compl.
001010
001000
000100
111001
001110
Opposite
Number
+10
+8
+4
–7
+14
2. Negating Octal and Hexadecimal Numbers
 Convert the octal or hex number to its binary equivalent with an appropriate number of
bits (e.g. 8, 16, 32 …)
 Negate the binary equivalent
 Convert the two’s complement number to its octal or hex equivalent
Lecture 1 – Introduction, Numbers, and Number System
Page 6 of 8
a. Example 1.10 – Negating Hexadecimal Numbers in Two’s Complement System
Find the opposite number of the following hexadecimal numbers
A107
FEB7
A0CF
FFCA
F0FA
1.7.
Binary Equivalent
1’s Complement.
1010000100000111
1111111010110111
1010000011001111
1111111111001010
1111000011111010
0101111011111000
0000000101001000
0101111100110000
0000000000110101
0000111100000101
+1
0101111011111001
0000000101001001
0101111100110001
0000000000110110
0000111100000110
Hexadecimal
Equivalent
5EF9
0149
5F31
0036
0F06
+5
+(–2)
+3
0101
1110
10011
2’s Complement
Addition in Two’s Complement System
Any carry-out from the signed bit is ignored
+5
+(+2)
+7
0101
0010
0111
ignore
–5
+(+2)
–3
1.8.
1011
0010
1101
–5
+(–2)
–7
1011
1110
11001
Ignore
Subtraction in Two’s Complement System
The easiest way of performing subtraction is to negate the subtrahend and add it to the
minuend. First, find the 2’s complement of the subtrahend and then perform addition.
+5
– (+2)
+3
0101
– 0010
2’s Compl
0101
+1110
10011
+5
– (–2)
+7
0101
–1110
2’s Compl
0101
+0010
0111
–5
– (–2)
–3
1011
–1110
2’s Compl
1011
+0010
1101
ignore
–5
– (+2)
–7
1011
– 0010
2’s Compl
1011
+1110
11001
ignore
Lecture 1 – Introduction, Numbers, and Number System
Page 7 of 8
1.9.
Examples on Two’s Complements
1. Example 1.11 – Addition and Subtraction in Two’s Complement Systems
Perform arithmetic operations of these numbers in 8-bit system:
+14
+(+6)
+20
–17
–(+11)
–28
00001110
00000110
00010100
11101111
– 00001011
+24
– (+12)
+12
2’s Compl
11101111
+ 11110101
1 11100100
Lecture 1 – Introduction, Numbers, and Number System
–23
– (–11)
–12
00011000
– 00001100
2’s Compl
00011000
+11110100
1 00001100
11101001
– 11110101
2’s Compl
11101001
+ 00001011
11110100
Page 8 of 8