Main Page » XQuery » Functions » Bit Functions
BaseX 14 · This is the documentation of an upcoming version. Current version →

Bit Functions

Added: New module.

This module contains functions for bitwise operations on integers. Bitwise operations on binary data are provided by the Binary Functions module.

Conventions

All functions and errors are in the http://basex.org/modules/bit namespace, to which the bit prefix is statically bound.

Integers are processed as 64-bit two’s complement values. Bits are indexed from 0 (least significant bit) to 63 (sign bit). Left shifts discard overflowing bits instead of raising an error, which simplifies the implementation of hash functions, checksums and similar algorithms.

Functions

bit:and

Added: New function.

Signature
bit:and(  $value1  as xs:integer?,  $value2  as xs:integer?) as xs:integer?
SummaryReturns the bitwise AND of $value1 and $value2. If one of the arguments is empty, an empty sequence is returned.
Examples
bit:and(12, 10)
Result: 8

bit:or

Added: New function.

Signature
bit:or(  $value1  as xs:integer?,  $value2  as xs:integer?) as xs:integer?
SummaryReturns the bitwise OR of $value1 and $value2. If one of the arguments is empty, an empty sequence is returned.
Examples
bit:or(12, 10)
Result: 14

bit:xor

Added: New function.

Signature
bit:xor(  $value1  as xs:integer?,  $value2  as xs:integer?) as xs:integer?
SummaryReturns the bitwise exclusive OR of $value1 and $value2. If one of the arguments is empty, an empty sequence is returned.
Examples
bit:xor(12, 10)
Result: 6

bit:not

Added: New function.

Signature
bit:not(  $value  as xs:integer?) as xs:integer?
SummaryInverts all bits of $value.
Examples
bit:not(5)
Result: -6

bit:shift

Added: New function.

Signature
bit:shift(  $value  as xs:integer?,  $by     as xs:integer) as xs:integer?
SummaryShifts the bits of $value by $by positions: to the left if $by is positive, and to the right if it is negative. Bits shifted beyond the sign bit are discarded. Right shifts preserve the sign. If $by is 64 or larger, 0 is returned.
Examples
bit:shift(1, 4)
Result: 16
bit:shift(-16, -2)
Result: -4
bit:shift(1, 63)
Result: -9223372036854775808

bit:rotate

Added: New function.

Signature
bit:rotate(  $value  as xs:integer?,  $by     as xs:integer,  $size   as xs:integer) as xs:integer?
SummaryRotates the lowest $size bits of $value by $by positions: to the left if $by is positive, and to the right if it is negative. Higher bits are discarded. $size must be between 0 and 64.
Errors
sizeThe specified bit size is out of range.
Examples
bit:rotate(1, -1, 8)
Result: 128
bit:rotate(128, 1, 8)
Result: 1

bit:test

Added: New function.

Signature
bit:test(  $value  as xs:integer?,  $index  as xs:integer) as xs:boolean?
SummaryChecks if the bit at position $index of $value is set. For positions beyond 63, the sign bit is returned. A type error is raised if $index is negative.
Examples
bit:test(5, 2)
Result: true
bit:test(-5, 100)
Result: true

bit:count

Added: New function.

Signature
bit:count(  $value  as xs:integer?) as xs:integer?
SummaryReturns the number of bits set in $value. For negative numbers, all 64 bits are considered.
Examples
bit:count(255)
Result: 8
bit:count(-1)
Result: 64

bit:unsigned

Added: New function.

Signature
bit:unsigned(  $value  as xs:integer?,  $size   as xs:integer) as xs:integer?
SummaryReturns the lowest $size bits of $value as non-negative integer. $size must not be negative. As integers are limited to 64 bits, $value is returned unchanged if $size is 64 or larger.
Errors
sizeThe specified bit size is out of range.
Examples
bit:unsigned(-1, 8)
Result: 255
bit:unsigned(-1, 32)
Result: 4294967295
fold-left(
  string-to-codepoints('hello'),
  2166136261,
  fn($hash, $cp) { bit:unsigned(bit:xor($hash, $cp) * 16777619, 32) }
)
Computes the 32-bit FNV-1a hash of an ASCII string. The result is 1335831723.

bit:signed

Added: New function.

Signature
bit:signed(  $value  as xs:integer?,  $size   as xs:integer) as xs:integer?
SummaryInterprets the lowest $size bits of $value as two’s complement integer. $size must not be negative. As integers are limited to 64 bits, $value is returned unchanged if $size is 64 or larger.
Errors
sizeThe specified bit size is out of range.
Examples
bit:signed(255, 8)
Result: -1
bit:signed(2048, 12)
Result: -2048
bit:signed(bit:shift(65535, -4), 12)
Extracts a signed 12-bit field from bits 4–15 of an integer. The result is -1.

Errors

CodeDescription
sizeThe specified bit size is out of range.

Changelog

Version 14.0
  • Added: New module.

⚡Generated with XQuery