> For the complete documentation index, see [llms.txt](https://docs.postgresql.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.postgresql.tw/the-sql-language/functions/functions-bitstring.md).

# 9.6. Bit String Functions and Operators \#

This section describes functions and operators for examining and manipulating bit strings, that is values of the types `bit` and `bit varying`. (While only type `bit` is mentioned in these tables, values of type `bit varying` can be used interchangeably.) Bit strings support the usual comparison operators shown in [Table 9.1](/the-sql-language/functions/functions-comparison.md#FUNCTIONS-COMPARISON-OP-TABLE), as well as the operators shown in [Table 9.14](#FUNCTIONS-BIT-STRING-OP-TABLE).

**Table 9.14. Bit String Operators**

| <p>Operator</p><p>Description</p><p>Example(s)</p>                                                                                                                                                 |                                                                                                                    |                                                                                      |   |                                           |
| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------ | - | ----------------------------------------- |
| <p><code>bit</code> <code>                                                                                                                                                                         |                                                                                                                    | </code> <code>bit</code> → <code>bit</code></p><p>Concatenation</p><p><code>B'10001' |   | B'011'</code> → <code>10001011</code></p> |
| <p><code>bit</code> <code>&</code> <code>bit</code> → <code>bit</code></p><p>Bitwise AND (inputs must be of equal length)</p><p><code>B'10001' & B'01101'</code> → <code>00001</code></p>          |                                                                                                                    |                                                                                      |   |                                           |
| <p><code>bit</code> <code>                                                                                                                                                                         | </code> <code>bit</code> → <code>bit</code></p><p>Bitwise OR (inputs must be of equal length)</p><p><code>B'10001' | B'01101'</code> → <code>11101</code></p>                                             |   |                                           |
| <p><code>bit</code> <code>#</code> <code>bit</code> → <code>bit</code></p><p>Bitwise exclusive OR (inputs must be of equal length)</p><p><code>B'10001' # B'01101'</code> → <code>11100</code></p> |                                                                                                                    |                                                                                      |   |                                           |
| <p><code>~~</code> <code>bit</code> → <code>bit</code></p><p>Bitwise NOT</p><p><code>~~ B'10001'</code> → <code>01110</code></p>                                                                   |                                                                                                                    |                                                                                      |   |                                           |
| <p><code>bit</code> <code><<</code> <code>integer</code> → <code>bit</code></p><p>Bitwise shift left (string length is preserved)</p><p><code>B'10001' << 3</code> → <code>01000</code></p>        |                                                                                                                    |                                                                                      |   |                                           |
| <p><code>bit</code> <code>>></code> <code>integer</code> → <code>bit</code></p><p>Bitwise shift right (string length is preserved)</p><p><code>B'10001' >> 2</code> → <code>00100</code></p>       |                                                                                                                    |                                                                                      |   |                                           |

<br>

Some of the functions available for binary strings are also available for bit strings, as shown in [Table 9.15](#FUNCTIONS-BIT-STRING-TABLE).

**Table 9.15. Bit String Functions**

| <p>Function</p><p>Description</p><p>Example(s)</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>bit\_count</code> ( <code>bit</code> ) → <code>bigint</code></p><p>Returns the number of bits set in the bit string (also known as “popcount”).</p><p><code>bit\_count(B'10111')</code> → <code>4</code></p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| <p><code>bit\_length</code> ( <code>bit</code> ) → <code>integer</code></p><p>Returns number of bits in the bit string.</p><p><code>bit\_length(B'10111')</code> → <code>5</code></p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| <p><code>length</code> ( <code>bit</code> ) → <code>integer</code></p><p>Returns number of bits in the bit string.</p><p><code>length(B'10111')</code> → <code>5</code></p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| <p><code>octet\_length</code> ( <code>bit</code> ) → <code>integer</code></p><p>Returns number of bytes in the bit string.</p><p><code>octet\_length(B'1011111011')</code> → <code>2</code></p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| <p><code>overlay</code> ( <em><code>bits</code></em> <code>bit</code> <code>PLACING</code> <em><code>newsubstring</code></em> <code>bit</code> <code>FROM</code> <em><code>start</code></em> <code>integer</code> \[ <code>FOR</code> <em><code>count</code></em> <code>integer</code> ] ) → <code>bit</code></p><p>Replaces the substring of <em><code>bits</code></em> that starts at the <em><code>start</code></em>'th bit and extends for <em><code>count</code></em> bits with <em><code>newsubstring</code></em>. If <em><code>count</code></em> is omitted, it defaults to the length of <em><code>newsubstring</code></em>.</p><p><code>overlay(B'01010101010101010' placing B'11111' from 2 for 3)</code> → <code>0111110101010101010</code></p> |
| <p><code>position</code> ( <em><code>substring</code></em> <code>bit</code> <code>IN</code> <em><code>bits</code></em> <code>bit</code> ) → <code>integer</code></p><p>Returns first starting index of the specified <em><code>substring</code></em> within <em><code>bits</code></em>, or zero if it's not present.</p><p><code>position(B'010' in B'000001101011')</code> → <code>8</code></p>                                                                                                                                                                                                                                                                                                                                                           |
| <p><code>substring</code> ( <em><code>bits</code></em> <code>bit</code> \[ <code>FROM</code> <em><code>start</code></em> <code>integer</code> ] \[ <code>FOR</code> <em><code>count</code></em> <code>integer</code> ] ) → <code>bit</code></p><p>Extracts the substring of <em><code>bits</code></em> starting at the <em><code>start</code></em>'th bit if that is specified, and stopping after <em><code>count</code></em> bits if that is specified. Provide at least one of <em><code>start</code></em> and <em><code>count</code></em>.</p><p><code>substring(B'110010111111' from 3 for 2)</code> → <code>00</code></p>                                                                                                                            |
| <p><code>get\_bit</code> ( <em><code>bits</code></em> <code>bit</code>, <em><code>n</code></em> <code>integer</code> ) → <code>integer</code></p><p>Extracts <em><code>n</code></em>'th bit from bit string; the first (leftmost) bit is bit 0.</p><p><code>get\_bit(B'101010101010101010', 6)</code> → <code>1</code></p>                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| <p><code>set\_bit</code> ( <em><code>bits</code></em> <code>bit</code>, <em><code>n</code></em> <code>integer</code>, <em><code>newvalue</code></em> <code>integer</code> ) → <code>bit</code></p><p>Sets <em><code>n</code></em>'th bit in bit string to <em><code>newvalue</code></em>; the first (leftmost) bit is bit 0.</p><p><code>set\_bit(B'101010101010101010', 6, 0)</code> → <code>101010001010101010</code></p>                                                                                                                                                                                                                                                                                                                                |

<br>

In addition, it is possible to cast integral values to and from type `bit`. Casting an integer to `bit(n)` copies the rightmost `n` bits. Casting an integer to a bit string width wider than the integer itself will sign-extend on the left. Some examples:

```

44::bit(10)                    0000101100
44::bit(3)                     100
cast(-44 as bit(12))           111111010100
'1110'::bit(4)::integer        14
```

Note that casting to just “bit” means casting to `bit(1)`, and so will deliver only the least significant bit of the integer.

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/functions-bitstring.html)（英文原文，待翻譯）


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.postgresql.tw/the-sql-language/functions/functions-bitstring.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
