Unsigned Problems Revisited
| From: | Jason T. Greene | Date: | Tue, 22 Oct 2002 17:23:23 +0000 |
| Subject: | Unsigned Problems Revisited | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-89927@lists.php.net to get a copy of this message | ||
Everyone,
After being bit by the unsigned number problems in php again, I have
decided to focus on getting them quickly resolved. After spending some
time researching, I have come up with a list of possible fixes ranked in
order of my preference. Please send me your arguments, comments, etc
1. Implement an unsigned right shift operator
---------------------------------------------
I personally think we should just implement >>>, and >>>=. There is no
real need for symmetry, and I really don't think it will be a problem to
users. If we absolutely have to have symmetry, then I suggest we create
the bogus <<<, and <<<= which will require overloading the heredoc
operator. Seeing as the heredoc operator and the bogus unsigned left
shift operator have completely different uses, I do not think their
would be either confusion nor technical problems, or at least no more
confusing then the double meaning of the unary '-' operator, and the
arithmetic '-' operator.
An example demonstrating this option:
<?php
//Set b = 1
$b = 1 << 31 >>> 31;
//set b = -1
$b = 1 << 31 >> 31;
?>
If for some reason we HAVE to have a symmetrical bogus unsigned left
shift operator, and we completely disagree with my arguments on
overloading the HEREDOC operator, then we can implement >>>>, >>>>=,
<<<<. <<<<= as the unsigned shift operators.
2. Shift behavior declare compiler directive
--------------------------------------------
Implement a declare compiler directive which will change the behavior of
>>. It is not very likely that one would perform an arithmetic right
shift and a true unsigned right shift in the same php file, but if that
was desired, it could be achieved by using the block style declare.
An example demonstrating this option:
<?php
declare (arithmetic_shifting=false);
// Set b = 1
$b = (1 << 31) >> 31;
declare (arithmetic_shifting=true) {
//Set b = -1
$b = (1 << 31) >> 31;
}
?>
3. Implement Unsigned Data types
---------------------------------
It is possible to implement a uint data type, which could be made
available by either using settype(), or (uint), and by certain scenarios
decided by the engine. Ambiguities are solved by the following rule: All
mathematical operations performed on an unsigned type will result in an
unsigned type, unless overflow occurs. Floating point numbers, if
positive, could be converted to an unsigned type before right shift
occurs. The following code demonstrates this options:
<?php
$b = (uint) 1;
//Set b = 1
$b = (1 << 31) >> 31;
?>
4. Add a function which performs an unsigned right shift
--------------------------------------------------------
This functionality could be moved to a function called
unsigned_right_shift(), or true_right_shift. I dislike this the most
because it is inconsistent, you lose the assign-op ability, and it
requires a lot more typing.
ex
<?php
// Set b = 1
$b = true_right_shift(1 << 31, 31);
// Set b = -1
$b = (1 << 31) >> 31;
?>
Thanks,
-Jason
--
Jason T. Greene <jason@inetgurus.net>
<jason@php.net>