9.6. Функции и операторы для работы с битовыми строками
В этом разделе описываются функции и операторы, предназначенные для работы с битовыми строками, то есть с данными типов bit
и bit varying
. Помимо обычных операторов сравнения, с такими данными можно использовать операторы, перечисленные в Таблице 9.13. Заметьте, что операторы &
, |
и #
работают только с двоичными строками одинаковой длины. Операторы побитового сдвига сохраняют длины исходных строк, как показано в примерах.
Таблица 9.13. Операторы для работы с битовыми строками
Оператор | Описание | Пример | Результат |
---|---|---|---|
|| | конкатенация | B'10001' || B'011' | 10001011 |
& | битовый AND | B'10001' & B'01101' | 00001 |
| | битовый OR | B'10001' | B'01101' | 11101 |
# | битовый XOR | B'10001' # B'01101' | 11100 |
~ | битовый NOT | ~ B'10001' | 01110 |
<< | битовый сдвиг влево | B'10001' << 3 | 01000 |
>> | битовый сдвиг вправо | B'10001' >> 2 | 00100 |
Следующие функции языка SQL работают как с символьными, так и с битовыми строками:
, length
, bit_length
, octet_length
, position
, substring
.overlay
С битовыми и двоичными строками работают функции
и get_bit
. При работе с битовыми строками эти функции нумеруют биты слева направо и самый левый бит считается нулевым.set_bit
Кроме того, целые значения можно преобразовать в тип bit
и обратно. Например:
44::bit(10) 0000101100 44::bit(3) 100 cast(-44 as bit(12)) 111111010100 '1110'::bit(4)::integer 14
Заметьте, что приведение к типу «bit» без длины будет означать приведение к bit(1)
, и в результате будет получен только один менее значащий бит числа.
Примечание
Приведение целого числа к типу bit(n)
копирует правые n
бит числа. Если же целое преобразуется в битовую строку большей длины, чем требуется для этого числа, она дополняется слева битами знака числа.
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
. Aside from the usual comparison operators, the operators shown in Table 9.13 can be used. Bit string operands of &
, |
, and #
must be of equal length. When bit shifting, the original length of the string is preserved, as shown in the examples.
Table 9.13. Bit String Operators
Operator | Description | Example | Result |
---|---|---|---|
|| | concatenation | B'10001' || B'011' | 10001011 |
& | bitwise AND | B'10001' & B'01101' | 00001 |
| | bitwise OR | B'10001' | B'01101' | 11101 |
# | bitwise XOR | B'10001' # B'01101' | 11100 |
~ | bitwise NOT | ~ B'10001' | 01110 |
<< | bitwise shift left | B'10001' << 3 | 01000 |
>> | bitwise shift right | B'10001' >> 2 | 00100 |
The following SQL-standard functions work on bit strings as well as character strings:
, length
, bit_length
, octet_length
, position
, substring
. overlay
The following functions work on bit strings as well as binary strings:
, get_bit
. When working with a bit string, these functions number the first (leftmost) bit of the string as bit 0. set_bit
In addition, it is possible to cast integral values to and from type bit
. 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.
Note
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.