Created
March 22, 2016 11:26
-
-
Save drott/b4e0424c3a11e281064e to your computer and use it in GitHub Desktop.
Test for integer truncation
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 <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