Handle two\'s complement numbers.
Embed:
(wiki syntax)
Show/hide line numbers
SignExtend.cpp
00001 #include "mbed.h" 00002 #include "assert.h" 00003 00004 int SignExtend( int value, const int bit_width ) 00005 { 00006 assert( ( bit_width > 1) && ( bit_width < (8*sizeof(int)) ) ); 00007 00008 value &= ( (1<<(bit_width)) - 1 ); /* Clear out any extra MSB data */ 00009 00010 if ( ( value & ( 1 << (bit_width-1) ) ) != 0 ) 00011 { 00012 /* value IS negative */ 00013 value -= (1<<bit_width); 00014 } 00015 00016 return value; 00017 }
Generated on Tue Jul 12 2022 16:29:02 by 1.7.2