PHP Bugs  
php.net | support | documentation | report a bug | advanced search | search howto | statistics | login

go to bug id or search bugs for  

Bug #25877 >> operator bug for negative LOP
Submitted:15 Oct 2003 7:40am UTC Modified: 9 Aug 2004 5:31pm UTC
From:cestmirl at freeside dot sk Assigned to:
Status:Closed Category:Documentation problem
Version:4.3.3 OS:Linux Debian, Win32
View/Vote Developer Edit Submission

Welcome! If you don't have a SVN 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.
SVN Username: SVN Password:
Quick Fix:
Status: Assign to:
Category:
Summary:
From: cestmirl at freeside dot sk
New email:
Version: OS:
New/Additional Comment:

[15 Oct 2003 7:40am UTC] cestmirl at freeside dot sk
Description:
------------
Bitwise shift right operator returns negative result (most significant
bit set) for negative input (left operand).

Following assert fails (though should NOT)

assert((1 << 31 >> 31) == 1);

Reproduce code:
---------------
assert((1 << 31 >> 31) == 1);

Actual result:
--------------
assertion failed
[15 Oct 2003 7:56am UTC] moriyoshi@php.net
Since PHP doesn't support unsigned shift operations that are represented
by '<<<' or '>>>' in the C language, bit shift operations are done in
"sign-respectful" manner, where the most significant bit will never be
changed. This is expected behaviour, but not documented yet.
[15 Oct 2003 8:00am UTC] moriyoshi@php.net
Heh, who told me that C supports '>>>' operator... there's no such
operator in that language.
[15 Oct 2003 11:54am UTC] jay@php.net
This is expected. (1 << 31) wraps the 32-bit integer 
limit, and I believe this behaviour is undefined, at least 
in C and C++. If you run the same code in C or C++, the 
result is the same as what you're experiencing. 
 
J 
[15 Oct 2003 12:57pm UTC] cestmirl at freeside dot sk
You're right, it's the same semantics as GCC or Java has for signed int
(though I've never experienced this yet as I've raised in Pascal
world...).

However, in this case, I think it's worth considering to implement Java
unsigned shift ops (<<<, >>>) in PHP. Of course, there's no problem to
mask out appropriate number of bits from left after making "signed
shift" right, but standard operator would be much cleaner solution.
[15 Oct 2003 7:55pm UTC] moriyoshi@php.net
You may want to see the following thread brought up in the past at the
php internals list:
http://marc.theaimsgroup.com/?l=php-dev&m=103530736509081&w=2

Anyway, Jay, do you think it is more than enough to add a slight comment
on the relevant page that the opeators cannot be used for unsigned
operation? I see no reason to bogusify this as I regard this as a
documentation problem.

 
[16 Oct 2003 11:22am UTC] jay@php.net
Yeah, that would probably be a good idea. Maybe just a 
note that this sort of behaviour is undefined. We should 
probably check to see how it acts on Windows, too. (I 
don't have a Win box in front of me at the moment so if 
anybody could check...) 
 
J 
[9 Aug 2004 5:31pm UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation
better.

"Don't right shift for more than 32 bits on 32 bits systems. Don't left
shift in case it results to number longer than 32 bits."

RSS feed | show source 

PHP Copyright © 2001-2009 The PHP Group
All rights reserved.
Last updated: Sat Nov 21 10:30:49 2009 UTC