What is binary?
The term "binary" means "composed of two pieces or two parts". Dispite its strong association with computer, we can actually use the term binary in everyday language like "She had a binary choice; choose the cookies, or choose freedom." More specifically, a
binary number is a number represented by only two symbols - usually "1" and "0". The binary number system is the most symbolically simple way of expressing numbers, and has been used to various extents by
ancient Egyptians, in
ancient Chinese texts, by a 2nd century BC
Indian scholar and by many other ancient cultures.
Modern binary usage is heavily influenced by
George Boole, who invented an system of logic that is now known as "
Boolean algebra". Computer scientists typically use ones/zeros or true/false or on/off to represent the two symbols of the binary number system. When these symbols are put together into a string, we call each symbol a "bit" - the smallest possible unit of information.
How the conversion works
Binary numbers may look a bit scary, but they're actually quite simple to interpret. Let's look at a decimal number for a second:
> 678
The "6" is in the "one hundreds" place, so we know that it represent 6 groups of 100. The "7" is in the "tens" place, so we know it represents 7 groups of 10. Similarly, the "8" is in the "ones" place, so it represents 8 groups of 1. So when you read "678" you're interpreting the number and its position to arrive at an understanding of "how much" it represents.
We do the same thing with binary, except instead of having "ones", "tens" and "hundreds", we have "ones", "twos", "fours", "eights", etc. The following diagram from
aulavirtual.cl runs us through an example:
Makes sense? Here's a table of the first few binary numbers to help you practice:
Decimal |
Binary |
0 |
000 |
1 |
001 |
2 |
010 |
3 |
011 |
4 |
100 |
5 |
101 |
JavaScript Functions
The JavaScript functions to convert to and from binary are amazingly simple!
function binaryToDecimal(binary) {
return parseInt(binary, 2);
}
function decimalToBinary(decimal) {
return parseInt(decimal, 10).toString(2);
}
↓ Read more... ↓