Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: bit array comparison

by tybalt89 (Prior)
on Oct 22, 2019 at 16:50 UTC ( #11107863=note: print w/replies, xml ) Need Help??


in reply to bit array comparison

#!/usr/bin/perl use common::sense; # https://perlmonks.org/?node_id=11107846 my $a = 0; my $b = 0; $a |= 1 << 0; $a |= 1 << 1; $b |= 1 << 1; $b |= 1 << 2; my $i = $a & $b; my $u = $a | $b; my $i_cnt = unpack '%32b*', pack 'N', $i; my $u_cnt = unpack '%32b*', pack 'N', $u; printf "a is %#032b %d\n", $a, $a; printf "b is %#032b %d\n", $b, $b; printf "intersection is %#032b %d\n", $i, $i; printf "union is %#032b %d\n", $u, $u; say "set bit count in intersection: $i_cnt"; say "set bit count in union: $u_cnt";

Gives your expected result.

Replies are listed 'Best First'.
Re^2: bit array comparison
by Amendil (Novice) on Oct 22, 2019 at 17:29 UTC

    Oh, we can also use pack that way, so that it formats it just as unpack wants it.

    Thank you, I know understand more the different example I could see elsewhere.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11107863]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (1)
As of 2021-04-17 01:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?