O'Reilly logo

Learning WML, and WMLScript by Martin Frost

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Shift Operators

As well as these operators to change individual bits, WMLScript also provides shift operators, which allow you to move all the bits in an integer left or right. Because of the way integers are represented, these can be used to multiply or divide integers by a power of two.

Shift right has two operators, >> and >>>. The difference between these two is that >> handles negative numbers properly, so you can always use it to do division by powers of two. On the other hand, >>> treats all numbers as unsigned, even though all integers in WMLScript are signed, and because of this it doesn’t work as you’d expect for negative numbers. It’s most often used, like the &, |, and ^ operators, when you use an integer just to store 32 separate bits, rather than a standard integer.

Shift left is represented by the << operator. It works for all numbers without requiring two operators.

All these shift operators require both their operands to be integers. In addition, the right operand (the number of places to shift) can’t be negative. (This last condition isn’t specified explicitly by the WMLScript specification, but different interpreters handle negative shift counts inconsistently.)

For example:

10 << 3 gives 80 (equivalent to 10 * 2 * 2 * 2)
10 >> 3 gives 1 (equivalent to 10 div 2 div 2 div 2)
10 >>> 3 gives 1 (>> and >>> work the same for positive numbers)
-10 << 3 gives -80 (equivalent to -10 * 2 * 2 * 2)
-10 >> 3 gives -1 (equivalent to -10 div 2 div 2 div 2)
-10 >>> 3 gives 536870911 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required