note 58190 added to language.operators.bitwise

From: Date: Thu, 27 Oct 2005 02:30:45 +0000
Subject: note 58190 added to language.operators.bitwise
Groups: php.notes 
Request: Send a blank email to php-notes+get-97649@lists.php.net to get a copy of this message
I refer to Eric Swanson's post on Perl VS PHP's implementation of xor. Actually, this is not an issue with the implementation of XOR, but a lot more to do with the lose-typing policy that PHP adopts. Freely switching between int and float is good for most cases, but problems happen when your value is near the word size of your machine. Which is to say, 32-bit machines will encounter problems with values that hover around 0x80000000 - primarily because PHP does not support unsigned integers. using bindec/decbin would address this issue as a work-around to do unsigned-int xor, but here's the real picture (i'm not claiming that this code will perform better, but this would be a better pedagogical code): <?php function unsigned_xor32 ($a, $b) { $a1 = $a & 0x7FFF0000; $a2 = $a & 0x0000FFFF; $a3 = $a & 0x80000000; $b1 = $b & 0x7FFF0000; $b2 = $b & 0x0000FFFF; $b3 = $b & 0x80000000; $c = ($a3 != $b3) ? 0x80000000 : 0; return (($a1 ^ $b1) |($a2 ^ $b2)) + $c; } $x = 3851235679; $y = 43814; echo "<br>This is the value we want"; echo "<br>3851262585"; echo "<br>The result of a native xor operation on integer values is treated as a signed integer"; echo "<br>".($x ^ $y); echo "<br>We therefore perform the MSB separately"; echo "<br>".unsigned_xor32($x, $y); ?> This is really foundation stuff, but for those of you who missed this in college, there seems to be something on 2's complement here: http://www.evergreen.edu/biophysics/technotes/program/2s_comp.htm ---- Manual Page -- http://www.php.net/manual/en/language.operators.bitwise.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+58190 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58190&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58190&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58190&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58190&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58190&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58190&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58190&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+58190&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#97649) next »