|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #3176 Using Bit manupulation ( Bitwise AND, OR, XOR, NOT) do not work as expected
Submitted: 2000-01-11 07:56 UTC Modified: 2005-03-30 09:03 UTC
Avg. Score:4.2 ± 0.9
Reproduced:10 of 10 (100.0%)
Same Version:2 (20.0%)
Same OS:1 (10.0%)
From: markj at usn dot nl Assigned:
Status: Wont fix Package: Misbehaving function
PHP Version: 3.0.13 OS: HPUX/10
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: markj at usn dot nl
New email:
PHP Version: OS:


 [2000-01-11 07:56 UTC] markj at usn dot nl
I encountered this with version 4.0b3, but I suspect it to be in other versions too... i'll check as soon as I can get 3 running somewhere...

I'm currently using PHP4.0b3, and have encountered some inconsistencies when manipulating
an integer number at the bit level. I use this integer 'settings' to store up to 30 user-configurable
settings for my application. 0 being that all are turned off.
When I toggle a specific bit (with my own function toggle_bit( &$bitfield, $bitnr ) (included below))
i get really strange results if I for example do this:
$settings = 0;
toggle_bit( $settings, 1 )   /* This should toggle bit with value 1, making $settings 1... */
echo $settings     /* This will return garbage... */

This gives random results (probably because $settings is signed, the result always has a
1 as the first bit (when i do bindec), but isn't -1 (two's complement)

When I first 'turn my value into a unsigned int' by or-ing it with 0:
$settings = 0
$newset = ( $settings | 0 )
toggle_bit( $newset, 1 )
echo $newset    /* This will return the expected value of 1... */


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2000-01-11 08:37 UTC] markj at usn dot nl
Here's the toggle_bit function:

function toggle_bit ( &$bitfield, $bitvalue )
     $bitfield ^= $bitvalue;
 [2005-03-30 09:03 UTC]
We are sorry, but we can not support PHP 3 related problems anymore.
Momentum is gathering for PHP 5, and we think supporting PHP 3 will
lead to a waste of resources which we want to put into getting PHP 5
ready. Of course PHP 4 will continue to be supported for the
forseeable future.

 [2005-12-16 10:03 UTC] nite2quake at yahoo dot co dot uk
I'm currently using PHP5 and I still seem to have this bug effecting my scripts.

 [2006-11-16 21:17 UTC] borg at servernet dot se
Its still in 4.4.3!
We need a fix for this.
 [2006-11-19 01:19 UTC] sn_ at gmx dot net
Using the following code I cannot reproduce the bug using php 5.1.2 or 5.2.0


function toggle_bit ( &$bitfield, $bitvalue )
     $bitfield ^= $bitvalue;

$settings = 4;
toggle_bit($settings, 10);
echo $settings."\n";
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sun Oct 17 07:03:33 2021 UTC