Home arrow JavaScript arrow The Power of Javascript: Operators continued

The Power of Javascript: Operators continued

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.

Author Info:
By: Michael Youssef
Rating: 5 stars5 stars5 stars5 stars5 stars / 10
July 26, 2005
  1. · The Power of Javascript: Operators continued
  2. · Bitwise Operators
  3. · The Bitwise Operators Example
  4. · Assignment Operators
  5. · Assignment Operators Example

print this article

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.

blog comments powered by Disqus

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials