php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #6260 xor operator not working correctly
Submitted: 2000-08-20 09:44 UTC Modified: 2001-05-13 05:32 UTC
From: fredrik at devibe dot net Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.0.1pl2 OS: Linux RedHat 60
Private report: No CVE-ID: None
 [2000-08-20 09:44 UTC] fredrik at devibe dot net
I Compiled php with MySQL and apxs.

php.ini shouldn't be relevant. It was not included in the install (btw. why?) and I've only created one with only session related stuff.

I found this thing on a newsgroup, alt.php, a new group not yet found on many servers, but you *can* find it on news.cis.dfn.de. The thread is named "a little maths problem", started 08/15/00 11:07.

I've considered it for some time now, and I can't find any explanation:
the code snippet
<?php
$val = 2634023077 ^ 407240870;
echo $val;
?>
outputs "-1740242778".

This can't to my knowledge be correct. I mean, 2634023077 would normally be an unsigned integer, but I haven't found any way to specify signed/unsigned integers. In addition, the answer should be 2235957251, and -1740242778 is not the signed representation of that number.

  1001 1101 0000 0000 0000 0000 1010 0101  -  2634023077
^ 0001 1000 0100 0110 0000 0000 1010 0110  -  0407240870
= 1000 0101 0100 0110 0000 0000 0000 0011  -  2235957251

The result from an xor operation performed on these two integers in php is
-1740242778. 1740242778 (positive) is in binary:
0110 0111 1011 1001 1111 1111 0101 1010

Then the negative representation should be (negating each bit and "adding" one):
1001 1000 0100 0110 0000 0000 1010 0110

which unsigned is 2554724518, not 2235957251.

All this taken into consideration, is this a bug/flaw or am I wrong?


--Fredrik de Vibe

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-08-20 09:50 UTC] stas@php.net
PHP integers are signed longs. If you want to go outside signed long, you should use bcmath library.

I do not close it since PHP has showed some very strange behaviour on large numbers, which might be useful to investigate and decide what to do.
 [2001-05-13 05:32 UTC] derick@php.net
This is not a bug in PHP, so closing.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 08:01:29 2024 UTC