I've seen a lot of people struggle when first learning decimal-to-binary and other conversions, mostly because they find themselves overwhelmed with conversion charts and don't quite grasp the concept of a numbering base. I decided to write this post when I realized the method I use to teach people binary and hexadecimal isn't used in any of the books I have (which isn't to say it's unique by any means, but perhaps not very widespread).
If you do use the methodology described here, I would appreciate feedback on how well it worked (or didn't) so that it can be improved.
To express values greater than the highest available digit (e.g. values greater than 9), we must use multiple digits. We increase the column to the left (which is zero if not present) by one and reset the current column to zero; 09 becomes 10. This is a recursive process; it is repeated as necessary until we reach the leftmost column of the number (e.g. counting from 999 to 1000).
Here's the key concept to absorb: every numbering base works this way. The only difference is the amount of digits you can use.
Congratulations, you now know how to count in octal (or base-8), which uses only the digits 0 through 7.
And now you can count in binary.
One note here: binary numbers are typically written in groups of eight bits (_binary digits_), with leading zeros for padding. This is done out of convention simply because this is typically the smallest amount of bits a computer deals with at any one time.
Note that hexadecimal numbers are normally denoted with a leading "0x"; the number in the above example would typically be written 0x11. Padded to fill a 32-bit variable, it would be written 0x00000011.
The value of each place is derived as the base number (10 for decimal, 8 for octal, etc.) to the power of its column (starting with zero). In other words:
(A quick math tip: remember that any number to a power of zero equals one.)
Realizing this, we can easily extend this concept to converting other bases to decimal. Let's try our octal number 21:
Conversion from binary takes a bit longer but works exactly the same way:
It just takes a bit of practice to begin memorizing the first eight powers of two (1, 2, 4, 8, 16, 32, 64, and 128). Once you've got them locked in your head, converting between decimal and binary becomes second nature.
If you do use the methodology described here, I would appreciate feedback on how well it worked (or didn't) so that it can be improved.
Step 1: Relearn to Count in Decimal
Think back to kindergarten or whenever you first learned to count. As a typical example, let's say you have seventeen apples. To express this as a decimal value, start with zero and begin counting: 1, 2, 3... up to 9. When you reach the tenth apple, you've run out of digits. This is because we count in a base-10 numbering system, more commonly known as decimal, which employs the digits 0 through 9.To express values greater than the highest available digit (e.g. values greater than 9), we must use multiple digits. We increase the column to the left (which is zero if not present) by one and reset the current column to zero; 09 becomes 10. This is a recursive process; it is repeated as necessary until we reach the leftmost column of the number (e.g. counting from 999 to 1000).
Here's the key concept to absorb: every numbering base works this way. The only difference is the amount of digits you can use.
Step 2: Counting with Fewer Digits
Now let's try counting to seventeen again, but this time you can't use the digits 8 or 9. We begin counting, 1, 2, 3... 7. Since we're out of available digits at this point, we increase the column to the left from 0 (or nothing) to 1, and reset the current column to zero. We continue increasing the value of this column again until we run out of digits a second time. The column to the left is again increased and the current column reset. We continue increasing the value of the rightmost column again until we run out of apples to count.Congratulations, you now know how to count in octal (or base-8), which uses only the digits 0 through 7.
Step 3: Counting with Even Fewer Digits
Let's extend this concept to its extreme; for this exercise, you can only use the digits 0 and 1. Start counting: 0, 1 -- we're already out of digits, so add another column, resume counting, and repeat. Notice that we have to increase the column to the left and start over for every other apple we count.And now you can count in binary.
One note here: binary numbers are typically written in groups of eight bits (_binary digits_), with leading zeros for padding. This is done out of convention simply because this is typically the smallest amount of bits a computer deals with at any one time.
Step 4: Counting with More Digits
"What do you mean, more digits? We only have ten!" True, we only have ten numeric digits; that's why we begin adding letters to the mix for bases like hexadecimal (base-16) or "hex" for short.Note that hexadecimal numbers are normally denoted with a leading "0x"; the number in the above example would typically be written 0x11. Padded to fill a 32-bit variable, it would be written 0x00000011.
Step 5: Converting Back to Decimal
To convert a number from another base to a decimal number, each digit is multiplied by the value of its place. To illustrate this, first we example a the decimal number - 742, for instance - can be expressed as the sum of its place values:The value of each place is derived as the base number (10 for decimal, 8 for octal, etc.) to the power of its column (starting with zero). In other words:
(A quick math tip: remember that any number to a power of zero equals one.)
Realizing this, we can easily extend this concept to converting other bases to decimal. Let's try our octal number 21:
Conversion from binary takes a bit longer but works exactly the same way:
It just takes a bit of practice to begin memorizing the first eight powers of two (1, 2, 4, 8, 16, 32, 64, and 128). Once you've got them locked in your head, converting between decimal and binary becomes second nature.
Tidak ada komentar:
Posting Komentar