Handle two\'s complement numbers.
Revision 0:b495118be5a7, committed 2011-01-20
- Comitter:
- Owen
- Date:
- Thu Jan 20 20:01:36 2011 +0000
- Commit message:
Changed in this revision
| SignExtend.cpp | Show annotated file Show diff for this revision Revisions of this file |
| SignExtend.h | Show annotated file Show diff for this revision Revisions of this file |
--- /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;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SignExtend.h Thu Jan 20 20:01:36 2011 +0000 @@ -0,0 +1,1 @@ +int SignExtend( int value, const int bit_width ); \ No newline at end of file