Subject | How to invert a bitmask? |
---|---|
Author | Michael Ludwig |
Post date | 2010-05-10T00:39:16Z |
I'm using an INTEGER column as a bitfield, and I'd like to manipulate
it via stored procedures. I'm using FB 2.1 and I've found the following
useful functions in the release notes (operators such as in Perl or
other languages in parentheses):
* BIN_AND ( & )
* BIN_OR ( | )
* BIN_SHL ( << )
* BIN_SHR ( >> )
* BIN_XOR ( ^ )
All well. However, there's one thing I'm missing: the bit inversion
operator, which inverts all bits in a number ( ~ in Perl):
$ perl -lwe 'print $_, " ", ~ $_ for reverse 0 .. 2'
2 4294967293
1 4294967294
0 4294967295
Using the inversion operator I can easily produce a number with all bits
( ~ 0 ) or all but one bit set ( ~ (1 << $_) ):
$ perl -lwe 'print ~ (1 << $_), " ", 1 << $_ for 0 .. 5'
4294967294 1
4294967293 2
4294967291 4
4294967287 8
4294967279 16
4294967263 32
How can I invert a bitmask in Firebird/PSQL (such as with ~ in Perl)?
--
Michael Ludwig
it via stored procedures. I'm using FB 2.1 and I've found the following
useful functions in the release notes (operators such as in Perl or
other languages in parentheses):
* BIN_AND ( & )
* BIN_OR ( | )
* BIN_SHL ( << )
* BIN_SHR ( >> )
* BIN_XOR ( ^ )
All well. However, there's one thing I'm missing: the bit inversion
operator, which inverts all bits in a number ( ~ in Perl):
$ perl -lwe 'print $_, " ", ~ $_ for reverse 0 .. 2'
2 4294967293
1 4294967294
0 4294967295
Using the inversion operator I can easily produce a number with all bits
( ~ 0 ) or all but one bit set ( ~ (1 << $_) ):
$ perl -lwe 'print ~ (1 << $_), " ", 1 << $_ for 0 .. 5'
4294967294 1
4294967293 2
4294967291 4
4294967287 8
4294967279 16
4294967263 32
How can I invert a bitmask in Firebird/PSQL (such as with ~ in Perl)?
--
Michael Ludwig