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? |
|---|---|
| Summary | Returns the bitwise AND of $value1 and $value2. If one of the arguments is empty, an empty sequence is returned. |
| Examples | Result: 8 |
bit:or
Added: New function.
| Signature | bit:or( $value1 as xs:integer?, $value2 as xs:integer?) as xs:integer? |
|---|---|
| Summary | Returns the bitwise OR of $value1 and $value2. If one of the arguments is empty, an empty sequence is returned. |
| Examples | Result: 14 |
bit:xor
Added: New function.
| Signature | bit:xor( $value1 as xs:integer?, $value2 as xs:integer?) as xs:integer? |
|---|---|
| Summary | Returns the bitwise exclusive OR of $value1 and $value2. If one of the arguments is empty, an empty sequence is returned. |
| Examples | Result: 6 |
bit:not
Added: New function.
| Signature | bit:not( $value as xs:integer?) as xs:integer? |
|---|---|
| Summary | Inverts all bits of $value. |
| Examples | Result: -6 |
bit:shift
Added: New function.
| Signature | bit:shift( $value as xs:integer?, $by as xs:integer) as xs:integer? |
|---|---|
| Summary | Shifts 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 | Result: 16 Result: -4 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? | ||
|---|---|---|---|
| Summary | Rotates 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 |
| ||
| Examples | Result: 128 Result: 1 |
bit:test
Added: New function.
| Signature | bit:test( $value as xs:integer?, $index as xs:integer) as xs:boolean? |
|---|---|
| Summary | Checks 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 | Result: true Result: true |
bit:count
Added: New function.
| Signature | bit:count( $value as xs:integer?) as xs:integer? |
|---|---|
| Summary | Returns the number of bits set in $value. For negative numbers, all 64 bits are considered. |
| Examples | Result: 8 Result: 64 |
bit:unsigned
Added: New function.
| Signature | bit:unsigned( $value as xs:integer?, $size as xs:integer) as xs:integer? | ||
|---|---|---|---|
| Summary | Returns 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 |
| ||
| Examples | Result: 255 Result: 4294967295Computes 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? | ||
|---|---|---|---|
| Summary | Interprets 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 |
| ||
| Examples | Result: -1 Result: -2048Extracts a signed 12-bit field from bits 4–15 of an integer. The result is -1. |
Errors
| Code | Description |
|---|---|
size | The specified bit size is out of range. |
Changelog
Version 14.0- Added: New module.