Updated on 2025-10-23 GMT+08:00

Bit String Types

Bit strings are strings consisting of 1 and 0. They can be used to store bit masks. Table 1 lists the bit string types supported by M-compatible databases.

Table 1 Bit string types

Name

Description

Storage Space

BIT[(n)]

Bit strings are strings consisting of 1 and 0. They can be used to store bit masks. n indicates the bit string length. The value range is [1,64]. If it is not specified, the default precision 1 is used.

Up to 64 bytes

  • 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.
  • If the bit string value is output, the bit string value of the original data is output as a character string. See Example.
  • The BIT type supports bit string input and common character string input. See Example.
  • If the value of sql_mode contains strict_trans_tables and the input value is invalid or out of the value range, an error is reported. If strict_trans_tables is not contained and the input value is invalid or out of the value range, the maximum value within the specified length range is stored.
  • When setBytes or setBinaryStream is used to insert data into a BIT column in the JDBC&&PBE scenario, if the byte stream is not 4-byte-aligned, the data fails to be inserted and an error is reported because the data is incomplete in the M-compatible database. Replace the setBytes method with the setString method.

Example

Bit string types are displayed using character strings.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
-- Create a table.
m_db=# CREATE TABLE bit_type_t1
(
    BIT_COL BIT(64)
);

-- Insert data.
m_db=# INSERT INTO bit_type_t1 VALUES (b'101');

-- Query data in the table.
m_db=# SELECT *  FROM bit_type_t1;
 bit_col 
---------
 101
(1 row)

-- Drop the table.
m_db=# DROP TABLE bit_type_t1;

-- Create a table.
m_db=# CREATE TABLE bit_type_t2
(
    BIT_COL BIT(3)
);

-- With the strict mode enabled, an error is reported if the length of an inserted string exceeds the length specified for the type.
m_db=# SET SQL_MODE = 'strict_trans_tables,only_full_group_by,no_zero_in_date,no_zero_date,error_for_division_by_zero,
no_auto_create_user,no_engine_substitution';
SET
m_db=# INSERT INTO bit_type_t2 VALUES (b'10101');
ERROR:  bit string too long for type bit varying(3)
CONTEXT:  referenced column: bit_col

-- With the strict mode disabled, a warning message is reported and the maximum value within the specified length range is inserted if an inserted string exceeds the length specified for the type.
m_db=# SET SQL_MODE = '';
SET
m_db=# INSERT INTO bit_type_t2 VALUES (b'10101');
WARNING:  bit string too long for type bit varying(3)
CONTEXT:  referenced column: bit_col
INSERT 0 1

-- Query data in the table.
m_db=# SELECT * FROM bit_type_t2;
 bit_col 
---------
 111
(1 row)

-- Drop the table.
m_db=# DROP TABLE bit_type_t2;