Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
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
Replies are listed 'Best First'.
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 cooling their heels in the Monastery: (13)
As of 2015-07-29 16:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (265 votes), past polls