CS计算机代考程序代写 algorithm mips Base Conversion

Base Conversion
Three cases:
I. From any base b to base 10
II. From base 10 to any base b
III. From any base b to any other base c

From Base b to Base 10 •Base(radix): b
• Digits (symbols): 0 … (b – 1) • Sn-1Sn-2….S2S1S0
Value = Σ (Sibi)
Use summation to transform any base to decimal
n-1
i=0

From Base b to Base 10
■ Example: 12345 = ?10
= 1 * 53 + 2 * 52 + 3 * 51 + 4 * 50 = 125 + 50 + 15 + 4
= 194
■ Example: 2015 = ?10
= 2 * 52 + 0 * 51 + 1 * 50 = 50 + 1
= 51

From Base 10 to Base b
■ Use successive divisions
■ Remember the remainders
■ Divide again with the quotients

From Base 10 to Base b Example: 12310 = ??????5

From Base 10 to Base b Example: 12310 = ?????5
N = 123, i = 0, b = 5 q = N/b = 123/5 = 24 r = 123 % 5 = 3
123 =___3 10 5
■ ■ ■ ■

From Base 10 to Base b Example: 12310 = _ _ _ 35
N = 24, i = 1, b = 5 q = N/b = 24/5 = 4 r = 24 % 5 = 4
123 =__43 10 5
■ ■ ■ ■

From Base 10 to Base b Example: 12310 = _ _ 4 35
N = 4, i = 2, b = 5 q = N/b = 4/5 = 0 r=4%5=4
123 = _ 4 4 3 10 5
■ ■ ■ ■

From Base 10 to Base b Example: 12310 = 4435
■ q = 0, algorithm ends!

From Base 10 to Base b Example: 201010 = ?5

From Base 10 to Base b Example: 201010 = ??????5
Do it at home!!! (ans = 31020)

From Base b to Base c
■ Use a known intermediate base
■ The easiest way is to convert from base b to base 10 first, and then from 10 to c
■ Or, in some cases, it is easier to use base 2 as the intermediate base (we’ll see them soon)

Binary Number System
■ Base (radix): 2
■ Digits (symbols): 0, 1
■ Binary Digits, or bits
■ Example:
◆ 10012 =1*23 +0*22 +0*21 +1*20
=8 +0 +0 +1
=9
◆ 11000 =1*2 +1*2 +0*2 +0*2 +0*2
104 3 2 1 0
2 = 16 + 8 = 2410

Octal Number System
■ Base (radix): 8
■ Digits (symbols): 0, 1, 2, 3, 4, 5, 6, 7
■ 3458 = 3*82 + 4*81 + 5*80
= 192 + 32 + 5
= 22910
■ 10018 =1*83 +0*82 +0*81 +1*80
= 512 + 1
= 51310
■ In C, octal numbers are represented with a leading 0 (0345 or 01001).

Representing Multi-bit Values
■Number bits from right (0) to left (n-1) ■Use brackets to denote range:
D[l:r] denotes bit l to bit r, from left to right
MSB (Most significant bit)
LSB (Least significant bit)
15 0
A = 0101001101010101
A[14:9] = 101001
A[3:1] = 010
May also see A<14:9>, especially in hardware block diagrams.

Hexadecimal Number System
■ Base (radix): 16
■ Digits (symbols): 0-9, A–F (a-f)
■ In C/MIPS: leading “0x” (e.g., 0xA3)
■ Sometimes: leading “x” (e.g., “x3000”)
■ Hexadecimal is also known as “hex” for short
Hex
Decimal
A
10
B
11
C
12
D
13
E
14
F
15

Examples of Converting Hex to Decimal
■ 0xA316 = A*161 + 3*160 = 10*16 + 3*1
= 160 + 3
= 163
■ 0x3E816 = Do it at Home!! (answer is 1000)

Decimal To Binary Conversion: Method 1
■ Keep dividing decimal value by 2 until the value is 0;
■ Example: 6110

Knowing The Powers Of Two
■ Know them in your sleep
20
1
21
2
22
4
23
8
24
16
25
32
26
64
27
128
28
256
29
512
210
1024
Yes it’s on the exam. ☺

Binary to Octal Conversion
■ Group into 3 starting at least significant bit ◆ Why 3?
◆ Add leading 0 as needed ★ Why not trailing 0s?
■ Write one octal digit for each group

Binary to Octal Conversion: Examples
■ 100 010 111 (binary) 4 2 7 (octal)
■ 10 101 110 (binary) (octal)
Octal
Binary
0
000
1
001
2
010
3
011
4
100
5
101
6
110
7
111

Binary to Octal Conversion: Examples
■ 100 010 111 (binary) 4 2 7 (octal)
■ 010 101 110 (binary) 2 5 6 (octal)
Octal
Binary
0
000
1
001
2
010
3
011
4
100
5
101
6
110
7
111

Octal to Binary Conversion
■ Write down the 3-bit binary code for each octal digit
■ Example: ◆ 047
Octal
Binary
0
000
1
001
2
010
3
011
4
100
5
101
6
110
7
111

Octal to Binary Conversion
■ Write down the 3-bit binary code for each octal digit
■ E◆xample: 000 100 111
Octal
Binary
0
000
1
001
2
010
3
011
4
100
5
101
6
110
7
111
0
4
7

Binary to Hex Conversion
■ Group into 4 starting at least significant bit ◆ Why 4?
◆ Add leading 0 if needed
■ Write one hex digit for each group

Binary to Hex Conversion: Examples
1001 1110 0111 0000 (binary) 9 E 7 0 (hex)
1 1111 1010 0011 (binary) (hex)
Hex
Bin
Hex
Bin
0
0000
8
1000
1
0001
9
1001
2
0010
A
1010
3
0011
B
1011
4
0100
C
1100
5
0101
D
1101
6
0110
E
1110
7
0111
F
1111

Binary to Hex Conversion: Examples
1001 1110 0111 0000 (binary) 9 E 7 0 (hex)
0001 1111 1010 0011 (binary) 1 F A 3 (hex)
Hex
Bin
Hex
Bin
0
0000
8
1000
1
0001
9
1001
2
0010
A
1010
3
0011
B
1011
4
0100
C
1100
5
0101
D
1101
6
0110
E
1110
7
0111
F
1111

Hex to Binary Conversion
■ Write down the 4-bit binary code for each hex digit
■ Example:
◆ 0x 3 9 C 8
0011 1001 1100 1000
Hex
Bin
Hex
Bin
0
0000
8
1000
1
0001
9
1001
2
0010
A
1010
3
0011
B
1011
4
0100
C
1100
5
0101
D
1101
6
0110
E
1110
7
0111
F
1111
Know this in your sleep

Hex to Binary Conversion
■ Write down the 4-bit binary code for each hex digit
■ Example:
◆ 0x 3 9 C 8
0011 1001 1100 1000
Hex
Bin
Hex
Bin
0
0000
8
1000
1
0001
9
1001
2
0010
A
1010
3
0011
B
1011
4
0100
C
1100
5
0101
D
1101
6
0110
E
1110
7
0111
F
1111
Know this in your sleep

Conversion Table
Decimal
Hexadecimal
Octal
Binary
0
0
0
0000
1
1
1
0001
2
2
2
0010
3
3
3
0011
4
4
4
0100
5
5
5
0101
6
6
6
0110
7
7
7
0111
8
8
10
1000
9
9
11
1001
10
A
12
1010
11
B
13
1011
12
C
14
1100
13
D
15
1101
14
E
16
1110
15
F
17
1111

A practical example: HTML Color Codes
RGB
2 digit Hex value for Red, 2 digit Hex value for Green, 2 digit Hex value for Blue

Remember Base Conversion
Three cases:
I. From any base b to base 10
II. From base 10 to any base b
III. From any base b to any other base c

More Conversions
■ Hex → Octal
◆ Doitin2steps
◆ Hex → binary → octal
■ Decimal → Hex
◆ Doitin2steps
◆ Decimal → binary → hex
■ So why use hex and octal and not just binary and decimal?

Largest Number
■ What is the largest number that we can represent in n digits…
◆ Inbase10?10n -1 e.g.n=2,100-1=99 ◆ Inbase2?2n -1
◆ In octal? 8n -1
◆ In hex? 16n -1
◆ Inbase7?7n -1 ◆ Inbaseb?bn -1
■ How many different numbers can we represent with n digits in base b?
bn (remember 0 is a number 2, inclusion)

How many bits are necessary?
■ How many bits are necessary? ◆ log2(444) = 8.8…
◆ ceil(8.8) = 9
■ Where is log2 on my calculator?
logy(x) = log(x)/log(y) log2(x) = log(x)/log(2)