# bxor

Chandrashekhar Mullaparthi Chandrashekhar.Mullaparthi@REDACTED
Wed Feb 19 18:47:39 CET 2003

```You could convert them to integers, bxor them and then convert the result to
a binary again.

bin_bxor(Bin1, Bin2) ->
Sz1 = size(Bin1)*8,
Sz2 = size(Bin2)*8,
<<Int1:Sz1>> = Bin1,
<<Int2:Sz2>> = Bin2,
Int3 = Int1 bxor Int2,
Sz3 = max(Sz1, Sz2),
<<Int3:Sz3>>.

max(Int1, Int2) when Int1 >= Int2 -> Int1;
max(Int1, Int2) when Int1 < Int2 -> Int2.

cheers
Chandru

-----Original Message-----
From: Dan Melomedman [mailto:dan-erlang@REDACTED]
Sent: 19 February 2003 17:29
To: erlang-questions@REDACTED
Subject: Re: bxor

Matthias Lang wrote:
>
> Dan Melomedman writes:
>  > Hi. I need to bxor other datatypes than integers. What's the
>  > recommended way of doing something like this? Thanks.
>
> It depends on what you want to do and how fast you want to do it and
> how long you want to spend programming it.

It doesn't need to be fast, and I don't want to spend too much time
programming just to be able to XOR. So I am hoping there's an easy way.

> A simple byte-wise approach to XOR-ing the binary Bin with a constant is:
>
>   list_to_binary( [X bxor 123 || X <- binary_to_list(Bin)] )

I think initially I'd like to be able to XOR two binaries. Or two lists.
What if the sizes of the two binaries differ? Will I need to pad one of
them to the size of the other? Thanks much.

NOTICE AND DISCLAIMER:
This email (including attachments) is confidential.  If you have received
this email in error please notify the sender immediately and delete this
email from your system without copying or disseminating it or placing any
reliance upon its contents.  We cannot accept liability for any breaches of
confidence arising through use of email.  Any opinions expressed in this
email (including attachments) are those of the author and do not necessarily
reflect our opinions.  We will not accept responsibility for any commitments
made by our employees outside the scope of our business.  We do not warrant
the accuracy or completeness of such information.

```