Skip to content

Instantly share code, notes, and snippets.

@k3a
Created June 17, 2019 18:13
Show Gist options
  • Save k3a/b4b75b38b7e0b264ca46bdb9186e218f to your computer and use it in GitHub Desktop.
Save k3a/b4b75b38b7e0b264ca46bdb9186e218f to your computer and use it in GitHub Desktop.
#include <stdio.h> // definition of printf()
#include <stdint.h> // definitions of uint32_t and other types
void printUInt(uint32_t val) {
unsigned char binval[33];
for (unsigned i=0; i<32; i++) {
binval[i] = (val & (1<<(31-i))) ? '1' : '0';
}
// zero-terminate
binval[32] = 0;
printf("] hex 0x%08X, bin %s, dec %u\n", val, binval, val);
}
int main() {
uint32_t val;
// print 0x12
val = 0x12;
printUInt(val);
// print 0x1234
val = 0x1234;
printUInt(val);
// compose 0x1234 from two separate bytes
val = (0x12 << 8) | 0x34;
printUInt(val);
// compose 0x1234 from two separate bytes using + instead of bit or
val = (0x12 << 8) + 0x34;
printUInt(val);
// compose full int
val = (0xAA << 24) + (0xBB << 16) + (0xCC << 8) + 0xDD;
printUInt(val);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment