Bit: Bitwise Operations

The functions providing bitwise operations are defined in the module Bit.

These functions deal with sequences of binary digits represented by signed integers.

Each integer represents a sequence of binary digits, which is infinite to the left, and can be obtained by writing the integer as a two's complement binary number of infinite size. If the integer is non-negative, the sequence thus obtained contains a finite number of ones. Otherwise, if the integer is negative, the sequence contains a finite number of zeros. For example:
      +3   ...000011
      +2   ...000010
      +1   ...000001
      +0   ...000000
      -1   ...111111
      -2   ...111110
      -3   ...111101

The positions in the binary sequence are numbered from right to left, starting from zero.

      $func  BitOr   s.Int1 s.Int2 = s.Int;
      $func  BitAnd  s.Int1 s.Int2 = s.Int;
      $func  BitXor  s.Int1 s.Int2 = s.Int;

BitOr returns the bitwise logical "or" of the arguments.

BitAnd returns the bitwise logical "and" of the arguments.

BitXor returns the bitwise logical "exclusive or" of the arguments.

      $func  BitNot  s.Int = s.Int;

BitNot returns the bitwise logical "not" of the argument.

      $func  BitLeft   s.Int s.Shift = s.Int;
      $func  BitRight  s.Int s.Shift = s.Int;

BitLeft returns the result of logically shifting s.Int by the number of positions specified by s.Shift. If s.Shift is non-negative, s.Int is shifted left, the new bits being zerofilled. Otherwise, if s.Shift is negative, s.Int is shifted right.

BitRight returns the result of logically shifting s.Int by the number of positions specified by s.Shift. If s.Shift is non-negative, s.Int is shifted right. Otherwise, if s.Shift is negative, s.Int is shifted left, the new bits being zero-filled.

      $func? BitTest   s.Int s.Pos = ;

BitTest returns a failure, if the position s.Pos in s.Int is equal to zero, otherwise, it returns an empty ground expression.

      $func  BitSet    s.Int s.Pos = s.Int;
      $func  BitClear  s.Int s.Pos = s.Int;

BitSet sets the position s.Pos in s.Int to 1, and returns the integer thus obtained.

BitClear sets the position s.Pos in s.Int to 0, and returns the integer thus obtained.

      $func  BitLength  s.Int = s.Len;

BitLength returns the "length" of s.Int. Namely, if s.Int is non-negative, the function returns the position of the right-most 0 such that there is no 1 to the left of this 0. Otherwise, if s.Int is negative, the function returns the position of the rightmost 1 such that there is no 0 to the left of this 1.

For example:
      <BitLength  3>     ==>  2
      <BitLength  2>     ==>  2
      <BitLength  1>     ==>  1
      <BitLength  0>     ==>  0
      <BitLength -1>     ==>  0
      <BitLength -2>     ==>  1
      <BitLength -3>     ==>  2