Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: bit array comparison

by tybalt89 (Monsignor)
on Oct 22, 2019 at 16:50 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11107863]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2024-04-25 14:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found