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

From: Francisco Olarte <folarte(at)peoplecall(dot)com>
To: amovitz(at)bncpu(dot)com, PostgreSQL mailing lists <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: BUG #16403: set_bit function does not have expected effect
Date: 2020-04-30 09:18:27
Message-ID: CA+bJJbzyLq9wHynta=NFR2gWOq3ZdJY1frBoCqNjr76+HupVMA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Thu, Apr 30, 2020 at 9:47 AM PG Bug reporting form
<noreply(at)postgresql(dot)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.

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message 断桥烟雨三两月 2020-04-30 11:36:41 report bug
Previous Message PG Bug reporting form 2020-04-30 08:05:05 BUG #16404: PostgreSQL 12.2: Database restore does not accept the EEST time zone.