Glib::Flags boolean operators (was Re: Glib::IO->add_watch not uninstalling)
- From: muppet <scott asofyet org>
- To: gtk2-perl List <gtk-perl-list gnome org>
- Subject: Glib::Flags boolean operators (was Re: Glib::IO->add_watch not uninstalling)
- Date: Sun, 25 Feb 2007 20:38:07 -0500
On Feb 25, 2007, at 9:46 AM, Jeffrey Ratcliffe wrote:
On 25/02/07, muppet <scott asofyet org> wrote:
Well, TIMTOWDI, of course. :-) But, yes, the ">=" operator, which is
overloaded for Glib::Flags bitsets to mean the same as "&", which is
"are all of the flags listed on the right hand side set in the left
hand side?" is how i would write it.
I can see that &, >= and * should be the same for this example.
The Glib::Flags operators are explained in the "This Is Now That"
section of the Glib manpage.
But in the manpage, it implies (to me at least) that there is a
difference between &, >= and *. Is there difference?
For a boolean test, no. & and >= are implemented differently, but *
and & are the same. The names (and operators) are derived from set
theory, but you can also use any knowledge of binary logic operations
from C.
(Warning: potentially redundant but intended-to-be complete
information follows.)
Glib.pm contains this:
package Glib::Flags;
use overload
'bool' => \&bool,
'+' => \&union, '|' => \&union,
'-' => \&sub,
'>=' => \&ge,
'==' => \&eq, 'eq' => \&eq, # eq for is_deeply in
Test::More
'*' => \&intersect, '&' => \&intersect,
'/' => \&xor, '^' => \&xor,
'@{}' => \&as_arrayref,
'""' => sub { "[ @{$_[0]} ]" },
fallback => 1;
Those subs are implemented essentially like this:
bool := NOT NOT a
if any of the bits in a are set, the expression evaluates to TRUE
union := a OR b
the result contains all of the bits set in a and b
that is, the result is the union of the two sets.
the | mnemonic is for the C (and Perl) bitwise OR operator.
the + mnemonic is for union of two sets -- you're adding them
together.
sub := a AND NOT b
the result contains all the bits set in a minus the bits that
were also on in b.
this is subtracting the set of b from the set of a.
the - mnemonic is awesome, because "a & ~b" is bizarre looking.
ge := (a AND b) IS b
the result is TRUE if the bits in b are on in a.
other bits may be on in a, but they are ignored (by the AND).
hence, this is true if the set of a is greater than or equal
to the set of b.
eq := a IS b
the result is TRUE iff the same set of bits are on in both.
intersect := a AND b
the result contains all of the bits that are on in both a and b.
that is, it returns the intersection of sets a and b.
the & mnemonic is the C (and Perl) bitwise AND operator.
the * mnemonic is for the intersection of two sets.
xor := a XOR b
the result contains all of the bits that are on in either or
both a and b.
the bits that are on in both are not included; this is the
exclusive or of the two sets.
the ^ mnemonic is for the C (and Perl) bitwise XOR operator.
All that said, i think the only ones i have ever used are the >= and
bool operators.
--
If the monkey could type one keystroke every nanosecond, the expected
waiting time until the monkey types out Hamlet is so long that the
estimated age of the universe is insignificant by comparison ... this
is not a practical method for writing plays.
-- Gian-Carlo Rota
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]