Re: BUG #16403: set_bit function does not have expected effect

Поиск
Список
Период
Сортировка
От Francisco Olarte
Тема Re: BUG #16403: set_bit function does not have expected effect
Дата
Msg-id CA+bJJbzyLq9wHynta=NFR2gWOq3ZdJY1frBoCqNjr76+HupVMA@mail.gmail.com
обсуждение исходный текст
Ответ на BUG #16403: set_bit function does not have expected effect  (PG Bug reporting form <noreply@postgresql.org>)
Ответы Re: BUG #16403: set_bit function does not have expected effect  (Tom Lane <tgl@sss.pgh.pa.us>)
Список pgsql-bugs
On Thu, Apr 30, 2020 at 9:47 AM PG Bug reporting form
<noreply@postgresql.org> wrote:
> Input:
> SELECT set_bit('\x00000000'::bytea, 0, 1);
> Expected Output:
> '\x00000001' or '\x80000000'
> Actual Output:
> '\x01000000'
>
> Input:
> SELECT set_bit('\x00000000'::bytea, 8, 1);
> Expected Output:
> '\x00000100' or '\x00800000'
> Actual Output:
> '\x00010000'
>
> Issue:
> set_bit function changes the right-most bit of the byte, but with
> little-endian byte order. This is confusing to any use case where setting a
> bit in a BYTEA in a specific position. To iterate through the bits within
> the BYTEA, one must have nested loops which set bits within byte boundaries.

I think this is a display expectations issue, when using addresses it
seems it is changing the least significant bit in the first byte and
the second byte ( in C-speak it is setting bit n%8 byte n/8 ), the
problem is the hex display of a byte list ( file, C array, bytea ) in
customarily is done by printing the bytes left to right, and the
single byte display displays most significant nibble first in hex. It
would be even weirder if the bytes where printed in binary, but it's
been done this way in many programs for ages, so I think your
expectation is faulty here. AAMOF if it printed any of your expected
outputs I, and I suspect many more people, would be unpleasantly
surprised.

Also, if you had done set_bit(0,2) it would print "02000000", which
according to your description should be to the left of 01000000, how
do you order 02000000 and 01000000 left or right ? The problem is
right-left do not apply well here, the bits in registers/memory/disks
are not aligned left/right, or top/down or front/back, the most you
can do is talk about addresses, left-right only applies when you place
a picture of the thing, or print it on a screen. If you print the
"bits" one by one by using a get_bit loop you'll get your desired
order ( in any way, depending on wheter you seep hi to lo or lo to hi
offset and wheter your terminal prints left to right, right to left,
or top to bottom, or... ).

Francisco Olarte.



В списке pgsql-bugs по дате отправления:

Предыдущее
От: PG Bug reporting form
Дата:
Сообщение: BUG #16404: PostgreSQL 12.2: Database restore does not accept the EEST time zone.
Следующее
От: "断桥烟雨三两月"
Дата:
Сообщение: report bug