Bitwise Operations
 Dealing with Binary Representations of Numbers
WMLScript
supports bitwise operations, although they are less commonly used in
mobile Internet browsing applications. Bitwise operations are
operations that deal with binary representations of numbers. In this
section, we will provide you a brief description of the bitwise
operators supported in WMLScript.
Bitwise
AND, OR, XOR, NOT
The
&, , ^ and ~ are the bitwise AND, bitwise OR, bitwise XOR and
bitwise NOT operators respectively. The following examples illustrate
how to use them:
z
= 4 & 5;
The
binary representation of 4 is 100 and that of 5 is 101. The result of
bitwise AND operation on 100 and 101 is 100, which is 4 in decimal
representation. Hence, after the execution of the code, z contains
the value 4.
z
= 4  5;
The
binary representation of 4 is 100 and that of 5 is 101. The result of
bitwise OR operation on 100 and 101 is 101, which is 5 in decimal
representation. Hence, after the execution of the code, z contains
the value 5.
z
= 4 ^ 5;
The
binary representation of 4 is 100 and that of 5 is 101. The result of
bitwise XOR operation on 100 and 101 is 001, which is 1 in decimal
representation. Hence, after the execution of the code, z has the
value 1.
z
= ~4;
The
32bit binary representation of 4 is
00000000000000000000000000000100. The result of bitwise NOT operation
on 00000000000000000000000000000100 is
11111111111111111111111111111011, which is 5 in decimal
representation. Hence, after the execution of the code, z contains
the value 5.
Bitwise
Left Shift
The
bitwise left shift operator is <<. It helps us shift the bits
of a number to the left by a certain number of places and zero is
used for filling. For example:
z
= 2147483646 << 1;
The
above line of script instructs the WMLScript engine to shift the bits
of 2147483646 to the left by one place and store the result in the z
variable. The 32bit binary representation of 2147483646 is
10000000000000000000000000000010. If its leftmost bit is removed and
a zero is filled at the rightmost position, the result will be
00000000000000000000000000000100 (4 in decimal representation).
Hence, z will contain the value 4 after executing the line of script.
Bitwise
Right Shift with Sign
The
bitwise right shift with sign operator is >>. It helps us shift
the bits of a number to the right by a certain number of places and
the sign bit (i.e. the leftmost bit) is used for filling. For
example:
z
= 2147483646 >> 1;
The
above line of script instructs the WMLScript engine to shift the bits
of 2147483646 to the right by one place and store the result in the
z variable. The 32bit binary representation of 2147483646 is
10000000000000000000000000000010 (sign bit is 1). If its rightmost
bit is removed and a 1 is filled at the leftmost position, the result
will be 11000000000000000000000000000001, (1073741823 in decimal
representation). Hence, z has the value 1073741823 after executing
the line of script.
Bitwise Right
Shift with Zero Fill
The
bitwise right shift with zero fill operator is >>>. Like the
>> operator, it is used to shift the bits of a number to the
right by a certain number of places. However, the >>>
operator uses a zero for filling. Here is an example:
z
= 2147483646 >>> 1;
The
above line of script instructs the WMLScript engine to shift the bits
of 2147483646 to the right by one place and store the result in the
z variable. The 32bit binary representation of 2147483646 is
10000000000000000000000000000010. If its rightmost bit is removed and
a zero is filled at the leftmost position, the result will be
01000000000000000000000000000001, which is 1073741825 in decimal
representation. Hence, z has the value 1073741825 after the execution
of the above WMLScript code.
Feedback Form (ExpandCollapse)

