Re: confused about bit strings

From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: Neil Conway <neilc(at)samurai(dot)com>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: confused about bit strings
Date: 2003-10-08 20:30:35
Message-ID: Pine.LNX.4.44.0310082227460.26413-100000@peter.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Neil Conway writes:

> nconway=# select 1::bit;
> bit
> -----
> 0
> (1 row)

Oops. I've always thought that casting between int and bit should be
disallowed, but apparently it keeps sneaking back in.

> nconway=# select X'4'::bit varying;
> varbit
> --------
> 0100
> (1 row)
> -- why is that 4 bits, not 3?

SQL says so:

12) The declared type of a <hex string literal> is fixed-length
bit string. Each <hexit> appearing in the literal is equivalent
to a quartet of bits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C,
D, E, and F are interpreted as 0000, 0001, 0010, 0011, 0100,
0101, 0110, 0111, 1000, 1001, 1010, 1011, 1100, 1101, 1110,
and 1111, respectively. The <hexit>s a, b, c, d, e, and f have
respectively the same values as the <hexit>s A, B, C, D, E, and
F.

> nconway=# select "bit"('14'::int);
> bit
> ----------------------------------
> 00000000000000000000000000001110
> (1 row)
> -- shouldn't bit be equivalent to bit(1), which should be
> right-truncated?

It is, but here you're calling a function, not referring to the type.

--
Peter Eisentraut peter_e(at)gmx(dot)net

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Bob Badour 2003-10-08 21:43:04 Re: Dreaming About Redesigning SQL
Previous Message Vivek Khera 2003-10-08 19:58:03 Re: PostgreSQL vs. MySQL