Inconsistent ::bit(N) and get_bit()?

From: Boszormenyi Zoltan <zb(at)cybertec(dot)at>
To: PGSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Inconsistent ::bit(N) and get_bit()?
Date: 2010-08-11 13:48:52
Message-ID: 4C62AA44.6000503@cybertec.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

I just came across the following confusing thing.

zozo=# create table bit_test(i integer);
CREATE TABLE
zozo=# insert into bit_test values (1), (2), (3);
INSERT 0 3
zozo=# select i, i::bit(2), get_bit(i::bit(2), 1) as bit1,
get_bit(i::bit(2), 0) as bit0 from bit_test;
i | i | bit1 | bit0
---+----+------+------
1 | 01 | 1 | 0
2 | 10 | 0 | 1
3 | 11 | 1 | 1
(3 rows)

So, conversion from int to bitstring creates a readable bitstring, i.e.
the least significant bit is the rightmost one. But get_bit() on the same
bit string works in the opposite order. The only description about get_bit
I found in the 9.0beta docs are in
http://www.postgresql.org/docs/9.0/static/functions-binarystring.html#FUNCTIONS-BINARYSTRING-OTHER

-------------------------------------
Function Return Type Description Example Result

||
|get_bit|(string, offset) int Extract bit from string
get_bit(E'Th\\000omas'::bytea, 45) 1

-------------------------------------
||
and in
http://www.postgresql.org/docs/9.0/static/functions-bitstring.html

-------------------------------------
||The following functions work on bit strings as well as binary strings:
|get_bit|, |set_bit|.
-------------------------------------

Shouldn't it at least be documented in more depth? Say, get_bit(, N)
provides the Nth bit (0-based) counting from the leftmost bit?
I would certainly appreciate a warning spelled out about this
so if you convert a number to bitstring of length N and you want the
Mth bit (according to any programming language) then you need to use
get_bit(..., N-1-M).

Best regards,
Zoltán Böszörményi

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2010-08-11 13:55:07 Re: Regression tests versus the buildfarm environment
Previous Message Robert Haas 2010-08-11 13:46:04 Re: RecordTransactionCommit() and SharedInvalidationMessages