Re: [GENERAL] Shouldn't B'1' = 1::bit be true?

From: "Thomas Swan" <tswan(at)idigx(dot)com>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: "Bill Moran" <wmoran(at)potentialtech(dot)com>, pgsql-general(at)postgresql(dot)org, pgsql-hackers(at)postgresql(dot)org
Subject: Re: [GENERAL] Shouldn't B'1' = 1::bit be true?
Date: 2004-03-10 18:45:13
Message-ID: 54173.199.222.14.2.1078944313.squirrel@www.idigx.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

<quote who="Tom Lane">
> Bill Moran <wmoran(at)potentialtech(dot)com> writes:
>> Am I missing something here?
>
> Hmm. It seems like int-to-bit casting ought to be aware of the
> bit-width one is casting to, and take that number of bits from
> the right end of the integer. This would make it be the inverse
> of the other direction. Right now it's only an inverse when you
> cast to and from bit(32). For shorter bitfield widths, we're
> effectively inserting at the right end of the integer, but removing
> bits from the left, which is not consistent.
>
> regression=# select B'11000'::bit(5)::int;
> int4
> ------
> 24
> (1 row)
>
> regression=# select 24::int::bit(32);
> bit
> ----------------------------------
> 00000000000000000000000000011000
> (1 row)
>
> regression=# select 24::int::bit(32)::bit(5);
> bit
> -------
> 00000
> (1 row)
>
> regression=# select 24::int::bit(5);
> bit
> -------
> 00000
> (1 row)
>
> If we made int-to-bit-N take the rightmost N bits, then the last two
> cases would yield different results, but that doesn't seem unreasonable
> to me. Or at least it's less unreasonable than bit(5)-to-int not being
> the inverse of int-to-bit(5).
>
> Comments?
>

Tom,

I would suggest looking at char to int to char conversions for correctness
examples. This can be looked at as bit(8)::bit(32)::bit(8) operations.

To convert low bits ot high bits you pad 0 bits on the left. To convert
from high to low you strip bits off the left hand side. This allows
reasonable behavior.

Coverting from low precision to high precision and back to low precision
should be lossless for bits. High to low to high should be lossy for
bits because you lost bits in the smaller container.

So the conversion should be symmetric for values that fit in the bitspace.
Even the MS calculator does this. 65534::bit(8)::int should yield 254
just as 254::int::bit(8) should be 254 and 254::int::bit(32)::bit(8)::int
should yield 254.

I would say the current way of handling bit conversions is broken. I for
one would like to see the bitwise behavior be more correct. It's almost
as if the bit(32)::bit(8) operation is thinking of string truncation. Is
this endian related?

Thomas

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2004-03-10 19:06:00 Re: [HACKERS] Shouldn't B'1' = 1::bit be true?
Previous Message Tom Lane 2004-03-10 18:40:40 Re: postgres FROM clause problem

Browse pgsql-hackers by date

  From Date Subject
Next Message Zeugswetter Andreas SB SD 2004-03-10 18:58:04 Re: PITR Functional Design v2 for 7.5
Previous Message Ramanujam H S Iyengar 2004-03-10 17:58:51 How to get RelationName ??