Bitwise Operators

version 1 by Bart Massey

  • Home page
  • Beginning
  • Next

  • Documentation

    This module extends Inform 7 by providing some standard bitwise logical operations on numbers. Bitwise operations are rarely needed in the course of stories; the use of XOR in the example below was the inspiration for creating this extension.

    The supported bitwise logical operators are "NOT", "AND", "OR", "XOR" (eXclusive-OR), "SHL" (left shift) and "SHR" (arithmetic right shift). Negative shift counts work as expected.

        bit-not (number)
        (number) bit-and (number)
        (number) bit-or (number)
        (number) bit-xor (number)
        (number) bit-shl by (number)
        (number) bit-shr by (number)

    It is useful to be able to modify variables "in-place". Bitwise NOT is the simplest case. Note that the variable argument N must be a local variable, not a global.

        bit-negate N

    For binary increment and decrement, the standard Inform 7 syntax provides:

        increase N by (number)
        decrease N by (number)

    A similar syntax is used for bitwise shifts SHL and SHR.

        bit-shl N by (number)
        bit-shr N by (number)

    This right-to-left syntax is grammatically awkward for the binary logical operators. Bitwise AND, OR and XOR use a left-to-right construction with "into".

        bit-and (number) into N
        bit-or (number) into N
        bit-xor (number) into N