### 11.1.3. 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.

#### 11.1.3.1. 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 32-bit 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.

#### 11.1.3.2. 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 32-bit 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.

#### 11.1.3.3. 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 32-bit 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.

#### 11.1.3.4. 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 32-bit 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.

Previous Page | Page 18 of 71 | Next Page |

**Click Here for Full Table of Contents**)

- 1. WMLScript Introduction
- 2. Hello World WMLScript Example
- 3. Compiling WMLScript Code
- 4. WMLScript Language Rules
- 5. Defining WMLScript Functions
- 6. Calling WMLScript Functions
- 7. WMLScript Variables
- 8. WMLScript Data Types
- 9. WMLScript Variables Vs WML Variables
- 10. Passing Arguments to Functions By Value and By Reference
- 11. WMLScript Operators
- 12. WMLScript Conditional Statements
- 13. WMLScript Looping Statements
- 14. WMLScript Standard Libraries Overview
- 15. WMLScript WMLBrowser Standard Library
- 16. WMLScript Dialogs Standard Library
- 17. WMLScript String Standard Library
- 18. WMLScript Float Standard Library
- 19. WMLScript Lang Standard Library
- 20. WMLScript URL Standard Library
- 21. WMLScript Example: Validating Form Data