LINUX.IE, website of the Irish Linux Users' Group
Tux rules!

   
Home
New Users
Articles
Download
Projects
Community
Vendors

  Print Version
Email to...
 
Archives:


planetILUG

Recent News

News Archive


Join the
ILUG
on FaceBook


Join the
ILUG
on LinkedIn


Join the
ILUG SETI
Group



















 
 :: Mailing Lists

[ILUG] puzzle of the day

[ILUG] puzzle of the day

John P. Looney john at antefacto.com
Thu Feb 14 17:32:17 GMT 2002


On Thu, Feb 14, 2002 at 05:13:26PM +0000, Colm Buckley mentioned:
> On Thu 14 Feb 2002 17:06, Kate wrote:
> 
> > Anyone know how to turn a signed number into an unsigned one.
> 
> Er.  *If* you have a type which can hold the unsigned result, then
> it's:
> 
>       2^(size) + (signed number)
> 
> eg : 4294967296 + (-1407713022) = 2887254274 (not 2392992828; don't
> know where you got that from).
 
 That was a guess :) 

 Ah. Right. Found out what the problem was. Horrid bug. Basically, I was
trying to do a PHP "sort by IP" type function. Problems;

 printf("%u", signed_int);

 printed nothing (despite being told by the docs it should do an integer
conversion on it). So, I'd to do it with:

function ip_compare($arg1,$arg2) {
    $ip1=ip2long($arg1);
    $ip2=ip2long($arg2);
    if($ip1 < 0 ) {
        $ip1 += pow(2,32);
    }
    if($ip2 < 0 ) {
        $ip2 += pow(2,32);
    }
    return($ip1-$ip2);
}

 The idea being that if it return negative, then $ip2 was bigger than
$ip1. Simple. Only, some times it didn't work. Why ?

 The calling function (in this case uasort()) expected an integer. So cast
back the number from a float to an int. Which worked, most of the time,
except if $ip1 was way bigger (more than 2 billion) than $ip2. In which
cast it would return a number bigger than 2 billion...which would get
turned into a negative signed int.

 Bah.

Kate

-- 
_______________________________________
John Looney             Chief Scientist
a n t e f a c t o     t: +353 1 8586004
www.antefacto.com     f: +353 1 8586014





More information about the ILUG mailing list
Read this without the formatting.
                                                                                                    

 

Hosted by HEAnet


Maintained by the ILUG website team. The aim of Linux.ie is to support and help commercial and private users of Linux in Ireland. You can display ILUG news in your own webpages, read backend information to find out how. Networking services kindly provided by HEAnet, server kindly donated by Dell. Linux is a trademark of Linus Torvalds, used with permission. No penguins were harmed in the production or maintenance of this highly praised website. Looking for the Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!
RSS Version
Powered by Dell