Title: Binary, Octal and Hexidecimal numbers
1Binary, Octal and Hexidecimal numbers
- Mr. Simon
- Lowell High School
- San Francisco, CA
2Numeral Systems
- The common system of numerals is base 10 called
decimal - You can have other systems with a different base
(also called radix) - In computers, the common systems are
- Base 2 called binary
- Base 8 called octal
- Base 16 called hexidecimal
3Place value and expanded notation
- In Elementary school, students are asked to write
numbers in expanded notation - 1,235 1000 200 30 5
- 1 (103) 2 (102) 3 (101) 5 (100)
- In a decimal number each digit has a place value
that is a power of 10 -
4Binary place values are powers of 2
- In Binary, each digit has a place value that is a
power of 2 - 1011
5Converting Binary to Decimal
- To convert binary to decimal, just write the
number out in expanded notation - 1011 1 (23) 0 (22) 1 (21) 1 (20)
-
6Converting Binary to Decimal
- To convert binary to decimal, just write the
number out in expanded notation - 1011 1 (23) 0 (22) 1 (21) 1 (20)
- 18 04 12 11
-
7Converting Binary to Decimal
- To convert binary to decimal, just write the
number out in expanded notation - 1011 1 (23) 0 (22) 1 (21) 1 (20)
- 18 04 12 11
- 8 0 2 1
-
8Converting Binary to Decimal
- To convert binary to decimal, just write the
number out in expanded notation - 1011 1 (23) 0 (22) 1 (21) 1 (20)
- 18 04 12 11
- 8 0 2 1
- 11
-
9Powers of 8
- 20 1
- 21 2
- 22 4
- 23 8
- 24 16
- 25 32
- 26 64
- 27 128
10Binary numbers often comein groups of eight
called octets
- If all 8 bits are 0s 00000000 then the value of
the octet is 0. - If all 8 bits are 1s, 11111111 then the value of
the octet is 255 (1286432168421). - If the 8 bits are mixed, such as 10101000, add
the place values of the 1 bits 128 32 8 168
11Binary Game!
12Octal Numbers
- Octal is base 8
- Binary numbers are long and unwieldy
- Octal makes it easier to represent binary numbers
than decimal 3 binary digits map to one Octal
digit - So the same number in binary has three times as
many digits as it would in Octal - Octal is used in Linux, for instance to set user
permissions using the chmod command
13Place Value
- In base 8, each place value is a power of 8
- 1,245 (base 8) 1x83 2x82 4x81 5x80
14Converting to base 10
- To convert a number from another base to base 10
- Just write out the number in expanded notation,
and evaluate it - 1,245 (base 8) 1x83 2x82 4x81 5x80
15Converting to base 10
- Just write out the number in expanded notation,
and evaluate it - 1,245 (base 8) 1x83 2x82 4x81 5x80
- 512 128 32 40
161,245 (base 8) 712 (base 10)
- Just write out the number in expanded notation,
and evaluate it - 1,245 (base 8) 1x83 2x82 4x81 5x80
- 512 128 32 40
- 712 (base 10)
17Converting from base 10
- Converting from base 10 to another base is the
opposite - First, figure out what the highest place value is
- Then, work from the highest place value to the
lowest
18197 (base 10) ?? (base 8)
19197 (base 10) ?? (base 8)
- What's the largest power of 8 that will go into
197? - 80 1
- 81 8
- 82 64
- 83 512
2019710 ? x 82 ? x 81 ? x 80
- Since 64 is the largest power of 8 that will fit,
we know that the number will have these three
place values - 80 1
- 81 8
- 82 64
- 83 512
2119710 ? x 82 ? x 81 ? x 80
- How many times will 64 go into 197?
- 64 x 1 64
- 64 x 2 128
- 64 x 3 192
2219710 3 x 82 ? x 81 ? x 80
- Now how much room do we have for the next power
of 8? - 197 192 5
- 81 x ?
2319710 3 x 82 0 x 81 ? x 80
- Now how much room do we have for the next power
of 8? - 197 192 5
- 81 x 0 0
- 81 x 1 8
2419710 3 x 82 0 ? x 80
- Now how much room do we have for the next power
of 8? - 197 192 - 0 5
- 80 x 4 4
- 80 x 5 5
- 80 x 6 6
2519710 3 x 82 0 x 81 5 x 80
- So, 197(base 10) 305(base 8)
26Luckily, you dont usually convert back and forth
from decimal to octal
- Octal converts easily back and forth from binary
- One Octal digit maps to three Binary Digits
- So, 101001 is
27Luckily, you dont usually convert back and forth
from decimal to octal
- Octal converts easily back and forth from binary
- One Octal digit maps to three Binary Digits
- So, 101 001 is
28Luckily, you dont usually convert back and forth
from decimal to octal
- Octal converts easily back and forth from binary
- One Octal digit maps to three Binary Digits
- So, 101 001 is
- 5 1
29Luckily, you dont usually convert back and forth
from decimal to octal
- Octal converts easily back and forth from binary
- One Octal digit maps to three Binary Digits
- So, 1010012 is 518
- Its that easy!
30Convert 4728 to binary
- Again, each Octal digit maps to three Binary
digits
31Convert 4728 to binary
- Again, each Octal digit maps to three Binary
digits - So 472 is
32Convert 4728 to binary
- Again, each Octal digit maps to three Binary
digits - So 4 7 2 is
33Convert 4728 to binary
- Again, each Octal digit maps to three Binary
digits - So 4 7 2 is
- 100 111 010
34Convert 4728 to binary
- Again, each Octal digit maps to three Binary
digits - So 4728 is 1001110102
- Easy!
35In Class
- Convert each octal value to binary, and each
binary value to octal - 63
- 70
- 100100101
- 001101101
36Hexadecimal Numbers
- hex for short
- Hexadecimal is base 16
- It uses sixteen distinct symbols, the symbols 09
to represent values zero to nine, and A, B, C, D,
E, F to represent values ten to fifteen
37Mac addresses are usually in hex
- 00-13-D3-A2-8F
- Some IP addresses (IPv6) are also written in hex
- Encryption keys in wireless routers can be in hex
- You can usually spot hex because it has only the
letters A-F
38Converting Hex to Decimal
- In a hex number, each place value is a power of
16 - 13FA
39Converting Hex to Decimal
- In a hex number, each place value is a power of
16 - 13FA 1 (163)3 (162)15 (161)10 (160)
40Converting Hex to Decimal
- In a hex number, each place value is a power of
16 - 13FA 1 (163)3 (162)15 (161)10 (160)
- 4096 768 240 10
41Converting Hex to Decimal
- In a hex number, each place value is a power of
16 - 13FA 1 (163)3 (162)15 (161)10 (160)
- 4096 768 240 10
- 5114
42If you use a table, converting back and forth
from hex to binary is easy
- To convert hexadecimal F8 to binary, write down
the binary for F first, then the binary for 8. - F 8
- 1111 1000
- So, the answer is 11111000
- Thats all there is to it!
43Converting Binary to Hex
- Converting Binary to Hex is just as easy
- 01011110101101010010
- 0101 1110 1011 0101 0010
- 5 E B 5 2
- 5EB52
44In Class
- Convert each hex value to binary, and each binary
value to hex - E1
- 15
- 10010010
- 00110110
45Sometimes binary and hexcome up in Java
- The getRGB() function returns a single int that
has all three values - //get RGB values at mouseX and mouseY
- int c image.getRGB(mouseX, mouseY)
- //use bitwise logical operations
- int red (c 0x00ff0000) gtgt 16
- int green (c 0x0000ff00) gtgt 8
- int blue c 0x000000ff
- So let's say we clicked on a magenta area. . .
46Sometimes binary and hexcome up in Java
- getRGB() will return 0x00FF00FF
- which in binary is
- 00000000 11111111 00000000 11111111
- The octets are opacity, red, green and blue
47The bitwise
- int red (c 0x00ff0000) gtgt 16
- The single is the bitwise logical AND
- We're looking for the bits that are 1 in both
numbers - 00000000 11111111 00000000 11111111
- 00000000 11111111 00000000 00000000
48The bitwise
- int red (c 0x00ff0000) gtgt 16
- So this expression evaluates to
- 00000000 11111111 00000000 11111111
- 00000000 11111111 00000000 00000000
- 00000000 11111111 00000000 00000000
49gtgt bit-shift operator
- int red (c 0x00ff0000) gtgt 16
- This means shift to the right 16 places
- 00000000 11111111 00000000 00000000
50gtgt bit-shift operator
- int red (c 0x00ff0000) gtgt 16
- This means shift to the right 16 places
- 00000000 11111111 00000000 00000000
- which means that red is assigned
- 00000000 00000000 00000000 11111111
- which is 255 in decimal