Re: unexpected results using sprintf() with %u

From: Date: Fri, 03 Oct 2003 07:06:32 +0000
Subject: Re: unexpected results using sprintf() with %u
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-164865@lists.php.net to get a copy of this message
* Thus wrote Cody Phanekham (Cody.Phanekham@salmat.com.au): > Im trying to retrieve the unsigned value of an integer. sounds pretty simple... > > [code] > $i = -86; > echo "<br>" . sprintf("%d", $i); > echo "<br>" . sprintf("%u", $i); > [/code] > > produces: > -86 > 4294967198 > > what i expected: > -86 > 86 > > Ive searched the archives but most of the messages refer to %s or %d... nothing in regards to > %u What you want is abs() http://php.net/abs > can anyone see what im doing wrong? You're expecting wrong results. Signed and Unsigned integers are stored identical bit by bit. The only difference is that in signed the last bit is reserved for the sign (0 = positive 1 = negative), whereas in the unsigned the 1 makes the number bigger by (sizeof(int)^2)-1 [i think my math is right on that] HTH, Curt -- "I used to think I was indecisive, but now I'm not so sure."

« previous php.general (#164865) next »