Skip to content

Instantly share code, notes, and snippets.

@LSViana
Created July 21, 2018 15:06
Show Gist options
  • Save LSViana/1ca5daf396bbf57e1bf75177ff2856e4 to your computer and use it in GitHub Desktop.
Save LSViana/1ca5daf396bbf57e1bf75177ff2856e4 to your computer and use it in GitHub Desktop.
Using memory alignment to mount a 12-byte object with an int and unsigned long long
class MemorySpace {
public:
void setSecond(long long second) {
this->second_1 = second & 0x00000000ffffffff;
this->second_2 = (second & 0xffffffff00000000) >> 32;
}
long long getSecond() {
return this->second_1 + (((unsigned long long )this->second_2) << 32);
}
private:
int first;
unsigned int second_1;
unsigned int second_2;
long long dummy;
};
int main() {
MemorySpace ms1;
// It is 18446744073709551375
unsigned long long second = 0xffffffffffffff0f;
ms1.setSecond(second);
// Same 18446744073709551375, but this one is got from two unsigned int variables :)
unsigned long long _second = ms1.getSecond();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment