Bit functions in GoogleSQL

GoogleSQL for Spanner supports the following bit functions.

Function list

NameSummary
BIT_AND Performs a bitwise AND operation on an expression.
For more information, seeAggregate functions.
BIT_COUNT Gets the number of bits that are set in an input expression.
BIT_OR Performs a bitwise OR operation on an expression.
For more information, seeAggregate functions.
BIT_REVERSEReverses the bits in an integer.
BIT_XOR Performs a bitwise XOR operation on an expression.
For more information, seeAggregate functions.

BIT_COUNT

BIT_COUNT(expression)

Description

The input,expression, must be aninteger orBYTES.

Returns the number of bits that are set in the inputexpression.For signed integers, this is the number of bits in two's complement form.

Return Data Type

INT64

Example

SELECTa,BIT_COUNT(a)ASa_bits,FORMAT("%T",b)asb,BIT_COUNT(b)ASb_bitsFROMUNNEST([STRUCT(0ASa,b''ASb),(0,b'\x00'),(5,b'\x05'),(8,b'\x00\x08'),(0xFFFF,b'\xFF\xFF'),(-2,b'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFE'),(-1,b'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF'),(NULL,b'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF')])ASx;/*-------+--------+---------------------------------------------+--------+ | a     | a_bits | b                                           | b_bits | +-------+--------+---------------------------------------------+--------+ | 0     | 0      | b""                                         | 0      | | 0     | 0      | b"\x00"                                     | 0      | | 5     | 2      | b"\x05"                                     | 2      | | 8     | 1      | b"\x00\x08"                                 | 1      | | 65535 | 16     | b"\xff\xff"                                 | 16     | | -2    | 63     | b"\xff\xff\xff\xff\xff\xff\xff\xfe"         | 63     | | -1    | 64     | b"\xff\xff\xff\xff\xff\xff\xff\xff"         | 64     | | NULL  | NULL   | b"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" | 80     | +-------+--------+---------------------------------------------+--------*/

BIT_REVERSE

BIT_REVERSE(value,preserve_sign)

Description

Takes an integer value and returns its bit-reversed version. Whenpreserve_sign isTRUE, this function provides the same bit-reversalalgorithm used in bit-reversed sequence. For more information, seeBit-reversed sequence.

If the input value isNULL, the function returnsNULL.

Arguments:

  • value: The integer to bit reverse.Sequence only supportsINT64.
  • preserve_sign:TRUE to exclude the sign bit, otherwiseFALSE.

Return Data Type

The same data type asvalue.

Example

SELECTBIT_REVERSE(100,true)ASresults/*---------------------+ | Results             | +---------------------+ | 1369094286720630784 | +---------------------*/
SELECTBIT_REVERSE(100,false)ASresults/*---------------------+ | Results             | +---------------------+ | 2738188573441261568 | +---------------------*/
SELECTBIT_REVERSE(-100,true)ASresults/*----------------------+ | Results              | +----------------------+ | -7133701809754865665 | +----------------------*/
SELECTBIT_REVERSE(-100,false)ASresults/*---------------------+ | Results             | +---------------------+ | 4179340454199820287 | +---------------------*/

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.