Bit functions work for any pair of numbers. Inputs and output are represented as a vectors of bits. The steps are:

  1. Converts the inputs into binary.
  2. Performs a standard bitwise operation on these two.
  3. Converts the binary result back into decimal and returns the value.

bitAnd

bitAnd bitwise AND of the inputs

Parameters:

  • a: a number
  • b: a number

Usage:

  • bitAnd(a,b)

Returns:

  • UInt8

bitOr

bitOr bitwise OR of the inputs

Parameters:

  • a: a number
  • b: a number

Usage:

  • bitOr(a,b)

Returns:

  • UInt8

bitXor

bitXor bitwise XOR of the inputs

Parameters:

  • a: a number
  • b: a number

Usage:

  • bitXor(a,b)

Returns:

  • UInt8

bitNot

bitNot bitwise NOT of the input

Parameters:

  • a: a number

Usage:

  • bitNot(a)

Returns:

  • UInt8

bitShiftLeft

bitShiftLeft moves each digit in a number's binary representation left, the most-significant bit is lost, and a 00 bit is inserted on the other end.

Parameters:

  • a: a number
  • b: number of bits to shift

Usage:

  • bitShiftLeft(a,b)

Returns:

  • UInt8

bitShiftRight

bitShiftRight moves each digit in a number's binary representation right, the least-significant bit is lost and a 00 is inserted on the other end.

Parameters:

  • a: a number
  • b: number of bits to shift

Usage:

  • bitShiftRight(a,b)

Returns:

  • UInt8

bitRotateLeft

bitRotateLeft is similar to bitShiftLeft except that the bits that fall off at left end are put back at right end.

Parameters:

  • a: a number
  • b: number of bits to rotate

Usage:

  • bitRotateLeft(a,b)

Returns:

  • UInt8

bitRotateRight

bitRotateRight is similar to bitShiftRight except that the bits that fall off at right end are put back at left end.

Parameters:

  • a: a number
  • b: number of bits to rotate

Usage:

  • bitRotateRight(a,b)

Returns:

  • UInt8

bitTest

bitTest returns the value of a bit at specified position. The countdown starts from 0 from the right to the left.

Parameters:

  • number: a number
  • p: position of bit

Usage:

  • bitTest(number, p)

Returns:

  • UInt8 - value of bit at specific position

bitTestAll

bitTestAll returns result of logical conjuction (AND operator) of all bits at given positions. The countdown starts from 0 from the right to the left.

Parameters:

  • number: a number
  • p1, p2, p3, p4 – positions of bit

Usage:

  • bitTestAll(number, p1, p2, p3, p4, ...)

Returns:

  • UInt8 - result of logical conjuction

bitTestAny

bitTestAny returns result of logical disjunction (OR operator) of all bits at given positions. The countdown starts from 0 from the right to the left.

Parameters:

  • number: a number
  • p1, p2, p3, p4 – positions of bit

Usage:

  • bitTestAny(number, p1, p2, p3, p4, ...)

Returns:

  • UInt8 - result of logical conjunction

bitCount

bitCount calculates the number of bits set to one in the binary representation of a number.

Parameters:

  • number: a number

Usage:

  • bitCount(number)

Returns:

  • UInt8 - number of bits set to one

Did this page help you?