Handle two\'s complement numbers.
Diff: SignExtend.cpp
- Revision:
- 0:b495118be5a7
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SignExtend.cpp Thu Jan 20 20:01:36 2011 +0000 @@ -0,0 +1,17 @@ +#include "mbed.h" +#include "assert.h" + +int SignExtend( int value, const int bit_width ) +{ + assert( ( bit_width > 1) && ( bit_width < (8*sizeof(int)) ) ); + + value &= ( (1<<(bit_width)) - 1 ); /* Clear out any extra MSB data */ + + if ( ( value & ( 1 << (bit_width-1) ) ) != 0 ) + { + /* value IS negative */ + value -= (1<<bit_width); + } + + return value; +}