> 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/12/the-sql-language/data-types/bit-string-types.md).

# 8.10. 位元字串型別

Bit strings are strings of 1's and 0's. They can be used to store or visualize bit masks. There are two SQL bit types: `bit(`*`n`*) and `bit varying(`*`n`*), where *`n`* is a positive integer.

`bit` type data must match the length *`n`* exactly; it is an error to attempt to store shorter or longer bit strings. `bit varying` data is of variable length up to the maximum length *`n`*; longer strings will be rejected. Writing `bit` without a length is equivalent to `bit(1)`, while `bit varying` without a length specification means unlimited length.

## Note

If one explicitly casts a bit-string value to `bit(`*`n`*), it will be truncated or zero-padded on the right to be exactly *`n`* bits, without raising an error. Similarly, if one explicitly casts a bit-string value to `bit varying(`*`n`*), it will be truncated on the right if it is more than *`n`* bits.

Refer to [Section 4.1.2.5](https://www.postgresql.org/docs/12/sql-syntax-lexical.html#SQL-SYNTAX-BIT-STRINGS) for information about the syntax of bit string constants. Bit-logical operators and string manipulation functions are available; see [Section 9.6](https://www.postgresql.org/docs/12/functions-bitstring.html).

**Example 8.3. Using the Bit String Types**

```
CREATE TABLE test (a BIT(3), b BIT VARYING(5));
INSERT INTO test VALUES (B'101', B'00');
INSERT INTO test VALUES (B'10', B'101');

ERROR:  bit string length 2 does not match type bit(3)

INSERT INTO test VALUES (B'10'::bit(3), B'101');
SELECT * FROM test;

  a  |  b
-----+-----
 101 | 00
 100 | 101
```

A bit string value requires 1 byte for each group of 8 bits, plus 5 or 8 bytes overhead depending on the length of the string (but long values may be compressed or moved out-of-line, as explained in [Section 8.3](https://www.postgresql.org/docs/12/datatype-character.html) for character strings).


---

# 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/12/the-sql-language/data-types/bit-string-types.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.
