Currently the behavior of bit-string extensions is pretty insane.
SELECT b'01'::bit(2)::bit(4),
b'01'::bit(2)::int::bit(4);
bit | bit
------+------
0100 | 0001
(1 row)
I'd like propose we standardize this a bit. Previously, in version 8 compatibility was broke. From the Version 8 release notes (thanks to Rhodium Toad for the research),
> Casting an integer to BIT(N) selects the rightmost N bits of the integer, not the leftmost N bits as before.
Everything should select the right-most bits, and extend the left-most bits from the docs:
> Casting an integer to a bit string width wider than the integer itself will sign-extend on the left.
That makes sense to me. Intergers sign-extend on the left, and the behavior is currently undefined for bit->bit extensions. What say you?