Skip to content

Instantly share code, notes, and snippets.

@drott
Created March 22, 2016 11:26
Show Gist options
  • Save drott/b4e0424c3a11e281064e to your computer and use it in GitHub Desktop.
Save drott/b4e0424c3a11e281064e to your computer and use it in GitHub Desktop.
Test for integer truncation
#include <stdio.h>
// TIL, this compiles without warnings with
// $ clang -Wall main.cpp -o testTruncation
// The following local pragmas may be used to activate warnings for this case,
// if you can't use -Wconversion globally.
// #pragma GCC diagnostic push
// #pragma GCC diagnostic warning "-Wconversion"
bool test(unsigned short a) {
if (a < 0x20)
return true;
return false;
}
int main() {
int testA = 0x90012;
unsigned short testB = testA;
printf("result for testA %x: %s\n", testA, test(testA) ? "true" : "false");
printf("result for testB %x: %s\n", testB, test(testB) ? "true" : "false");
}
// #pragma GCC diagnostic pop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment