Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
9.6. Bit String Functions and Operators
Prev UpChapter 9. Functions and OperatorsHome Next

9.6. Bit String Functions and Operators#

This section describes functions and operators for examining and manipulating bit strings, that is values of the typesbit andbit varying. (While only typebit is mentioned in these tables, values of typebit varying can be used interchangeably.) Bit strings support the usual comparison operators shown inTable 9.1, as well as the operators shown inTable 9.14.

Table 9.14. Bit String Operators

Operator

Description

Example(s)

bit||bitbit

Concatenation

B'10001' || B'011'10001011

bit&bitbit

Bitwise AND (inputs must be of equal length)

B'10001' & B'01101'00001

bit|bitbit

Bitwise OR (inputs must be of equal length)

B'10001' | B'01101'11101

bit#bitbit

Bitwise exclusive OR (inputs must be of equal length)

B'10001' # B'01101'11100

~bitbit

Bitwise NOT

~ B'10001'01110

bit<<integerbit

Bitwise shift left (string length is preserved)

B'10001' << 301000

bit>>integerbit

Bitwise shift right (string length is preserved)

B'10001' >> 200100


Some of the functions available for binary strings are also available for bit strings, as shown inTable 9.15.

Table 9.15. Bit String Functions

Function

Description

Example(s)

bit_count (bit ) →bigint

Returns the number of bits set in the bit string (also known aspopcount).

bit_count(B'10111')4

bit_length (bit ) →integer

Returns number of bits in the bit string.

bit_length(B'10111')5

length (bit ) →integer

Returns number of bits in the bit string.

length(B'10111')5

octet_length (bit ) →integer

Returns number of bytes in the bit string.

octet_length(B'1011111011')2

overlay (bitsbitPLACINGnewsubstringbitFROMstartinteger [FORcountinteger] ) →bit

Replaces the substring ofbits that starts at thestart'th bit and extends forcount bits withnewsubstring. Ifcount is omitted, it defaults to the length ofnewsubstring.

overlay(B'01010101010101010' placing B'11111' from 2 for 3)0111110101010101010

position (substringbitINbitsbit ) →integer

Returns first starting index of the specifiedsubstring withinbits, or zero if it's not present.

position(B'010' in B'000001101011')8

substring (bitsbit [FROMstartinteger] [FORcountinteger] ) →bit

Extracts the substring ofbits starting at thestart'th bit if that is specified, and stopping aftercount bits if that is specified. Provide at least one ofstart andcount.

substring(B'110010111111' from 3 for 2)00

get_bit (bitsbit,ninteger ) →integer

Extractsn'th bit from bit string; the first (leftmost) bit is bit 0.

get_bit(B'101010101010101010', 6)1

set_bit (bitsbit,ninteger,newvalueinteger ) →bit

Setsn'th bit in bit string tonewvalue; the first (leftmost) bit is bit 0.

set_bit(B'101010101010101010', 6, 0)101010001010101010


In addition, it is possible to cast integral values to and from typebit. Casting an integer tobit(n) copies the rightmostn 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)000010110044::bit(3)100cast(-44 as bit(12))111111010100'1110'::bit(4)::integer14

Note that casting to justbit means casting tobit(1), and so will deliver only the least significant bit of the integer.


Prev Up Next
9.5. Binary String Functions and Operators Home 9.7. Pattern Matching
pdfepub
Go to Postgres Pro Standard 17
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp