Created
August 26, 2014 09:08
-
-
Save talaj/fce92d9a540a17e0f5a4 to your computer and use it in GitHub Desktop.
integral promotion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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