473,498 Members | 1,544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP bug (or my bad code)?

Consider this code:

<?
$mask_n = (pow(2,32) - (pow(2,32-24)-1));
printf("%x\n", $mask_n);
?>

On PHP 4.1.2 it prints:

ffffff00

On PHP 4.3.10 is prints:

ffffff01
Both versions of PHP are the standard Debian packaged versions (4.1.2
from Woody, 4.3.10 from Sarge)

tecra# php -v
PHP 4.3.10-9 (cli) (built: Mar 6 2005 17:45:14)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

admin# php -v
4.1.2

Results are (predictably) the same under both the CLI version and under
the Apache module.
Is this a bug in PHP, or is there something broken in my code?
Jul 17 '05 #1
3 1272
Bob Purdon wrote:
Consider this code:

<?
$mask_n = (pow(2,32) - (pow(2,32-24)-1));
printf("%x\n", $mask_n);
?>

On PHP 4.1.2 it prints:
ffffff00

On PHP 4.3.10 is prints:
ffffff01

Both versions of PHP are the standard Debian packaged versions (4.1.2
from Woody, 4.3.10 from Sarge)


Some other code:

<pre>
<?
$mask_n = (pow(2,32) - (pow(2,32-24)-1));
var_dump($mask_n);
printf("%x\n", $mask_n);

$mask_n2 = 4294967041;
var_dump($mask_n2);
printf("%x\n", $mask_n2);

echo "so... what's the difference ... " . ($mask_n - $mask_n2);

?>
</pre>

On woody 4.1.2:

float(4294967041)
ffffff00
float(4294967041)
ffffff01
so... what's the difference ... -3.3378601074219E-06

On sarge 4.3.10:

float(4294967041)
ffffff01
float(4294967041)
ffffff01
so... what's the difference ... 0

What's going on?!

Hans van Kranenburg

--
"He who asks a question is a fool for five minutes;
he who does not ask a question remains a fool forever"
Jul 17 '05 #2
"Hans van Kranenburg" <f0****@xs4all.nl> wrote in message
news:42*********************@news.xs4all.nl...
Bob Purdon wrote:
Consider this code:

<?
$mask_n = (pow(2,32) - (pow(2,32-24)-1));
printf("%x\n", $mask_n);
?>

On PHP 4.1.2 it prints:
ffffff00

On PHP 4.3.10 is prints:
ffffff01

Both versions of PHP are the standard Debian packaged versions (4.1.2
from Woody, 4.3.10 from Sarge)


Some other code:

<pre>
<?
$mask_n = (pow(2,32) - (pow(2,32-24)-1));
var_dump($mask_n);
printf("%x\n", $mask_n);

$mask_n2 = 4294967041;
var_dump($mask_n2);
printf("%x\n", $mask_n2);

echo "so... what's the difference ... " . ($mask_n - $mask_n2);

?>
</pre>

On woody 4.1.2:

float(4294967041)
ffffff00
float(4294967041)
ffffff01
so... what's the difference ... -3.3378601074219E-06

On sarge 4.3.10:

float(4294967041)
ffffff01
float(4294967041)
ffffff01
so... what's the difference ... 0

What's going on?!

Hans van Kranenburg


I imagine the relevant statement from the manual is:

"If possible, this function will return an integer"

Your expression would be OK if indeed it were calculated in integers. But
since 2 raised to the power 32 cannot be represented in 32 bits (it misses
by 1), you're thrown into the realm of floating point, and in that world,
you can't assume all bits will be as you think they will be, only that the
numeric values will be sufficiently close to the exact result. And in
floating point, the *order* in which you do arithmetic matters, quite a lot.

As to your code, the most straight-forward way to create bit masks (which is
what I'm thinking you are trying to do) is like this:

$mask = 0xFFFFFFFF << (32 - 8)

which will reliably give you:

0xFFFFFF00

Further, if this were my software, I'd simply write:

$mask = 0xFFFFFF00;

because that says, as clearly as can be said, "I want a 32-bit mask with the
high 24 bits set and the low 8 bits clear".

If I had a variable that specified the number of low zero bits I needed, I'd
write:

$mask = 0xFFFFFFFF << (32 - $low_zero_bits);

because that says, as clearly as can be said, "I want a mask that's 32 bits
wide and is everywhere 1's except for the $low_zero_bits which are zero".

Finally, if you want this to work cross-platform, you should define a
constant that's the number of bits in an integer, and work from that,
remembering to never shift by more than that many bits, since Intel
disagrees with most of the rest of the world as to the meaning of
over-shifting (shifting more than 32 on a 32-bit machine, for example).
Jul 17 '05 #3
> As to your code, the most straight-forward way to create bit masks (which is
what I'm thinking you are trying to do) is like this:

$mask = 0xFFFFFFFF << (32 - 8)
Yeah, indeed a much better way.
Further, if this were my software, I'd simply write:

$mask = 0xFFFFFF00;
Agree, except the mask varies, so:
$mask = 0xFFFFFFFF << (32 - $low_zero_bits);


Is the way it needs to go...

Seems I've been cracking this nut the hard way and overlooking the
obvious :-)

Thanks all.

Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

51
5200
by: Mudge | last post by:
Please, someone, tell me why OO in PHP is better than procedural.
109
5739
by: Andrew Thompson | last post by:
It seems most people get there JS off web sites, which is entirely logical. But it is also a great pity since most of that code is of such poor quality. I was looking through the JS FAQ for any...
0
2075
by: Namratha Shah \(Nasha\) | last post by:
Hey Guys, Today we are going to look at Code Access Security. Code access security is a feature of .NET that manages code depending on its trust level. If the CLS trusts the code enough to...
171
7579
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles)...
4
2087
by: KenFehling | last post by:
Hello. I am wondering if there exists a piece of software that takes multiple .js files that are nicely indented and commented and create one big tightly packed .js file. I'm hoping the one file...
88
7950
by: Peter Olcott | last post by:
Cab you write code directly in the Common Intermediate language? I need to optimize a critical real-time function.
7
3418
by: blackrosezy | last post by:
#include char *code; void main() { char buf = "book";
2
4803
by: rn5a | last post by:
Assume that a user control (MyUC.ascx) encapsulates 2 TextBoxes with the IDs 'txt1' & 'txt2' respectively. To use this user control in an ASPX page, the following Register directive will be...
1
1506
by: ahammad | last post by:
Hello, I have written a fairly complex parsing tool that is used to parse information from company documents. The program works very well, but in order to insure that all the data is copied...
66
7389
by: Jon Skeet [C# MVP] | last post by:
I'm sure the net will be buzzing with this news fairly soon, but just in case anyone hasn't seen it yet: Microsoft are going to make the source code for the .NET framework (parts of it,...
0
7126
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7005
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6891
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5465
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4916
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4595
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3087
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1424
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.