Justtechjobs.com Find a programming school near you






Online Campus Both


php3-list | 199903

Re: [PHP3] Integer size and bitshifting == problems From: Carsten H. Pedersen (carsten.pedersen <email protected>)
Date: 03/31/99

It ain't pretty, but...

typecast the <<24 part to float and multiply by the same number:

        $IPhigh1 = (float) $IPoctets[0] * 16777216; // *2^24

PHP's automagic typecast takes care of the rest, you end up
with $IP as a float and Bob's yer uncle. Now, all you have to
do is make a string of $IP to store in your database (MySQL
communicates by passing strings anyhow).

Of course, if all you need to do is store the IP adress in the
database, then the method you already use will work fine. You
just have to remember to AND out the sign bits when restoring
the IP number from the database. Unless, of course, you're
betting that PHP doesn't change to 64-bit integers in the
meantime... (lemme guess: this is system/compiler dependent, no?)

Regards,

Carsten H. Pedersen

"Martin B. Jespersen" wrote:
>
> Hi there, i'm doing some bitshifting and ending up with a number that is larger than 32bit signed, so how do i make that work?
>
> ok here is what i do:
>
> <?php
> $IPoctets = explode(".",$REMOTE_ADDR);
> $IPhigh1 = $IPoctets[0] << 24;
> echo "IPhigh1: $IPhigh1, IPoctets[0]: $IPoctets[0]<br>\n";
> $IPhigh2 = $IPoctets[1] << 16;
> echo "IPhigh2: $IPhigh2, IPoctets[1]: $IPoctets[1]<br>\n";
> $IPhigh3 = $IPoctets[2] << 8;
> echo "IPhigh3: $IPhigh3, IPoctets[2]: $IPoctets[2]<br>\n";
> echo "IPhigh4: $IPoctets[3]<br>\n";
> $IP = $IPhigh1 + $IPhigh2 + $IPhigh3 + $IPoctets[3];
> echo "IP: $IP<br>";
> ?>
>
> now this should give me the unsigned 32bit number of my IPaddress, but since the number is stored in a 32bit signed variable
> internally in php the $IPhigh1 variable is wrapped around and becomes a very large negative number.
>
> here is the output from the above code:
>
> IPhigh1: -1023410176, IPoctets[0]: 195
> IPhigh2: 6356992, IPoctets[1]: 97
> IPhigh3: 41728, IPoctets[2]: 163
> IPhigh4: 208
> IP: -1017011248
>
> now this is clearly not what i am interested in :)
>
> so how do i make sure that i am working with unsigned 32bit numbers instead of signed?
>
> Also: what is the limit of numbers in php? I am working with datastructures (MySQL) that contains 64bit signed intergers, can php
> handle that?
>
> --
> Martin B. Jespersen
> Technical Project Leader
> Icon Medialab A/S (http://www.IconMedialab.dk)
>
> E-Mail: Martin <email protected>
> Mail: Havnegade 39, DK-1058 Copenhagen K
> Voice: (+45) 7023-1001
> Fax: (+45) 33320209
>
> --
> PHP 3 Mailing List http://www.php.net/
> To unsubscribe send an empty message to php3-unsubscribe <email protected>
> To subscribe to the digest list: php3-digest-subscribe <email protected>
> For help: php3-help <email protected> Archive: http://www.php.net/mailsearch.php3
> List administrator: zeev-list-admin <email protected>

-- 
"If you sent American beer out to be analyzed, the lab 
would probably phone up and say, "Your horse has diabetes."
                      - Sharyn McCrumb, "Bimbos of the Death Sun"

-- PHP 3 Mailing List http://www.php.net/ To unsubscribe send an empty message to php3-unsubscribe <email protected> To subscribe to the digest list: php3-digest-subscribe <email protected> For help: php3-help <email protected> Archive: http://www.php.net/mailsearch.php3 List administrator: zeev-list-admin <email protected>