05.01.2013 Views

hide - Understanding jQuery

hide - Understanding jQuery

hide - Understanding jQuery

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

In order to understand bitwise logic, let’s pick two binary numbers to see what happens when<br />

we use bitwise operators on them. By refering to the table on the Incrementing a Binary Number<br />

page we can choose two arbitrary numbers. Let’s say Seven (0111) and Nine (1001). But<br />

because binary numbers are a little unique in terms of how we assign them to JavaScript variables,<br />

there is a small problem.<br />

In JavaScript there isn’t a way to literally define a binary number. In fact, trying to define a<br />

binary number using its literal representation as in example below we will be defining an octal<br />

(base 8) number instead. JavaScript simply doesn’t have a direct way to define a binary value.<br />

var apple = parseInt(111, 2);<br />

var apple = parseInt(“0111”, 2);<br />

Octal, not binary.<br />

var apple = 0111;<br />

var apple = 7;<br />

var apple = 0x7;<br />

Evaluated as the binary number 7. Note that 0111 would<br />

generate an error. Do not pass leading zeros to parseInt<br />

when specifying a binary number to avoid a syntax error.<br />

However, if we pass the binary number as a string, then<br />

we are allowed to use leading 0s with no problem.<br />

Value is an octal number equal the decimal 73, not 7.<br />

In JavaScript numbers starting with leading 0s indicate<br />

the octal system, not binary. Avoid making this mistake.<br />

The familiar decimal system.<br />

This is the hexadecimal format. 0x7 equals 7 in decimal.<br />

In the third example notice how the number looks as if it is in binary format. But it’s actually<br />

not. By default JavaScript processes a value that starts with a zero as an octal number (base 8)<br />

not binary (base 2), which is what we’re looking for.<br />

An octal number is represented by digits between 0 and 7. And cleverly enough, JavaScript understands<br />

all of this and determines the number type automatically by looking at its value. For<br />

example 0118 would be equal the decimal 118 even though it has a leading 0. This is because<br />

JavaScript knows that the digit 8 is not part of the octal system (0-7 only). But 0117 would be<br />

treated as an octal number and the result would equal the decimal 79. Try it yourself:<br />

var decimal = 0111; // will equal the decimal 73, not the binary 7, nor the decimal 111.<br />

var decimal = 0117; // will equal the decimal 79.<br />

var decimal = 0118; // will equal decimal 118.<br />

If this confuses you, you are not alone. This is just the way JavaScript deals with numbers.<br />

Without an explicit way to specify a number type, things will be confusing. This is why it’s<br />

important to stick to one base. In the end, all numbers are represented in binary format inside<br />

the computer. So the number system becomes our choice. In order to understand bitwise operator<br />

logic we need to use the binary system, yet, it’s not directly available.<br />

But that doesn’t create a big problem for us as long as we know what the number equivalents<br />

are in the binary format. Equipped with parseInt, we are ready to explore the logic behind<br />

bitwise operators.<br />

32

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!