Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

How to compute Exclusive-OR of two hexadecimal Numbers

Any Perl modules are available ?

Replies are listed 'Best First'.
Re: Hexadecimal Exclusive OR
by BrowserUk (Patriarch) on Jun 16, 2010 at 12:45 UTC
    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.

      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

        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
Re: Hexadecimal Exclusive OR
by toolic (Bishop) on Jun 16, 2010 at 12:53 UTC
    Any Perl modules are available ?
    Clearly, BrowserUk offers the simplest solution, but the CPAN Bit::Vector module has an Xor (aka ExclusiveOr) function. I haven't used it, but you could look at it to see if it is relevant to whatever you are trying to do.

    I quickly found this using a search on CPAN for hexadecimal.

Re: Hexadecimal Exclusive OR
by Yary (Pilgrim) on Jun 16, 2010 at 15:05 UTC
    If you want to xor arbitrary buffers, you don't have to treat them as numbers.
    my $var1=pack 'H*','000080506300835700AD00EF'; my $var2=pack 'H*','042426FFFFFFFFFFDE00BE00'; my $result = $var1 ^ $var2; print unpack 'H*',$result; # 0424a6af9cff7ca8deadbeef
    edit: BrowserUK++ beat me to the punch! I need to stop leaving nodes open and going for a snack before writing up my reply.
Re: Hexadecimal Exclusive OR
by JavaFan (Canon) on Jun 16, 2010 at 13:03 UTC
    perl doesn't have hexadecimal numbers. Or decimal numbers. Or octal numbers. You may refer to integer values in Perl code using hexadecimal, decimal, octal or binary, but internally, they're all the same.

    Could you explain what you mean by "hexadecimal numbers"? Considering that Exclusive-OR operates on truth values (or bits), how do you intend to apply this on "hexadecimal numbers"?

Re: Hexadecimal Exclusive OR
by jethro (Monsignor) on Jun 16, 2010 at 12:46 UTC
    ^ is the xor operator

    UDPATE: Please ignore. Overlooked the word 'hexadecimal'