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

Re^3: Bitwise AND with large numbers

by Eliya (Vicar)
on Apr 16, 2012 at 17:12 UTC ( #965361=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Bitwise AND with large numbers
in thread Bitwise AND with large numbers

This works fine for me:

use Math::BigInt; my $switchPortNumber = 2; # 32 Character hex string from switch my $hexstring = Math::BigInt->new('0x40000000000000000000000000000000' +); my $mask = (Math::BigInt->new('0x80000000000000000000000000000000') >> + $switchPortNumber); my $result = ($hexstring & $mask) > 0;

Another approach would be to turn it into an ASCII bitstring, in which case you can use a simple substr() instead of the AND masking

my $bitstring = unpack('B*', pack('H*', '40000000000000000000000000000 +000')); my $result = substr($bitstring, $switchPortNumber, 1) eq '1';


Comment on Re^3: Bitwise AND with large numbers
Select or Download Code
Re^4: Bitwise AND with large numbers
by mdej (Initiate) on Apr 20, 2012 at 08:20 UTC
    Hey Eliya,

    Thanks for the quick response. Your solutions look promising. I especially like the one where you turn the hex string in a ASCII bit string. I will try this with my code and test it with the switch. I will let you know how things went.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (11)
As of 2014-08-22 12:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (156 votes), past polls