DECIMAL, BINARY, AND HEXADECIMAL. Decimal Numbering System Ten symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Represent larger numbers as a sequence of digits.

1 DECIMAL, BINARY, AND HEXADECIMAL ...
Author: Christopher Allison
0 downloads 4 Views

1 DECIMAL, BINARY, AND HEXADECIMAL

2 Decimal Numbering System Ten symbols: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 Represent larger numbers as a sequence of digits Each digit is one of the available symbols Example: 7061 in decimal (base 10) 7061 10 = (7 x 10 3 ) + (0 x 10 2 ) + (6 x 10 1 ) + (1 x 10 0 )

3 Octal Numbering System Eight symbols: : 0, 1, 2, 3, 4, 5, 6, 7 Notice that we no longer use 8 or 9 Base Comparison: Base 10: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12… Base 8: 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14… Example: What is 15 8 in base 10? 15 8 = (1 x 8 1 ) + (5 x 8 0 ) = 13 10 Example: What is 7061 8 in base 10? 7061 8 = (7 x 8 3 ) + (0 x 8 2 ) + (6 x 8 1 ) + (1 x 8 0 ) = 3633 10

4 Question What is 34 8 in base 10? (A)32 10 (B)34 10 (C)7 10 (D)28 10 (E)35 10

5 Binary Numbering System Binary is base 2 Symbols: 0, 1 Convention: 2 10 = 10 2 = 0b10 Example: What is 0b110 in base 10? 0b110 = 110 2 = (1 x 2 2 ) + (1 x 2 1 ) + (0 x 1 0 ) = 6 10 Base 10Base 8Base 2 000 111 2210 3311 44100 55101 66110 77111 8101000 9111001

6 Hexadecimal Number System Hexadecimal is base 16 (>10) Symbols? 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, …? Convention: 16 10 = 10 16 = 0x10 Example: What is 0xA5 in base 10? 0xA5 = A5 16 = (10 x 16 1 ) + (5 x 16 0 ) = 165 10 8, 9, A, B, C, D, E, F

7 Question Which of the following orderings is correct? (A)0xC < 0b1010 < 11 (B)0xC < 11 < 0b1010 (C)11 < 0b1010 < 0xC (D)0b1010 < 11 < 0xC (E)0b1010 < 0xC < 11

8 BASE CONVERSION

9 Converting to Base 10 Can convert from any base to base 10 110 2 =(1 x 2 2 ) + (1 x 2 1 ) + (0 x 1 0 ) = 6 10 0xA5 = A5 16 =(10 x 16 1 ) + (5 x 16 0 ) = 165 10 We learned to think in base 10, so this is fairly natural for us Challenge: Convert into other bases (e.g. 2, 16)

10 Challenge Question Convert 13 10 to binary Hints: 2 3 = 8 2 2 = 4 2 1 = 2 2 0 = 1

11 Converting from Decimal to Binary Given a decimal number N: List increasing powers of 2 from right to left until ≥ N From left to right, ask is that (power of 2) ≤ N? – If YES, put a 1 below and subtract that power from N – If NO, put a 0 below and keep going Example for 13: 2 4 =162 3 =82 2 =42 1 =22 0 =1 5 1 0 1 1 0 1 0

12 Converting from Decimal to Base B Given a decimal number N: List increasing powers of B from right to left until ≥ N From left to right, ask is that (power of B) ≤ N? – If YES, put how many of that power go into N and subtract from N – If NO, put a 0 and keep going Example for 165 into hexadecimal (base 16): 16 2 =25616 1 =1616 0 =1 5 0 A (10)50

13 Base 10Base 16Base 2 000000 110001 220010 330011 440100 550101 660110 770111 881000 991001 10A1010 11B1011 12C1100 13D1101 14E1110 15F1111

14 Convert 0b100110110101101 How many digits? Pad: 0b 0100 1101 1010 1101 Substitute: 0x4DAD Base 10Base 16Base 2 000000 110001 220010 330011 440100 550101 660110 770111 881000 991001 10A1010 11B1011 12C1100 13D1101 14E1110 15F1111 ? 15

15 Why are we learning this? Why does all of this matter? Humans think about numbers in base 10 but computers think about numbers in base 2 How is it that computers can do all of the amazing things that they do? – Binary encoding

16 BINARY ENCODING

17 Numerical Encoding

18 Binary Encoding ? 2 N

19 So What’s It Mean? A sequence of bits can have many meanings! Consider the hex sequence 0x4E6F21 Common interpretations include: The decimal number 5140257 The characters “No!” The background color of this slide The real number 7.203034 x 10 -39 [floating point] It is up to the program/programmer to decide how to interpret the sequence of bits

20 Summary Humans think about numbers in decimal; computers think about numbers in binary Base conversion to go between Hex is more human-readable than binary All information on a computer is in binary Nice because big difference between “high” and “low” Binary encoding can represent anything! Program needs to know how to interpret bits

21 Summary