Last active
November 6, 2016 20:54
-
-
Save dvtate/775184a7b8475aa6eaefc12831f353a5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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> | |
#pragma pack(0) | |
// a 4-bit bitfield | |
struct word_t { | |
unsigned int val : 4; | |
}; | |
// 8-bits | |
struct octet { | |
bool b0 : 1, b1 : 1, b2 : 1, b3 : 1, | |
b4 : 1, b5 : 1, b6 : 1, b7 : 1; | |
}; | |
// prints some memory in binary | |
void printBits(char* bytes, size_t len){ | |
for (; len > 0; len--, bytes++) { | |
std::cout <<((octet*) bytes)-> b0 <<((octet*) bytes)-> b1 | |
<<((octet*) bytes)-> b2 <<((octet*) bytes)-> b3 | |
<<((octet*) bytes)-> b4 <<((octet*) bytes)-> b5 | |
<<((octet*) bytes)-> b6 <<((octet*) bytes)-> b7 | |
<<" = " <<((word_t*) bytes)->val <<"\t:: " | |
<<(void*) bytes <<'\n'; | |
} | |
} | |
int main(){ | |
word_t wrds[5] = { { 15 }, { 15 } , { 0 }, { 0}, { 5 } }; | |
std::cout <<sizeof(wrds) <<'\n'; | |
printBits((char*)wrds, 5); | |
/* | |
word_t a; | |
a.val = 0; | |
std::cout <<sizeof(a) <<'\n' <<sizeof(word_t) <<'\n'; | |
for (int i = 0; i < 20; i++) | |
std::cout <<a.val++ <<'\n'; | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment