Binary to Decimal

Download Report

Transcript Binary to Decimal

Binary (Base 2, b)
Octal (Oct, Base 8)
Hexadecimal (Hex, Base 16, h, 0x)
Decimal (Base 10, d)
OR
Counting for Aliens
Why Binary and Hex?
Computers are essentially dumb machines
which can only do operations in base 2. They
have many many “switches” in their
processors each of which can either be on (1)
or off (0). Hence all data that is to be
manipulated on a computer must be
converted to binary behind the scenes. Hex is
just a convenient way to avoid working in
straight binary.
Representing numbers in different
bases (back to elementary
school math)
We all agree we have 14 dots!
Representing numbers in different
bases (back to elementary
school math)
1 group of ten and 4 groups of 1 = 14 base 10
Representing numbers in different
bases (back to elementary
school math)
1 group of 8 and 6 groups of one = 16 base 8
Representing numbers in different
bases (back to elementary
school math)
1 group of 8 and 1 group of 1 four and
1 group of 2 and 0 groups of ones = 1110 base 2
Binary to Decimal
1024
512 256
128
64
32
16
8
4
2
1
1
0
0
1
0
10010 base 2 = 16 + 2 = 18 base 10
Let’s try a game………..
Decimal to Binary (method 1)
from askville.amazon.com
I) Find the greatest power of two that's smaller than or equal to your number. Put a 1 in that power's place.
II) Subtract that power of two from your number. If the result is 0, go to step IV.
III) Let your new number be the difference computed in step II. Go to Step I.
IV) Fill in 0's in all positions that you haven't already filled in with 1s.
So, let's do this for a number like 2000 decimal:
The greatest power of 2 smaller than 2000 is 1024. So we'll put a 1 in that place:
1___________
2000 - 1024 = 976
Greatest power of two less than 976 is 512. Put a 1 in that place:
11__________
976 - 512 = 464
Greatest power of two less than 464 is 256. Put a 1 in that place:
111_________
464 - 256 = 208.
Greatest power of two less than 208 is 128. Put a 1 in that place:
1111________
208 - 128 = 80
Greatest power of two less than 80 is 64. Put a 1 in that place:
11111_______
80 - 64 = 16
Greatest power of two less than (or equal to) 16 is 16. Put a 1 in that place:
11111_1_____
16 - 16 = 0
Fill in 0s in all remaining spaces:
111110100000
So, 2000d = 111110100000b
Decimal to Binary (method 2)
Take your decimal number and divide it by 2 continuously.
For example,
100 decimal = ______ in binary
100/2= 50 Remainder 0
50/2= 25, remainder 0
25/2-12 remainder 1
12/2=6, remainder 0
6/2=3, remainder 0
3/2=1 remainder 1
1/2=0 remainder 1
The remainder numbers are the binary and you read upwards
So: 1100100 binary for a decimal of 100.
What the Heck is Hex
• Base 16 is like Base 10 EXCEPT we need more
symbols
• In base 10 we represent all values using 10
symbols {0 1 2 3 4 5 6 7 8 9}
• In base 16 we need 6 more symbols for a total
of 16 so we use letters
• The symbols used in base to represent all
values are {0,1,2,3,4,5,6,7,8,9,A,B,C,D,F}
Binary to Hex and Hex to Binary
• Each Hex digit can be easily converted to 4 binary digits.
• Each group of 4 binary digits can be easily converted to hex
0h = 0000b
1h = 0001b
2h = 0010b
3h = 0011b
4h = 0100b
5h = 0101b
6h = 0110b
7h = 0111b
8h = 1000b
9h = 1001b
Ah = 1010b
Bh = 1011b
Ch = 1100b
Dh = 1101b
Eh = 1110b
Fh = 1111b
i.e. 1 3 B 4 h =
0001 0011
1011 0100 b
HEXadecimal to Decimal
First off know the digits for Hex
0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
So 7 base 10 = 7 base 16
But 13 base 10 = D base 16
And F base 16 = 15 base 10
This makes for some weirdness
7+7=E
Representing numbers in different
bases (back to elementary
school math)
We all agree we have 24 dots!
Representing numbers in different
bases (back to elementary
school math)
1 group of 16 and 8 ones = 18 base 16
Representing numbers in different
bases (back to elementary
school math)
We all agree we have 30 dots!
Representing numbers in different
bases (back to elementary
school math)
1 groups of 16 and 14 ones BUT 14 needs to be represent with a single
symbol not two symbols like 1 and 4. So in base 16 we use letters. We
Use A for 10, B for 11, C for 12, D for 13, E for 14, and F for 15.
So 30 dots is 1E in base 16.
HEXadecimal to Decimal
Some serious multiplication can be involved
Determine your powers of 16:
1 16 256 4096 65536………
So 1AF0 base 16 = (1)4096 + (10)256 + (15)16 +
(0)1 = 6896 base 10
HEXadecimal to Decimal
Method two (use binary to help)
Every Hex digit represent 4 binary digits
0h = 0000b
9h = 1001b
1h = 0001b
Ah = 1010b
2h = 0010b
Bh = 1011b
3h = 0011b
Ch = 1100b
4h = 0100b
Dh = 1101b
5h = 0101b
Eh = 1110b
6h = 0110b
Fh = 1111b
7h = 0111b
8h = 1000b
HEXadecimal to Decimal
Method two continued (use binary to help)
Convert the hex digits to groups of 4 binary digits
Then convert the binary to decimal
1AF0 hex = 0001 1010 1111 0000 base 2
Working backwards
0+0+0+0+16+32+64+128+0+512+0+2048+4096+0+0+0 = 6896
Decimal to HEXadecimal
Method 1) Divide by 16 and write down
remainders
700 base 10 / 16 = 43 Remainder 12
43/16 = 2 Remainder 11
2/16 = 0 Remainder 2
so 700 decimal = 2BC hex
OR Method 2)
Covert to binary and then group in 4 bits 0010 1011 1100 and convert each to a
hex digit 2 B C
HOMEWORK - Practice examples
(convert to base 10 for addition)
1.
2.
3.
4.
5.
6.
7.
777 base 10 = ______ base 2
11001110 b = _______ d
2A9 hex = ______ base 2 = ________ base 10
10 h + 10 d = _____ h = ______ b
1010 b + FF h = _____ d
16 h + 32 h = ____ d
3D7C h = _________ base 2
HOMEWORK Part 1
• Write down a method for converting between
Octal (base 8) and Decimal and vice versa.
• Write down a method for converting between
Octal (base 8) and Binary and vice versa.
• Write down a method for converting between
Octal (base 8) and Hex and vice versa.
Delving Deeper…
• Checking your answers
http://www.easycalculation.com/decimal-converter.php
• Decimal(fractional) numbers in binary
http://cs.furman.edu/digitaldomain/more/ch6/dec_frac_to_bin.htm
http://mathforum.org/library/drmath/view/56091.html
• Signed binary numbers, addition & subtraction
http://en.wikipedia.org/wiki/Signed_number_representations
• Multiplying and dividing in base 2
http://www.helpwithpcs.com/courses/multiplying-dividing-binarynumbers.htm