Skip to content

Instantly share code, notes, and snippets.

@cr1901
Created June 14, 2016 19:52
Show Gist options
  • Save cr1901/46871cd9883b7bf4a1476d1e98a0e85d to your computer and use it in GitHub Desktop.
Save cr1901/46871cd9883b7bf4a1476d1e98a0e85d to your computer and use it in GitHub Desktop.
Portable? Unsigned to Signed Conversion?
#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