Bit testing

Bit testing

am 12.11.2010 08:27:26 von Chap Harrison

I'm almost embarrassed to ask this, but I can't figure out a simple way =
to construct a switch ('given') statement where the 'when' clauses =
involve bit-testing.

Here's the only way I've figured out to build a switch statement that =
does the trick. It seems unusually wordy, which makes me think there =
must be a simpler way to test for certain bit combinations. Any =
suggestions?

Thanks,
Chap

#!/usr/bin/perl =
=
=20

use strict;
use warnings;
use feature ":5.10";

# Here are masks for various bit combos of interest: =
=
=20

my $one_three =3D 0b00001010; # bits 1 and 3 (counting from 0, =
right to left) =
=20
my $zero_four =3D 0b00010001; # bits 0 and 4 =
=
=20
my $five =3D 0b00100000; # bit 5 =
=
=20

# Here we will test several bit fields for bit combos of interest: =
=
=20

for my $flags ( 0b10111010, 0b10111000, 0b10010010) {

my $asbits =3D sprintf("0b%08b", $flags); # prepare bits for =
pretty-printing =
=20

given ( $flags ) {
when ( ($_ & $one_three) == $one_three ) { # bits one and =
three are on =
=20
say "$asbits has bits 1 and 3";
}
when ( ($_ & $zero_four) == $zero_four ) { # bits zero and =
four are on =
=20
say "$asbits has bits 0 and 4";
}
when ( ($_ & $five) == $five ) { # bit five is on =
=
=20
say "$asbits has bit 5";
}
default {
say "$asbits has no interesting bit patterns.";
}
}
}



--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Bit testing

am 12.11.2010 08:56:46 von Uri Guttman

>>>>> "CH" == Chap Harrison writes:

CH> I'm almost embarrassed to ask this, but I can't figure out a
CH> simple way to construct a switch ('given') statement where the
CH> 'when' clauses involve bit-testing. Here's the only way I've
CH> figured out to build a switch statement that does the trick. It
CH> seems unusually wordy, which makes me think there must be a
CH> simpler way to test for certain bit combinations. Any
CH> suggestions?

CH> when ( ($_ & $one_three) == $one_three ) { # bits one and
CH> three are on

don't put comments on the same line as code. it can make it line wrap
like above.

anyhow, some boolean hacking does the trick. this seems to work.


when ( !(~$_ & $one_three) )

you invert the flag bits and then and against the test mask. if that is all
0's (tested with !) then your bits were set.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Bit testing

am 14.11.2010 00:47:36 von derykus

T24gTm92IDExLCAxMToyN6BwbSwgYy4uLkBwb2JveC5jb20gKENoYXAgSGFy cmlzb24pIHdyb3Rl
Ogo+IEknbSBhbG1vc3QgZW1iYXJyYXNzZWQgdG8gYXNrIHRoaXMsIGJ1dCBJ IGNhbid0IGZpZ3Vy
ZSBvdXQgYSBzaW1wbGUgd2F5IHRvIGNvbnN0cnVjdCBhIHN3aXRjaCAoJ2dp dmVuJykgc3RhdGVt
ZW50IHdoZXJlIHRoZSAnd2hlbicgY2xhdXNlcyBpbnZvbHZlIGJpdC10ZXN0 aW5nLgo+Cj4gSGVy
ZSdzIHRoZSBvbmx5IHdheSBJJ3ZlIGZpZ3VyZWQgb3V0IHRvIGJ1aWxkIGEg c3dpdGNoIHN0YXRl
bWVudCB0aGF0IGRvZXMgdGhlIHRyaWNrLiCgSXQgc2VlbXMgdW51c3VhbGx5 IHdvcmR5LCB3aGlj
aCBtYWtlcyBtZSB0aGluayB0aGVyZSBtdXN0IGJlIGEgc2ltcGxlciB3YXkg dG8gdGVzdCBmb3Ig
Y2VydGFpbiBiaXQgY29tYmluYXRpb25zLiCgQW55IHN1Z2dlc3Rpb25zPwo+ Cj4gVGhhbmtzLAo+
IENoYXAKPgo+ICMhL3Vzci9iaW4vcGVybCCgIKAgoCCgIKAgoCCgIKAgoCCg IKAgoCCgIKAgoCCg
IKAgoCCgIKAgoCCgIKAgoCCgIKAgoCCgIKAgoCCgIKAgoCCgIKAgoCCgIKAg oCCgIKAgoCCgIKAg
oCCgIKAgoCCgIKAgoCCgIKAgoCCgIKAgoCCgIKA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PiCgIKAgoCCgIH0KPiCgIKAgfQo+Cj4gfQoKTm90IGxvdHMgc2hvcnRlciBi dXQgeW91IGNvdWxk
IHVzZSBhIGNsb3N1cmUgdG8gaGlkZQp0aGUgY2FsY3VsYXRpb246CgpteSAk bWFzazsKZm9yIG15
ICRmbGFncyAoIC4uLiApIHsKICAgICAkbWFzayA9IHN1YiB7IHJldHVybiAo JGZsYWdzICYgJF9b
MF0pID09ICRfWzBdIH0KICAgICAgICAgICAgdW5sZXNzICRtYXNrOwogICAg IGdpdmVuKCAkZmxh
Z3MgKSB7CiAgICAgICAgICAgIHdoZW4gKCAkbWFzay0+KCRvbmVfYW5kX3Ro cmVlKSAgKSB7IC4u
LiB9CiAgICAgICAgICAgIHdoZW4gKCAkbWFzay0+KCR6ZXJvX2FuZF9mb3Vy KSAgICkgeyAuLi4g
fQogICAgICAgICAgICAuLi4KICAgICB9Cn0KCi0tCkNoYXJsZXMgRGVSeWt1 cwo=

Re: Bit testing

am 14.11.2010 10:11:13 von Shlomi Fish

Hi Charles,

On Sunday 14 November 2010 01:47:36 C.DeRykus wrote:
> On Nov 11, 11:27 pm, c...@pobox.com (Chap Harrison) wrote:
> Not lots shorter but you could use a closure to hide
> the calculation:
>
> my $mask;
> for my $flags ( ... ) {
> $mask = sub { return ($flags & $_[0]) == $_[0] }
> unless $mask;
> given( $flags ) {
> when ( $mask->($one_and_three) ) { ... }
> when ( $mask->($zero_and_four) ) { ... }
> ...
> }
> }
>

This won't work properly because the closure traps the initial value of
"$flags". For example:

[code]
#!/usr/bin/perl

use strict;
use warnings;

my $closure;

foreach my $name (qw(Sophie Jack Charles Dan Rachel))
{
$closure = sub { print "Hello $name!\n" ; } unless $closure;

$closure->();
}
[/code]

This prints "Hello Sophie!" five times. Either redeclare the closure on every
iteration, or declare it once while using a more outer lexical variable.

Regards,

Shlomi Fish

> --
> Charles DeRykus

--
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
List of Portability Libraries - http://shlom.in/port-libs

She's a hot chick. But she smokes.
She can smoke as long as she's smokin'.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Bit testing

am 14.11.2010 11:36:55 von derykus

T24gTm92IDEzLCAzOjQ3oHBtLCBkZXJ5Li4uQGdtYWlsLmNvbSAoIkMuRGVS eWt1cyIpIHdyb3Rl
Ogo+IE9uIE5vdiAxMSwgMTE6MjegcG0sIGMuLi5AcG9ib3guY29tIChDaGFw IEhhcnJpc29uKSB3
cm90ZToKPgo+Cj4KPiA+IEknbSBhbG1vc3QgZW1iYXJyYXNzZWQgdG8gYXNr IHRoaXMsIGJ1dCBJ
IGNhbid0IGZpZ3VyZSBvdXQgYSBzaW1wbGUgd2F5IHRvIGNvbnN0cnVjdCBh IHN3aXRjaCAoJ2dp
dmVuJykgc3RhdGVtZW50IHdoZXJlIHRoZSAnd2hlbicgY2xhdXNlcyBpbnZv bHZlIGJpdC10ZXN0
aW5nLgo+Cj4gPiBIZXJlJ3MgdGhlIG9ubHkgd2F5IEkndmUgZmlndXJlZCBv dXQgdG8gYnVpbGQg
YSBzd2l0Y2ggc3RhdGVtZW50IHRoYXQgZG9lcyB0aGUgdHJpY2suIKBJdCBz ZWVtcyB1bnVzdWFs
bHkgd29yZHksIHdoaWNoIG1ha2VzIG1lIHRoaW5rIHRoZXJlIG11c3QgYmUg YSBzaW1wbGVyIHdh
eSB0byB0ZXN0IGZvciBjZXJ0YWluIGJpdCBjb21iaW5hdGlvbnMuIKBBbnkg c3VnZ2VzdGlvbnM/
Cj4KPiA+IFRoYW5rcywKPiA+IENoYXAKPgo+ID4gIyEvdXNyL2Jpbi9wZXJs IKAgoCCgIKAgoCCg
IKAgoCCgIKAgoCCgIKAgoCCgIKAgoCCgIKAgoCCgIKAgoCCgIKAgoCCgIKAg oCCgIKAgoCCgIKAg
oCCgIKAgoCCgIKAgoCCgIKAgoCCgIKAg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ICAgICB3aGVuICggJG1hc2st Pigkb25lX2FuZF90
aHJlZSkgICkgeyAuLi4gfQogICAgICAgICAgICAgICB3aGVuICggJG1hc2st PigkemVyb19hbmRf
Zm91cikgICApIHsgLi4uIH0KICAgICAgICAgICAgICAgLi4uCiAgICAgIH0K Li4uCgogLS0KQ2hh
cmxlcyBEZVJ5a3Vz

Re: Bit testing

am 14.11.2010 17:10:57 von Chap Harrison

On Nov 14, 2010, at 4:36 AM, C.DeRykus wrote:

> And now it seems a little bit
> inelegant to redefine the closure each time through
> the loop.
>=20
>=20
> for my $flags ( ... ) {
> my $mask =3D sub { return ($flags & $_[0]) == $_[0] };
> given( $flags ) {
> when ( $mask->($one_and_three) ) { ... }
> when ( $mask->($zero_and_four) ) { ... }
> ...
> }
> ...

First, thanks for (($flags & $mask) == $mask).=20

What I eventually did was just call a subroutine on($flags, $mask) to do =
the calculation and determine whether all the specified bits were on. =
Seems simpler than the closure technique, even if having to repeatedly =
write '$flags' is less than ideal. =20

However, now I'm determined to get an understanding of closures, and I =
see another thread on this list that provides several links to help in =
that regard.

Thanks, one and all!

Regards,
Chap


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Re: Bit testing

am 14.11.2010 23:03:09 von derykus

On Nov 14, 1:11=A0am, shlo...@iglu.org.il (Shlomi Fish) wrote:
> Hi Charles,
>
> On Sunday 14 November 2010 01:47:36 C.DeRykus wrote:
>
> > On Nov 11, 11:27 pm, c...@pobox.com (Chap Harrison) wrote:
> > Not lots shorter but you could use a closure to hide
> > the calculation:
>
> > my $mask;
> > for my $flags ( ... ) {
> > =A0 =A0 =A0$mask =3D sub { return ($flags & $_[0]) == $_[0] }
> > =A0 =A0 =A0 =A0 =A0 =A0 unless $mask;
> > =A0 =A0 =A0given( $flags ) {
> > =A0 =A0 =A0 =A0 =A0 =A0 when ( $mask->($one_and_three) =A0) { ... }
> > =A0 =A0 =A0 =A0 =A0 =A0 when ( $mask->($zero_and_four) =A0 ) { ... }
> > =A0 =A0 =A0 =A0 =A0 =A0 ...
> > =A0 =A0 =A0}
> > }
>
> This won't work properly because the closure traps the initial value of
> "$flags". For example:
>
> [code]
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $closure;
>
> foreach my $name (qw(Sophie Jack Charles Dan Rachel))
> {
> =A0 =A0 $closure =3D sub { print "Hello $name!\n" ; } unless $closure;
>
> =A0 =A0 $closure->();}
>
> [/code]
>
> This prints "Hello Sophie!" five times. Either redeclare the closure on e=
very
> iteration, or declare it once while using a more outer lexical variable.

Right... or simply get rid of the statement qualifier
'unless $closure' which'll work too and is what you'll
have to do in any case.
(technically, you could declare 'my $closure' both
in/outside the loop leaving the statement qualifier
as is but that's horrible )

You could just 'my $closure' solely inside the loop
too.

Declaring once outside the loop with an outer lexical
seems less satisfactory since it loosens the 'tightest
lexical scope' best practice.

--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/