Re: [PHP] Bitwise AND for 31-st bit

2011-05-18 Thread Vitalii Demianets
On Tuesday 17 May 2011 22:06:34 David Harkness wrote:
 It appears that PHP is truncating the constant 0x8000 to be within
 MIN_INT and MAX_INT instead of as a bit field, but when shifting 1  31 it
 doesn't do apply any constraints. That's pretty typical of
 bit-manipulation: it will merrily slide 1 bits off either end. This
 explains why  produces 0 as it's doing 0x8000  0x7FFF. It also
 explains the second tests.

Yes, that's it!
I slightly expanded test output and now it's clear that you are right:

$tst1 = (1  31);
$tst2 = 0x8000;
$tst1_eq = $tst1  0x8000;
$tst2_eq = $tst2  0x8000;
$str1 = sprintf(%1$032b, $tst1);
$str2 = sprintf(%1$032b, $tst2);
print tst1=$tst1 ($str1), tst1_eq=$tst1_eq, tst1_type=.gettype($tst1).\n;
print tst2=$tst2 ($str2), tst2_eq=$tst2_eq, tst2_type=.gettype($tst2).\n;

produces this output:

tst1=-2147483648 (1000), tst1_eq=0, 
tst1_type=integer
tst2=2147483647 (0111), tst2_eq=2147483647, 
tst2_type=integer

Now it is obvious to me that PHP 5.2 clamps explicit constants to MAX_INT. 
Weird, b...


 On 64-bit 5.3.3 I get

 tst1=2147483648, tst1_eq=2147483648, tst1_type=integer
 tst2=2147483648, tst2_eq=2147483648, tst2_type=integer

 If I try the 64-bit-equivalent code I get

 tst1=-9223372036854775808, tst1_eq=-9223372036854775808,
 tst1_type=integer
 tst2=9.22337203685E+18, tst2_eq=-9223372036854775808, tst2_type=double


I get similar results with 5.3 on my amd64 host too. It works as it should, no 
weirdness. Glad to know that 5.3 get it fixed. Pity to me that I can not 
update my 5.2 on ARM board.

-- 
Vitalii Demianets

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Bitwise AND for 31-st bit

2011-05-17 Thread Vitalii Demianets
Hello, all!

I've encountered odd behavior of PHP regarding bitwise AND operation when 
dealing with 31-st bit, and kindly ask to give me some pointers.
Consider the following snippet:

$tst1 = (1  31);
$tst2 = 0x8000;
$tst1_eq = $tst1  0x8000;
$tst2_eq = $tst2  0x8000;
print tst1=$tst1, tst1_eq=$tst1_eq, tst1_type=.gettype($tst1).\n;
print tst2=$tst2, tst2_eq=$tst2_eq, tst2_type=.gettype($tst2).\n;

The output is:

tst1=-2147483648, tst1_eq=0, tst1_type=integer
tst2=2147483647, tst2_eq=2147483647, tst2_type=integer

And I totally can not  understand, why in the hell tst1_eq=0 ?
(BTW I'm using PHP 5.2.14 on 32-bit CPU, if that matters)

Sorry if this is well-known feature and have discussed before. In such a case 
please point me to the URL of the discussion.

-- 
Vitalii Demianets

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Bitwise AND for 31-st bit

2011-05-17 Thread Bálint Horváth
Hi,
Hmm.. interesting...
tst1=-2147483648, tst1_eq=-2147483648, tst1_type=integer tst2=2147483648,
tst2_eq=-2147483648, tst2_type=double at me...
PHP Version 5.3.3-1ubuntu9.5 (Apache 2.0 - i686)
Valentine

On Tue, May 17, 2011 at 5:19 PM, Vitalii Demianets
vi...@nppfactor.kiev.uawrote:

 Hello, all!

 I've encountered odd behavior of PHP regarding bitwise AND operation when
 dealing with 31-st bit, and kindly ask to give me some pointers.
 Consider the following snippet:

 $tst1 = (1  31);
 $tst2 = 0x8000;
 $tst1_eq = $tst1  0x8000;
 $tst2_eq = $tst2  0x8000;
 print tst1=$tst1, tst1_eq=$tst1_eq, tst1_type=.gettype($tst1).\n;
 print tst2=$tst2, tst2_eq=$tst2_eq, tst2_type=.gettype($tst2).\n;

 The output is:

 tst1=-2147483648, tst1_eq=0, tst1_type=integer
 tst2=2147483647, tst2_eq=2147483647, tst2_type=integer

 And I totally can not  understand, why in the hell tst1_eq=0 ?
 (BTW I'm using PHP 5.2.14 on 32-bit CPU, if that matters)

 Sorry if this is well-known feature and have discussed before. In such a
 case
 please point me to the URL of the discussion.

 --
 Vitalii Demianets

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] Bitwise AND for 31-st bit

2011-05-17 Thread Vitalii Demianets
On Tuesday 17 May 2011 18:31:00 Bálint Horváth wrote:
 Hi,
 Hmm.. interesting...
 tst1=-2147483648, tst1_eq=-2147483648, tst1_type=integer tst2=2147483648,
 tst2_eq=-2147483648, tst2_type=double at me...
 PHP Version 5.3.3-1ubuntu9.5 (Apache 2.0 - i686)

That is what I expected too.
There are 2 possibilities:
1) There was a bug in 5.2.14 which is fixed in 5.3.3;
2) There still is a bug in latest version of PHP which manifest itself only on 
ARM CPU (which I am working on).

The worst thing is that I can not compile PHP 5.3.x for my board at the 
moment. So, I think, I should live with it, until I manage somehow to upgrade 
PHP installation for that board.

-- 
Vitalii Demianets

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Bitwise AND for 31-st bit

2011-05-17 Thread David Harkness
It appears that PHP is truncating the constant 0x8000 to be within
MIN_INT and MAX_INT instead of as a bit field, but when shifting 1  31 it
doesn't do apply any constraints. That's pretty typical of bit-manipulation:
it will merrily slide 1 bits off either end. This explains why  produces 0
as it's doing 0x8000  0x7FFF. It also explains the second tests.

On 64-bit 5.3.3 I get

tst1=2147483648, tst1_eq=2147483648, tst1_type=integer
tst2=2147483648, tst2_eq=2147483648, tst2_type=integer

If I try the 64-bit-equivalent code I get

tst1=-9223372036854775808, tst1_eq=-9223372036854775808,
tst1_type=integer
tst2=9.22337203685E+18, tst2_eq=-9223372036854775808, tst2_type=double

David