> 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/19/the-sql-language/functions/functions-bitstring.md). Agents can also search and read this documentation through its [MCP server](https://docs.postgresql.tw/~gitbook/mcp).

# 9.6. 位元字串函式與運算子 \#

### 9.6. 位元字串函式與運算子 [#](#functions-bitstring) <a href="#functions-bitstring" id="functions-bitstring"></a>

本節說明用於檢查與操作位元字串的函式與運算子，也就是 `bit` 與 `bit varying` 型別的值。（雖然這些表格中只提到 `bit` 型別，但 `bit varying` 型別的值也可以互換使用。）位元字串支援[表 9.1](https://docs.postgresql.tw/19/the-sql-language/functions/functions-comparison.md#functions-comparison-table) 所列的一般比較運算子，以及[表 9.14](#functions-bit-string-op-table) 所列的運算子。

#### 表 9.14. 位元字串運算子 [#](#functions-bit-string-op-table) <a href="#functions-bit-string-op-table" id="functions-bit-string-op-table"></a>

| <p>運算子</p><p>說明</p><p>範例</p>                                                                                                                                 |                                                                                      |                                                                           |   |                                           |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------ | ------------------------------------------------------------------------- | - | ----------------------------------------- |
| <p><code>bit</code> <code>                                                                                                                                   |                                                                                      | </code> <code>bit</code> → <code>bit</code></p><p>串接</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>位元 AND（輸入必須等長）</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>位元 OR（輸入必須等長）</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>位元互斥 OR（輸入必須等長）</p><p><code>B'10001' # B'01101'</code> → <code>11100</code></p> |                                                                                      |                                                                           |   |                                           |
| <p><code>~~</code> <code>bit</code> → <code>bit</code></p><p>位元 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>位元左移（字串長度保持不變）</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>位元右移（字串長度保持不變）</p><p><code>B'10001' >> 2</code> → <code>00100</code></p>   |                                                                                      |                                                                           |   |                                           |

<br>

有些可用於二進位字串的函式，也可以用於位元字串，如[表 9.15](#functions-bit-string-table) 所示。

#### 表 9.15. 位元字串函式 [#](#functions-bit-string-table) <a href="#functions-bit-string-table" id="functions-bit-string-table"></a>

| <p>函式</p><p>說明</p><p>範例</p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| <p><code>bit\_count</code> ( <code>bit</code> ) → <code>bigint</code></p><p>回傳位元字串中被設定的位元數（也稱為 “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>回傳位元字串中的位元數。</p><p><code>bit\_length(B'10111')</code> → <code>5</code></p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                |
| <p><code>length</code> ( <code>bit</code> ) → <code>integer</code></p><p>回傳位元字串中的位元數。</p><p><code>length(B'10111')</code> → <code>5</code></p>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| <p><code>octet\_length</code> ( <code>bit</code> ) → <code>integer</code></p><p>回傳位元字串中的位元組數。</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>將 <em><code>bits</code></em> 中從第 <em><code>start</code></em> 個位元開始、延伸 <em><code>count</code></em> 個位元的子字串，替換為 <em><code>newsubstring</code></em>。如果省略 <em><code>count</code></em>，預設為 <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>回傳指定的 <em><code>substring</code></em> 在 <em><code>bits</code></em> 中第一次出現的起始索引；如果不存在則回傳零。</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>擷取 <em><code>bits</code></em> 的子字串：如果有指定，就從第 <em><code>start</code></em> 個位元開始；如果有指定，就在 <em><code>count</code></em> 個位元之後停止。<em><code>start</code></em> 與 <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>從位元字串中擷取第 <em><code>n</code></em> 個位元；第一個（最左邊的）位元是第 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>將位元字串中的第 <em><code>n</code></em> 個位元設為 <em><code>newvalue</code></em>；第一個（最左邊的）位元是第 0 個位元。</p><p><code>set\_bit(B'101010101010101010', 6, 0)</code> → <code>101010001010101010</code></p>                                                                                                                                                                                                                                                                          |

<br>

此外，整數值可以與 `bit` 型別互相轉換。將整數轉換為 `bit(n)` 時，會複製最右邊的 `n` 個位元。將整數轉換為比整數本身更寬的位元字串時，會在左邊進行符號延伸。一些範例：

```

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

請注意，只轉換為「bit」表示轉換為 `bit(1)`，因此只會得到整數的最低有效位元。

***

原文：[PostgreSQL 18.6 Documentation](https://www.postgresql.org/docs/18/functions-bitstring.html)（原文版本：18.6；核對日期：2026-09-11）


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
