Integer Numbers
Positional Number System
■ The value represented by a digit depends on its position in the number.
■ Ex:
◆ How to decode it?
3 +8*102 +3*101 +2*100
1 * 1000 8* 100 3* 10 2*1
1000 800 302
1832
1
8
3
2
CSE12 Winter 2021
1 * 10
2
Positional Number Systems (base b)
■ Select a number as the base b
■ Define an alphabet of b–1 symbols plus a
symbol for zero to represent all numbers
■ Use an ordered sequence of 1 or more digits d to represent numbers
■ The represented number is the sum of all digits, each multiplied by b to the power of the digit’s position i
Number = ∑ (di·bi) i=0
num digits
CSE12 Winter 2021
3
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
CSE12 Winter 2021
4
From Base b to Base 10
•Base(radix): b
• Digits (symbols): 0 … (b – 1)
• Sn-1Sn-2….S2S1S0
Value = Σ (Sibi)
n-1 i=0
Use summation to transform any base to decimal
CSE12 Winter 2021
5
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
CSE12 Winter 2021
6
From Base 10 to Base b
■ Use successive divisions
■ Remember the
remainders
■ Divide again with the quotients
CSE12 Winter 2021
7
From Base 10 to Base b
Example: 12310 = ??????5
CSE12 Winter 2021
8
From Base 10 to Base b
Example: 12310 = ?????r5
N = 123, i = 0, b = 5 q = N/b = 123/5 = 24 r = 123 % 5 = 3
CSE12 Winter 2021
9
From Base 10 to Base b
Example: 12310 = ????r35
N = 24, i = 1, b = 5 q = N/b = 24/5 = 4 r = 24 % 5 = 4
CSE12 Winter 2021
10
From Base 10 to Base b
Example: 12310 = ???r435
N = 4, i = 2, b = 5 q = N/b = 4/5 = 0 r=4%5=4
CSE12 Winter 2021
11
From Base 10 to Base b
Example: 12310 = 4435
q = 0, algorithm ends.
CSE12 Winter 2021
12
From Base 10 to Base b
Example: 201010 = ?5
CSE12 Winter 2021
13
From Base 10 to Base b
Example: 201010 = ??????5
Do it at home!!! (ans = 31020)
CSE12 Winter 2021
14
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)
CSE12 Winter 2021
15
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
◆ 110002 =1*24+1*23 +0*22 +0*21 +0*20
= 16 + 8 = 24
CSE12 Winter 2021
17
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
= 229
■ 10018 =1*83 +0*82 +0*81 +1*80
= 512 + 1
= 513
■ In C, octal numbers are represented with a leading 0 (0345 or 01001).
CSE12 Winter 2021
18
Representing Multi-bit Values
■Number bits from right (0) to left (n-1)
◆ just a convention — could be left to right, but must be consistent
■Use brackets to denote range:
D[l:r] denotes bit l to bit r, from left to right
MSB (Most
LSB (Least
significant bit)
15 0
A = 0101001101010101
significant bit)
A[2:0] = 101 May also see A<14:9>, especially in hardware block
diagrams.
A[14:9] = 101001
CSE12 Winter 2021
19
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
CSE12 Winter 2021
Hex
A
B
C
D
E
F
Decimal
10
11
12
13
14
15
20
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)
CSE12 Winter 2021
21