Learn Binary Numbers

There are 10 types of people in the world, those who understand binary and those that don’t.

What is binary?

The term ‘binary’ confused me for so long. But once I spent a little time understanding what it was about, turns out it’s pretty simple.

It helped me to understand by thinking about why computers use binary. In computer science, binary is important since all values are stored as a string of binary digits; a string of 0's and 1's. Since computers are electrical devices, they operate on information in the form of positive or negative charges on electrical circuits, where the positive charge represents 1 and negative charge represents 0.

In grade school we are taught the decimal system as the way we represent numbers, which is a base 10 system. But really we could use any numerical value we want, the 10 is arbitrary and is probably used since we have 10 fingers. We could just as easily use base 3 or base 12 if we wanted. Computers use base 2, the binary system.

In the decimal system, we count using the values 0 through 9. Once we exceed the number 9 we move over one place from the 1’s column to the 10’s place.

Decimal is base 10 and has ten digits:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Binary is base 2 and has two digits(bits):

0, 1

Binary numbers are created by connecting binary bits in a string.

For example, say we wanted to write the number eleven in decimal. First we place a digit in the 10’s position and we have 1 left over so that goes in the 1’s position.

example: eleven in decimal and binary

In decimal the first column is the 1’s place, then the next is the 10’s place, followed by 100’s…you see where this is going…each next position we just add a zero to the end, so we are increasing by a power of 10. Binary is this same thing, but instead we increase by a power of 2 instead of 10.

How to convert between decimal and binary?

To convert eleven from decimal into binary we can figure it out with a little math:

  1. find the largest power of 2 that we can divide our decimal value into, without being too large…meaning have a remainder greater than or equal to one
  2. repeat with the remainder value until its 0
11/2³ = 1 remainder 3
3/2² = 0 remainder 3
3/2¹ = 1 remainder 1
1/2⁰ = 1 remainder 0

This shows the binary value for eleven is ‘1011’.

Powers of 2 Table, reference http://www.vaughns-1-pagers.com/computer/powers-of-2.htm

If you have a binary value of ‘10111’, here is how to convert to decimal:

  1. start with the most right value binary digit, and multiply it by its power of two counterpart for that column
  2. add up all the values to get the decimal value
Binary value: 10111
1 x 2⁰ = 1
1 x 2¹ = 2
1 x 2² = 4
0 x 2³ = 0
1 x 2⁴ = 16
+ 23 decimal value

Example Algorithm: Convert Decimal to Binary

Pseudocode:

  1. decimal is the value to be converted
  2. binary base is the base 2
  3. binary is an array that will collect 1’s and 0’s
  4. do this until the decimal value is zero:
  • divide the decimal by the base (which is 2) and set the decimal to this value
  • add the remainder (which will be 1 or 0) to leftmost value in the binary array

Solution in JavaScript:

/**
* @param {number} decimal
* @return {string}
*/
var convertDecimalToBinary = function (decimal) {
var binaryBase = 2;
var binary = [];
var remainder;
    while (decimal !== 0) {
remainder = decimal % binaryBase;
decimal = Math.floor(decimal/binaryBase);
binary.unshift(remainder);
}
return binary.join("")
};

When we execute convertDecimalToBinary(23), it performs these calculations:

23/2 = 11 remainder 1, 11 is now the value for decimal
11/2 = 5 remainder 1, 5 is now decimal value
5/2 = 2 remainder 1, 2 is now decimal value
2/2 = 1 remainder 0, 1 is now the decimal value
1/2 = 0 remainder 1, technically decimal is 0.5 but it gets rounded. We stop here since decimal is now 0
Join the remainders together to get '11101' so that:
23 base 10 = 11101 base 2

Coming soon: How to Bitwise Operations