Malte Onken
Malte Onken

Reputation: 769

Objective c: binary operations with Integers?

I have BASIC logical question about the following code snippet:

1     uint64_t RMTileKey(RMTile tile)
2    {
3            uint64_t zoom = (uint64_t) tile.zoom & 0xFFLL; // 8bits, 256 levels
4            uint64_t x = (uint64_t) tile.x & 0xFFFFFFFLL;  // 28 bits
5            uint64_t y = (uint64_t) tile.y & 0xFFFFFFFLL;  // 28 bits
6    
7       uint64_t key = (zoom << 56) | (x << 28) | (y << 0);
8    
9       return key;
10    }

The return value key is an unsigned integer. I am very confused now, because i dont understand what is happening in line 3(4,5). The operator & does what with my uint64_t. I guess it is converting to a hex-value ? And then in line 7 i shift from bit 0 to 27 (28 to 56 ...) and merge theses hex-based numbers?

Upvotes: 0

Views: 1274

Answers (2)

pmg
pmg

Reputation: 108978

line 3: zoom has all the bits except 0 to 7 clear: 0000...000zzzzzzzz 8 bits

line 4: x has all the bits except 0 to 27 clear: 0000...000xxxx...xxxx; 28 bits

line 5: y has all the bits except 0 to 27 clear: 0000...000yyyy...yyyy; 28 bits

line 7: the bits are rearranged to make a single 64-bit value:

         ,--------------------- bit 56
        /          ,----------- bit 28
       /          /          ,- bit 0
zzzzzzzzxxxx...xxxxyyyy...yyyy
zoom<<56   x<<28      y<<0

Upvotes: 3

MByD
MByD

Reputation: 137312

First - & is the binary AND operator. | is the binary OR operator.

  1. Line 3 will assign the LSB of tile.zoom to zoom (which is modulu 0x100)
  2. Line 4 will assign the 28 least significant bits of tile.x to x (which is modulu 0x10000000)
  3. Line 5 as line 4.

  4. Line 7 will construct the key variable by putting all the above variable in different bits(offsets) of key.

Upvotes: 0

Related Questions