2

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

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
Avinash
  • 6,064
  • 15
  • 62
  • 95

3 Answers3

6

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.

Community
  • 1
  • 1
Vinko Vrsalovic
  • 330,807
  • 53
  • 334
  • 373
1

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.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
0

Those two lines are taking the 32bit values $lat and $lon and reducing them to 16bit values. Have a look into bitwise operators.

mdec
  • 5,122
  • 4
  • 25
  • 26