function explanation in php
i have one function but i am not getting what is it doing.
Below is my function
// My function gets two parameters lat and long
public function generate_peano1($lat, $lon)
{
$lat = (($lat + 90.0)/180.0 * 32767) + 16384;
$lon = ($lon + 180.0)/360.0 * 65535;
$lat_16 = $lat&0x0000FFFF; // Not getting what is here.
$lon_16 = $lon&0x0000FFFF; // Not getting what is here.
$peano = self::derive_peano_32($开发者_运维百科lat_16, $lon_16);
return $peano;
}
Thanks
Avinash
The &
operator is the bitwise AND operator. 0x0000FFFF
represents 16 unset bits (zeroes) followed by 16 set bits (ones) in hexadecimal.
$lat & 0x0000FFFF
will then give you the 16 least significant bits (on a little endian machine, which is the most common architecture) of $lat.
As to why is that needed here it depends on what does self::derive_peano_32()
do. I'd imagine it takes two 16 bit values and concatenate them somehow so they fit in a regular 32 bit integer.
This &
all alone is the bitwise AND operator.
Using $lat & 0x0000FFFF
, you'll get bits that are set in both sides of the operator ; i.e.e, bits that are set in both $lat
and 0x0000FFFF
Considering 0x0000FFFF
has it's 16 rightmost bits set to 1
and the other set to 0
, you'll get the 16 rightmost bits that were set to 1
in $lat
.
Those two lines are taking the 32bit values $lat and $lon and reducing them to 16bit values. Have a look into bitwise operators.
精彩评论