In the last article we talked about some of the unary and binary operators available in Javascript, namely the arithmetic operators, the comparison operators and the increment/decrement operators. In this article we continue our discussion of the Javascript operators; we will discuss how computers represent data, and bitwise operators.

The Power of Javascript: Operators continued (Page 1 of 5 )

Actually, you don't have to understand how computers store data in order to master Javascript, but it's an interesting thing to understand and it will help you when you learn a programming language like C++ or C#. As to the bitwise operators, you will not find them useful when you use client-side Javascript (I mean Javascript in the browsers), but those bitwise operators are part of Javascript itself, and part of most programming languages, so I can't avoid discussing them. So, how does a computer represent data such as 23, 3454, or 56767?

How Computers represent data

How do computers store a value like 5 in memory (in a variable)? A computer's memory is made up of millions of electrical circuits, which have two states, on and off. On means that there is electricity in the circuit and off means that there is no electricity. How we can use these electrical circuits to store a value like 5?

Actually we can't store the value 5 in memory as 5, because we don't have values other than on and off. We do have, however, something called the binary number system, which is another way to represent numbers using only 2 values (1 and 0). Computers use this instead of the decimal number system, which represents numbers using ten values (1, 2, 3, 4, 5, 6, 7, 8, 9 and 0). The decimal value 5 can be represented in the binary number System as 00000101. Let's explain how the binary number system works.

Let's first consider how we can represent the decimal value 5 using only two values (I'm talking about 1 for turning on the circuit and 0 for turning off the circuit). The first thing that come to our minds is a value like 00011111. Yes, you can turn 5 circuits on and you just get the value 5; this can work. But imagine that we need to store the value one billion in memory. We would need one billion circuits to store it in, which is not practical!

If this had been the only solution, we would not be using computers as we are using them today because we would need an endless amount of memory; actually, we would never have used computers at all. The next thing you might consider is building a system that counts 1, 2, 3, 4, 5 instead of counting how many circuits are turned on. To represent the value 5 in this system, you would turn on the fifth circuit. Let's take a look:

1 2 3 4 5 6 7 8 ..... 0 0 0 0 1 0 0 0

So now we have represented the value 5, but still this is not as practical as we need. We would need a massive amount of memory in order to represent a number like one billion. The binary system is the ultimate solution for this problem. To understand how the binary system works you have to take a look at a decimal value like 278 in which we think of the 8 as a digit in the ones position, 7 in the tens positions and 2 in the hundreds position and so on as follows:

100 10 1 2 7 8

We get the next position by multiplying the current position by 10 (the number of values in the decimal base). The same goes for the binary system; we get the next position by multiplying the current position by two (the number of values in the binary base), so the positions are as follows:

4 2 1 1 1 1

As you can see, we use 1 * 2 = 2 and 2 * 2 = 4 and so on. To convert a binary value like 00000111 to its decimal representation, you need to know the turned on positions (I mean the positions that have the value 1 not 0) and then add the positions' value to get the decimal representation. For the binary value 00000111 we have the first position turned on so we have (the decimal value) 1. The second position is turned on, so we also have the decimal value 2. The third position is turned on so we have the value 4. We add them together to get our number, 4 + 2 + 1 = 7.

In this way we can represent any number with the least possible circuits, which saves memory space and uses it efficiently. In computers terms we call the electrical circuit a bit. Eight bits is called a byte. A byte can store up to the value 255. Let's take a look at what happens when we turn on eight bits:

128 64 32 16 8 4 2 1 1 1 1 1 1 1 1 1

As we did with the value 111 we need to add the turned on positions in order to get the decimal representation of the value, so 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255. Two bytes (which means 16 bits) stores 0 up to 65535. To represent a value like 5 in binary you need to turn on the 4 position and the 1 position like this:

128 64 32 16 8 4 2 1 0 0 0 0 0 1 0 1

This is what you need to know about the binary system. Javascript features a group of operators that manipulate bits, which are called bitwise operators.