Created
June 14, 2016 19:52
-
-
Save cr1901/46871cd9883b7bf4a1476d1e98a0e85d to your computer and use it in GitHub Desktop.
Portable? Unsigned to Signed Conversion?
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 <limits.h> | |
/* Assume: short is 16-bits | |
twos-complement. */ | |
short wrap(unsigned short a); | |
short wrap(unsigned short in) | |
{ | |
if(in < SHRT_MAX) | |
{ | |
return (short) in; | |
} | |
else | |
{ | |
unsigned short tmp = in - SHRT_MAX; | |
short tmp2 = SHRT_MIN + tmp; | |
return tmp2; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment