I have a varying bitmask field and I want to perform a bitwise AND on it.
PG::Error: ERROR: cannot AND bit strings of different sizes
SELECT "groups".* FROM "groups" WHERE (read_roles_bitmask = B'0' OR read_roles_bitmask & B'10' > B'0')
( you need to have bitmasks with varying lengths in your table to get this error. )
I'm expecting the bitwise math to look like the following: 00010 & 100000010 = 00010
I've also tried casting the bitmask to a integer with no luck.
Why does PostgreSQL choke on this?
How should I rewrite this query to play nicely?
I was able to use the following to get bitwise operators working: lpad(read_roles_bitmask::varchar,64,'0')::bigint
However this is limited to 64 bits, is there a better way?
&to left-extend the smaller operand with zeroes automatically.B'10'::bit(1)isB'1'andB'1'::bit(2)isB'10'so bits go left-to-right. I get the impression that the bit order changed somewhere along the line so perhaps they force you to be explicit to avoid version issues.read_roles_bitmask & B'10' > B'0'part, you just want to see if bit-2 (from right to left, 1-based counting) is1, right? If your bits when left-to-right then you could cast tobit(2)to grab just the 2 left-most bits.