xor function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
probkid
Forum Newbie
Posts: 10
Joined: Thu Jun 20, 2002 9:03 am

xor function

Post by probkid »

Hi,
I'm having some problem using the function "xor" to get xor value of 2 bit strings. The strings that i am using are:
1001100010111111010010000000010010110001110110100100110001101001
and
1000001100111111011010110101000100100001101001111100110010100110
and I am getting the wrong result as
1001100010111111010010000000010010110001110110100100110001101001
whereas it should be
0001101110000000001000110101010110010000011111011000000011001111

Any ideas as to why this happens and what is the way to work around this?

Any help would be greatly appreciated.

Thanks.
User avatar
RandomEngy
Forum Contributor
Posts: 173
Joined: Wed Jun 26, 2002 3:24 pm
Contact:

Post by RandomEngy »

A simple workaround would just involve looping through the string, adding a '1' to the string if the bits are (0,1) or (1,0) and a '0' if the bits are (0,0) or (1,1). I'm sure you can code something like that.
probkid
Forum Newbie
Posts: 10
Joined: Thu Jun 20, 2002 9:03 am

xor function

Post by probkid »

You're right. I thought of writing a function but was just wondering why such a simple function was giving incorrect results, in php and if there was a readymade workaround!!
But, I guess I'll have to write my own function now.
Thanks!!
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

Looking through the manual, I'm pretty sure that it happens because PHP will only work with 32-bit or less numbers accurately. you should check out the GMP Library Reference at http://www.php.net/manual/en/ref.gmp.php. This will allow you arbitrary precision.[/url]
Post Reply