in reply to Hexadecimal Exclusive OR

Any Perl modules are available ?

Probably, but why?

printf "0x%0x\n", 0xff ^ 0xaa;; 0x55

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP an inspiration; A true Folk's Guy

Replies are listed 'Best First'.
Re^2: Hexadecimal Exclusive OR
by Anonymous Monk on Jun 16, 2010 at 13:13 UTC

    I have executed the following code but getting Integer overflow in hexadecimal number

    my $var1 = 0x0000805063008357 ; my $var2 = 0x042426FFFFFFFFFF ; my $result = $var1 ^ $var2; printf "0x%0x\n",$result;

      Those hex constants represent 64-bit integers. You are using a version of Perl that can only handle 32-bit integers.

      Several possibilities exist.

      1. Upgrade to a 64-bit Perl:
        $var1 = 0x0000805063008357 ;; Hexadecimal number > 0xffffffff non-portable at ( $var2 = 0x042426FFFFFFFFFF;; Hexadecimal number > 0xffffffff non-portable at ( printf "0x%0x\n", $var1 ^ $var2;; 0x424a6af9cff7ca8

        The non-portable warnings are just a stupid annoyance that can be suppressed.

      2. Manipulate the hex values as strings:
        $str2 = '042426FFFFFFFFFF';; $str1 = '0000805063008357';; print unpack 'H*', pack( 'H*', $str1 ) ^ pack('H*', $str2 );; 0424a6af9cff7ca8

        Whether that is useful will depend on how you are going to utilise the result?

      3. (Re-)compile your 32-bit version of Perl with use64bitint=define.

        That allows a 32-bit perl to manipulate 64-bit numbers, though you'd probably get those annoying 'non-portable' warnings.

      As always, the right solution for your purpose will depend heavily upon that purpose?


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
      use bignum:
      use bignum; my $var1 = 0x0000805063008357 ; my $var2 = 0x042426FFFFFFFFFF ; my $result = $var1 ^ $var2; printf "0x%016x\n",$result;
      Gives:
      0x00000000ffffffff
      Which goes to show that printf does not support big ints.
      Update: improved printf
        Which goes to show that printf does not support big ints.

        You could write

        print $result->as_hex();

        because, with the bignum pragma in effect, the numbers are upgraded to Math::BigInt objects which have the as_hex() method (among others).

        And to get rid of the "Hexadecimal number > 0xffffffff non-portable" warning, you could say

        my $var1 = hex("0x0000805063008357");

        (hex() would need to be imported for perls before 5.9.4)

      For really large values you can use the (core) module Math::BigInt:

      #!/usr/bin/perl -l use strict; use warnings; use Math::BigInt; my $x = Math::BigInt->new("0x00008050630083570000805063008357000080506 +3008357"); my $y = Math::BigInt->new("0x042426FFFFFFFFFF042426FFFFFFFFFF042426FFF +FFFFFFF"); print $x->bxor($y)->as_hex(); # 0x424a6af9cff7ca80424a6af9cff7ca8042 +4a6af9cff7ca8