21 March 2007

How Does the Binary System Work: An Introduction

Kiltak of Geeks Are Sexy just posted this. Since I'm a big fan of non-decimal numbering systems, he's getting a linkback for this one. How Does the Binary System Work: An Introduction. I wear a binary watch from Thinkgeek all the time. I've found I read it at the same speed as other people read the ones with hands, and by "other people" I don't mean kids who can't read those, I mean adults who grew up with and have always used analog watches. For reference, the watch stays lit for 5 seconds, and I'm always done finding out what time it is before the lights turn off.

When I was in 6th grade (so in 2000), I was required to learn ternary for school. Ternary is base 3. There's 0, 1, and 2. Decimal 18 is Ternary 200. Decimal 3 is Ternary 10. Binary 1010 is Ternary 201 and Decimal 10. If you understand binary and decimal and really get how this whole numbering system thing works, you should be able to figure out ternary too. On the ternary test, the bonus question was in binary. We didn't learn binary, but the deal was "if you get the theory behind this, you should be able to do binary too." Going a step beyond Kiltak's demo (which you need to read first to understand this), how about I show you how to easily go from binary to hexidecimal (base 16) and octal (base 8)?

Binary -> Octal

Octal switches to 10 at the number 8. In binary, this is 1000. The largest single digit in octal, therefore, is 7. In binary, this is 111. Every octal digit therefore represents 3 binary digits. The decimal number 29 is represented in binary as 11101. Break this into sets of 3, grouping from the right: 011 101. Now, convert each of those groupings into octal (it'll look like they each went back to decimal because of the groupings). You get 3 5. Octal numbers are traditionally shown as being octal by the 0 that precedes them, so the correct way to write this is 035.

Binary -> Hex

Hexidecimal switches at the number 16. How do we represent the numbers 10-15 as single digits, though? Use letters. Hex goes: 1, 2, 3, 4, 5, 6, 7, 8, 9, A [10], B [11], C [12], D [13], E [14], F [15]. If you've ever looked at HTML and wondered why the colors are written #FFAA00 and #CC553E, it's because it's in hex. Since the highest digit is F (15), there are 4 binary digits to each hex digit. Just like with octal, you need to break up the binary. Using 29 again, we break the binary into grouping of 4 and get 0001 1101. To make it a bit easier, we'll stop at decimal in the middle. 0001 is simply 1. 1101 is 8 + 4 + 0 + 1, or 13. 1 is the same in all of them, so we don't need to touch that. 13 has to be converted to the hex D, though. Put the two together: 1D. It is standard practice to preface hex numbers with 0x, so the proper way to write this is 0x1D. If you ever have any interest in Assembly, this representation is something you will see quite a lot. Regarding the HTML color codes I mentioned earlier, hex can fit up to FF (decimal: 255) in two digits. Since colors are represented on a 256 scale of 0-255, this fits. Colors use RGB (red, green, blue) format. #FFAA00 means there's 255 parts red + 170 parts green + no blue.

I'm not going to demonstrate ternary. After these examples, you should understand it well enough to figure it out yourself, and it has no use. I'm fairly sure the reason the teacher taught us ternary was because it's useless and he simply wanted it to be an exercise in learning to think about things differently, the same way discrete math classes do.

No comments: