CIS 1.5
Introduction to Programming Using C++
Numeral Systems
Numeral Systems
Hash Marks
- Primitive and simple
- Hard to keep track of 'how many'
- 'Hash marks in groups of five'
- Different numeral system
- How about arithmetic?
- No real zero
Roman Numeral System
- Decimal (not positional)
- No zero
- What about arithmetic?
Positional Numeral Systems
- Numerical value of symbol is dependent upon its position in the representation
- 1232 - 1,000 + 200 + 30 + 2
- The two 2's represent different numbers.
- Total value of the number is the sum of all the position's values\
- Iterate (cycle through) all the symbols in a given position; when run out of symbols...
- ... add another position, placing fiurst symbol there, and restart the cycle in the
original position
- Compact notation
- Simplifies arithmetic operations
The Decimal System
- Probably arose because we have ten fingers
- Ten symbols (one per finger): 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
- After counting to nine (9), we run out of symbols-- go to next position
The Hexadecimal System
- Sixteen symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
- After counting to fifteen (F), we run out of symbols-- go to next position
The Binary System
- Two symbols: 0, 1
- After counting to one (1), we run out of symbols-- go to next position
The Octal System
- Eight symbols: 0, 1, 2, 3, 4, 5, 6, 7
- After counting to seven (7), we run out of symbols-- go to next position
A comparison of the Numerals of the Different Bases
Decimal | Hex | Octal | Binary | Light Switch
|
---|
0 | 0 | 0 | 00000 | off off off off off
|
1 | 1 | 1 | 00001 | off off off off on
|
2 | 2 | 2 | 00010 | off off off on off
|
3 | 3 | 3 | 00011 | off off off on on
|
4 | 4 | 4 | 00100 | off off on off off
|
5 | 5 | 5 | 00101 | off off on off on
|
6 | 6 | 6 | 00110 | off off on on off
|
7 | 7 | 7 | 00111 | off off on on on
|
8 | 8 | 10 | 01000 | off on off off off
|
9 | 9 | 11 | 01001 | off on off off on
|
10 | A | 12 | 01010 | off on off on off
|
11 | B | 13 | 01011 | off on off on on
|
12 | C | 14 | 01100 | off on on off off
|
13 | D | 15 | 01101 | off on on off on
|
14 | E | 16 | 01110 | off on on on off
|
15 | F | 17 | 01111 | off on on on on
|
16 | 10 | 20 | 10000 | on off off off off
|
17 | 11 | 21 | 10001 | on off off off on
|
18 | 12 | 22 | 10010 | on off off on off
|
Indicating the Base/Radix of a Number
We indicate the radix or base we are dealing with using a subscript
(written in decimal):
112 = 310
118 = 910
1116 = 1710
Converting TO Decimal
Hexadecimal to Decimal
Each position reflects a power of 16
- First position (rightmost) is units (1's = 160)
- Next position is "tens" (16's = 161)
- Next position is "hundreds" (256's = 162)
To convert:
- Multiply the (decimal equivalent of the) digit in each position by the power of 16 represented by that position
- Add the values
1AC16 =
C * 1 = 12 * 1 = 12
+ A * 16 = + 10 * 16 = + 160
+ 1 * 256 = + 1 * 256 = + 256
----
428
Binary to Decimal
Each position reflects a power of 2
- First position (rightmost) is units (1's = 20)
- Next position is "tens" (2's = 21)
- Next position is "hundreds" (4's = 22)
To convert:
- Multiply the (decimal equivalent of the) digit in each position by the power of 2 represented by that position
- Add the values
1001012 =
1 * 1 = 1
0 * 2 = 0
1 * 4 = 4
0 * 8 = 0
0 * 16 = 0
1 * 32 = 32
---
37
Any Base to Decimal
Each position reflects a power of the base/radix (e.g. 2 for binary, 8 for octal, 10 for decimal, 16 for hexadecimal)
- First position (rightmost) is units (radix0)
- Next position is "tens" (radix1)
- Next position is "hundreds" (radix2)
To convert:
- Multiply the (decimal equivalent of the) digit in each position by the power of the radix represented by that position
- Add the values
Converting FROM Decimal
Decimal to Hexadecimal
To convert:
- Repeatedly divide by 16
- Hold on to the remainder at each step (represented in hexadecimal)
- Continue until the division produces a quotient of 0
- The hexadecimal result consists of:
- The first remainder as the rightmost digit
- The second remainder is the next (i.e., the tens) digit
- etc.
428 / 16 R 12 = C
26 / 16 R 10 = A
1 / 16 R 1 = 1
0
1AC
Decimal to Binary
To convert:
- Repeatedly divide by 2
- Hold on to the remainder at each step (represented in hexadecimal)
- Continue until the division produces a quotient of 0
- The binary result consists of:
- The first remainder as the rightmost digit
- The second remainder is the next (i.e., the tens) digit
- etc.
37 / 2 R 1
18 / 2 R 0
9 / 2 R 1
4 / 2 R 0
2 / 2 R 0
1 / 2 R 1
0
100101
Decimal to Any Base
To convert:
- Repeatedly divide by the radix
- Hold on to the remainder at each step (represented in the radix's digits)
- Continue until the division produces a quotient of 0
- The result consists of:
- The first remainder as the rightmost digit
- The second remainder is the next (i.e., the tens) digit
- etc.
Converting Between Arbitrary Bases
In general, one can always convert between any two bases b1 and b2 by:
- Converting from b1 to decimal
- Converting from decimal to b2
Converting Between Hexadecimal and Binary
You will have frequently need to convert between hexadecimal and binary and there is fortunately a
simple shortcut:
- The fact that both hexadecimal and binary are powers of 2 (hex = 24; binary = 21)
make the conversion quite straightforward.
- Notice the following table:
Hex | Binary
|
---|
0 | 0000
|
1 | 0001
|
2 | 0010
|
3 | 0011
|
4 | 0100
|
5 | 0101
|
6 | 0110
|
7 | 0111
|
8 | 1000
|
9 | 1001
|
A | 1010
|
B | 1011
|
C | 1100
|
D | 1101
|
E | 1110
|
F | 1111
|
- Exactly four digits of binary represent the same numbers as exactly one digit of hex
- When we have to go to a second hex position, we also have to go to a fifth binary position.
- Each digit of hex there is represented by a corresponding 4-bit binary value.
Hex to Binary
To convert:
- Translate each hex digit into its 4-bit binary equivalent.
- The result is the binary equivalent of the hex number
1AC16 = 0001 1010 1100 = 0001101011002
Binary to Hex
To convert:
- Translate each 4-bit binary value into its single hex digit equivalent.
- The result is the binary equivalent of the hex number
- The only extra concern here is to make sure you begin at the right (the number of binary bits may not be exactly divisible by 4).
1101011002 = 1 A C = 1AC16
Binary Arithmetic
Addition of Binary numbers is similar to that of decimal:
- Starting at the rightmost position, add the two digits:
0 + 0 = 0
|
0 + 1 = 1
|
1 + 0 = 1
|
1 + 1 = 0 ; carry 1 to the next position
|
1 + 1 + 1 = 1 ; carry 1 to the next position
|
111
+ 110
----
1101