1

Suppose I have two strings in perl

$a = "10001";
$b = "10101";

I need to find bitwise xor of these two strings i.e.

$a XOR $b = "00100";

How should I do this in perl ?

6 Answers 6

4

To xor two numbers:

my $n1 = 0b10001;
my $n2 = 0b10101;
my $n = $n1 ^ $n2;
say sprintf '%05b', $n;

To xor two numbers (starting from string form):

my $n1 = '10001';
my $n2 = '10101';
my $len = max map length, $n1, $n2;
my $n = oct("0b$n1") ^ oct("0b$n2");
say sprintf '%0*b', $len, $n;

To xor two strings: (any length, as long as it's the same for both strings):

my $n1 = '10001';
my $n2 = '10101';
my $n = ($n1 ^ $n2) | ("\x30" x length($n1));
say $n;

To xor two strings: (any length):

my $n1 = '010001';
my $n2 = '10101';
my $len = max map length, $n1, $n2;
$n1 = substr((" " x $len).$n1, -$len);
$n2 = substr((" " x $len).$n2, -$len);
my $n = ($n1 ^ $n2) | ("\x30" x $len);
say $n;
Sign up to request clarification or add additional context in comments.

Comments

3

Use oct:

If EXPR starts off with 0b, it is interpreted as a binary string.

#!/usr/bin/env perl

my ($x, $y) = map oct "0b$_", qw(10001 10101);
printf "%08b\n", $x ^ $y;

Comments

2

If you are declaring the information yourself, you can use a bit literal:

my $first = 0b10001;
my $second = 0b10101;

my $xored = $first ^ $second;

if ($xored == 0b00100)
{
    print "Good!";
}

Works with numbers and hex too:

my $first = 21;    # or 0b10101 or 0x15
my $second = 0x11; # or 0b10001 or 17

my $xored = $first ^ $second;

if ($xored == 0b00100) # or == 4,  or == 0x04 
{
    print "Good!";
}

As an aside, you should also avoid using $a and $b, as they have a special meaning for the sort function.

Comments

1

Use this subroutine:

sub bin2dec {
    return unpack("N", pack("B32", substr("0" x 32 . shift, -32)));
}

It will convert the strings to integers, and you can use the bitwise XOR ^ on them, then test if that equals 4.

Comments

0

You could write a few functions that does the one-bit operation:

sub xor
{
   my $p = shift;
   my $q = shift;

   return ( $p eq $q ) "0" : "1";
}

then you could call this successfully for each pair of characters (each successive bit).

for (my $index = 0; $index < inputLength; $index++)
{
     $result = $result . xor ( substr ($a, $index, 1), substr ($b, $index, 1) );
}

Where inputLength is the length of $a and $b. This is one way of doing it. Hope this helps!

Comments

0

As far as $a and $b have a fixed length, say 5:

 $a = '00011';
 $b = '00110';
 $xor = ($a ^ $b) | '00000';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.