Download this program:

StandAloneBitwiseOperatorsExamples.PRG


Example Program


Results

Bitwise AND operator (&)

The result of a bitwise AND is 1 if the corresponding bits of the two operands are 1. If any of the operand bits is 0, the result of the corresponding bit evaluates to 0.

Suppose we perform a bitwise AND operation on two integers, 12 = 00001100 (in binary) and 25 = 00011001 (in binary).

In the example above V3. Variable 3 is set to 00001100 and 00011001 = 00001000 = 8 (decimal notation).

Bitwise OR operator (|)

The result of a bitwise OR operation is 1 if at least one corresponding bit of the two operands is 1. In Ascript, bitwise OR operators are denoted as |.

In the example above V4.  Variable 4 is set to 00001100 or 00011001 = 00011101 = 29 (decimal notation).

Ascript bit shift operator

The Ascript language has two shift operators.

  • The bit right-shift operator >>.
  • The bit left-shift operator <<.

Notice how 212 = 11010100 (binary) is converted by the bit shift operator in the examples above.

Right-shift operator (>>)

The right-shift operator shifts all bits by the specified number of bits to the right. Bit positions freed by the right-shift operator are filled with zeros. It is indicated by the symbol >>.

Variable 6 is set to 11010100 shift 2 bits right = 00110101 (binary) = 53 (decimal)

Variable 7 is set to 11010100 shift right by 7 bits = 00000001 (binary) = 1 (decimal)

Variable 8 is set to 11010100 shift right by 8 bits = 0000000000 (binary) = 0 (decimal)

Variable 9 is set to 11010100 shift right, 0 bit = 11010100 (binary) No shift

Left-shift operator (<<)

The left-shift operator shifts all bits by the specified number of bits to the left. Bit positions freed by the left shift operator are filled with zeros. The symbol for the left-shift operator is <<.

Variable 10 is set to 11010100 Shift 1 bit left = 110101000 (binary) = 424 (decimal)

Variable 11 is set to 11010100 Left shift by zero bit = 11010100 (binary) No shift

Variable 12 is set to 11010100 4-bit left shift = 1101010000 (binary) = 3392 (decimal)