Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: SignExtend.cpp
- Revision:
- 0:b495118be5a7
diff -r 000000000000 -r b495118be5a7 SignExtend.cpp
--- /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;
+}