Explaining binary numbers using light bulbs

Binary numbers are a way of representing information using only two digits: 0 and 1. These digits are known as "bits," and they can be combined to create all sorts of data.

To understand how binary numbers work, let's imagine a row of light bulbs. Each light bulb can be either on or off, which we can represent using a 0 or a 1. So, a row of light bulbs that is turned off would be represented by a string of 0s, like this:

And a row of light bulbs that is turned on would be represented by a string of 1s, like this:

We can use these light bulbs to represent any number we want. For example, the number 3 could be represented like this:

In this case, the first three light bulbs are turned off, and the last two are turned on. We can read this binary number just like we would read any other number, from right to left. The rightmost light bulb represents the number 1, the next light bulb represents the number 2, the next represents the number 4, and so on.

So in the above case, 00011 in binary is equal to 2 + 1 = 3 in decimal.

How many numbers can we represent?

We can use this system to represent any number we want, no matter how large or small. Of course, for this we may need a certain number of light bulbs.

The rule is that the maximum number that can be represented with n light bulbs using the binary numbering system is 2^n - 1. In other words, if you have n light bulbs, you can represent 2^n numbers using those light bulbs. Basically any number from 0 to 2^n - 1.

Example: Using 3 light bulbs

For example, if you have 3 light bulbs, you can represent any number from 0 to 7 (which is 2^3 - 1). Here's how each number from 0 to 7 would be represented:

000 = 0
001 = 1
010 = 2
011 = 3
100 = 4
101 = 5
110 = 6
111 = 7

In total we have 8 values.

Example: Using 5 light bulbs

With 5 light bulbs, you can represent any number from 0 to 31 (which is 2^5 - 1) using the binary numbering system.

Example: Using 8 light bulbs

With 8 light bulbs, you can represent any number from 0 to 255 (which is 2^8 - 1). Here's how each number from 0 to 255 would be represented using 8 light bulbs:

00000000 = 0
00000001 = 1
00000010 = 2
00000011 = 3
00000100 = 4
...
11111110 = 254
11111111 = 255

Fact: A byte has 8 bits. Therefore a byte can be used to represent up to 256 different values.

Example: Using 16 light bulbs

With 16 light bulbs, you can represent any number from 0 to 2^16 - 1, which is 65,535. This is because 2^16 is equal to 65536, and when you subtract 1 from that number, you get 65,535.

Fact: Early home computers could access only 64KB of memory. This is because their address bus was 16 bit wide.

Modern computers have the address bus 64 bit wide. Calculate how much memory they can access! (at least in theory)

Printing binary numbers in JavaScript

In JavaScript it is really simple to print a number in binary format. Just specify the base on the .toString() method, like in the following example:

You can also consult the code in this tutorial about binary and hexadecimal numbers.

Conclusion

In summary, binary numbers are a way of representing information using only two digits (0 and 1), and they are commonly used in computing because they are simple and efficient.

See also

See also the article about hexadecimal numbers.

Read more blog articles Browse JavaScript projects

About codeguppy

CodeGuppy is a FREE coding platform for schools and intependent learners. If you don't have yet an account with codeguppy.com, you can start by visiting the registration page and sign-up for a free account. Registered users can access tons of fun projects!


Follow @codeguppy on Twitter for coding tips and news about codeguppy platform. For more information, please feel free to contact us.