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

No credit card required

Bitwise Operators

In addition to the normal arithmetic operators, WMLScript provides four operators for performing Boolean arithmetic on the bits that make up integers. These operators all operate only on integers, so if any operand can’t be converted to an integer, the result of the operation is `invalid`.

Bitwise complement (also called bitwise not) flips every bit in its argument. (Every `0` bit becomes `1`, and every `1` bit becomes `0`.) It’s represented by the `~` operator.

Bitwise and , or , and exclusive or each operate on two integers. For each bit position (32 of them), the bit in the result is set depending on the corresponding bits in the two operands. Bitwise and (represented by `&` ) sets this bit if both operand bits are set, bitwise or (represented by `|` ) sets it if either of the operand bits are set, and bitwise exclusive or (represented by `^` ) sets it if exactly one of the operand bits are set. This may sound complicated, but study the examples and it should become clear.

These operators all behave in exactly the same way as their counterparts in C and Java, so if you know either of those languages, you’ll feel right at home.

Examples include (some numbers are in hexadecimal to make the bit patterns clearer):

 `0x0110 & 0x0011` gives integer `0x0010` `0x0110 | 0x0011` gives integer `0x0111` `0x0110 ^ 0x0011` gives integer `0x0101` `6 & 3` gives integer `2` `6 | 3` gives integer `7` `6 ^ 3` gives integer `5` `~1` gives integer `0xFFFFFFFE` `~0` gives integer `0xFFFFFFFF`

The bitwise operators aren’t ...

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

No credit card required