18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 2<br />

Note that left shift preserves the sign of the number it’s operating on. <strong>For</strong> instance, if –2 is shifted to the<br />

left by 5 spaces, it becomes –64, not positive 64. “But isn’t the sign stored in the 32nd bit?” you ask. Yes it<br />

is, but that is behind the scenes of ECMAScript. The developer can never have access to that 32nd bit<br />

directly. Even printing out a negative number as a binary string shows the negative sign (for instance,<br />

–2 is displayed as –10 instead of 10000000000000000000000000000010).<br />

Signed right shift<br />

The signed right shift is represented by two greater-than signs (>>) and shifts all bits in a 32-bit number to<br />

the right while preserving the sign (positive or negative); signed right shift is the exact opposite of left<br />

shift. <strong>For</strong> example, if 64 is shifted to the right five bits, it becomes 2:<br />

var iOld = 64; //equal to binary 1000000<br />

var iNew = iOld >> 5; //equal to binary 10 with is decimal 2<br />

Once again, when bits are shifted, the shift creates empty bits. This time, the empty bits occur at the left<br />

of the number, but after the sign bit (see Figure 2-7). Once again, ECMAScript fills these empty bits with<br />

the value in the sign bit to create a complete number.<br />

"Secret" sign bit<br />

The number 64<br />

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0<br />

The number 64 shifted to the right 5 bits (the number 2)<br />

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0<br />

Padded with zeros<br />

Figure 2-7<br />

Unsigned right shift<br />

The unsigned right shift is represented by three greater-than signs (>>>) and shifts all bits in an<br />

unsigned 32-bit number to the right. <strong>For</strong> numbers that are positive, the effect is the same as a signed<br />

right shift. Using the same example as for the signed right shift example, if 64 is shifted to the right five<br />

bits, it becomes 2:<br />

var iOld = 64; //equal to binary 1000000<br />

var iNew = iOld >>> 5; //equal to binary 10 with is decimal 2<br />

<strong>For</strong> numbers that are negative, however, something quite different happens. You see, the unsigned right<br />

shift operator fills all empty bits with the value contained in the 32nd bit. <strong>For</strong> positive numbers, this bit<br />

is 0; so the empty bits are filled with zero. <strong>For</strong> negative numbers, however, this bit is 1, meaning that all<br />

empty bits are filled with 1. Because the result of unsigned right shift is an unsigned 32-bit number, you<br />

end up with a very large number. <strong>For</strong> example, if you shift –64 to the right by five bits, you end up with<br />

2147483616. How does this happen?<br />

42

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

Saved successfully!

Ooh no, something went wrong!