Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Bitwise AND with large numbers

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


in reply to Bitwise AND with large numbers

You can do bitwise AND with strings, too (in which case there's no size limitation).

So, convert your hex string and your number to binary strings using pack (e.g. pack "H*", $hexstring for the hex string), pad the shorter one to the length of the longer one (with \0 or \xff, depending on the desired semantics), AND (&) both strings, and convert the result back to whatever you like (e.g. hex string).

The details somewhat depend on what exactly you need, so if you provide sample inputs and desired result, someone will surely come up with actual code...

Otherwise, if you want to stick with Math::Big*, there is from_hex().


Comment on Re: Bitwise AND with large numbers
Select or Download Code
Re^2: Bitwise AND with large numbers
by mdej (Initiate) on Apr 16, 2012 at 16:56 UTC
    Hi,

    Thank you for your quick reactions ;-)
    Using Math::BigInt or pack I unfortunately still get an integer overflow. I'm probably doing something wrong but don't see what. Sorry if this is to much of a newbie question.

    A little more context: The hexstring comes from a switch. I want to use the hexstring to determine if a switchport is member of a specific VLAN. The code I have so far is:
    my $switchPortNumber = 2; # 32 Character hex string from switch my $hexstring = hex '40000000000000000000000000000000'; my $mask = (0x80000000000000000000000000000000 >> $switchPortNumber); my $result = ($hexstring & $mask) > 0;
    In this case only the first port is a member and the second port is NOT a member. Therefore the $result should be 0

    If possible I would like to stick to the default Perl modules.
    Thanks again for all the help so far.

      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';
        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://964799]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2014-12-22 09:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (113 votes), past polls