Skip to content

Instantly share code, notes, and snippets.

@talaj
Created August 26, 2014 09:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save talaj/fce92d9a540a17e0f5a4 to your computer and use it in GitHub Desktop.
Save talaj/fce92d9a540a17e0f5a4 to your computer and use it in GitHub Desktop.
integral promotion
#include <iostream>
union TileId_t
{
uint64_t packed;
struct {
uint64_t y : 28;
uint64_t x : 28;
uint8_t z : 8;
};
};
int main() {
TileId_t tid;
tid.x = 133021696;
tid.y = 135888896;
tid.z = 15;
std::cout << tid.packed << std::endl;
uint64_t sx = static_cast<uint64_t>(tid.x) >> 8;
uint64_t sy = static_cast<uint64_t>(tid.y) >> 8;
uint64_t pid1 = (sx << 20) + sy;
std::cout << "Variant 1 " << pid1 << std::endl;
uint64_t pid2 = ((static_cast<uint64_t>(tid.x) >> 8) << 20) + (static_cast<uint64_t>(tid.y) >> 8);
std::cout << "Variant 2 " << pid2 << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment