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

From: Bill Moran <wmoran(at)potentialtech(dot)com>
To: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Shouldn't B'1' = 1::bit be true?
Date: 2004-03-10 00:31:06
Message-ID: 404E61CA.8050908@potentialtech.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-hackers

Stephan Szabo wrote:
> On Wed, 3 Mar 2004, Bill Moran wrote:
>
>
>>Am I missing something here?
>>
>>agelis=# select B'1'::int;
>> int4
>>------
>> 1
>>(1 row)
>>
>>agelis=# select 1::bit;
>> bit
>>-----
>> 0
>>(1 row)
>>
>>agelis=# select B'1'=1;
>> ?column?
>>----------
>> f
>>(1 row)
>>
>>Last I checked, binary 1 and integer 1 were equal. Is this a bug, or is there
>>a reason for this that I need educated on? Why does int 1 cast to binary 0?
>
> Hmm, on my 7.4 machine, I get an error saying that there's no = operator
> for bit and integer. If I cast the B'1' into an integer I get a true
> result.
>
> The reason for 1::bit being 0 is that int4 -> bit(n) casts cast to a 32
> bit bit string. IIRC, when bit strings are truncated for size in casts,
> the spec says to take from the left.

Yeah. I made some changes to the casting config, so that int to bit is
cast automatically, instead of requiring an explicit cast. It made the
code I was converting easier to manage.

I dont' know why my original email took so long to hit the list, but I did
find out the same thing you just described. In the end I ended up converting
all the bit functions to int, so that I don't have to deal with the casting
pecularities, which just seemed like the easiest solution at the time.

--
Bill Moran
Potential Technologies
http://www.potentialtech.com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Simon Windsor 2004-03-10 00:42:18 How do I change column details
Previous Message Tom Lane 2004-03-09 23:43:58 Re: Shouldn't B'1' = 1::bit be true?

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2004-03-10 00:33:18 Re: How to get Relation name from Oid ??
Previous Message Tom Lane 2004-03-10 00:14:20 Re: pgFoundry WAS: On pgweb project