about summary refs log tree commit diff
path: root/lib/util/bitarith.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/util/bitarith.h')
-rw-r--r--lib/util/bitarith.h42
1 files changed, 42 insertions, 0 deletions
diff --git a/lib/util/bitarith.h b/lib/util/bitarith.h
new file mode 100644
index 00000000..4ed6c4c3
--- /dev/null
+++ b/lib/util/bitarith.h
@@ -0,0 +1,42 @@
+#ifndef BITARITH_H_INCLUDED
+#define BITARITH_H_INCLUDED
+
+#include "pm_config.h"
+
+static __inline__ unsigned char
+pm_byteLeftBits(unsigned char const x,
+                unsigned int  const n) {
+/*----------------------------------------------------------------------------
+  Clear rightmost (8-n) bits, retain leftmost (=high) n bits.
+
+  Return arbitrary value if n > 8.
+-----------------------------------------------------------------------------*/
+    unsigned char retval;
+
+    retval = x;
+    retval >>= (8-n);
+    retval <<= (8-n);
+
+    return retval;
+}
+
+
+
+static __inline__ unsigned char
+pm_byteRightBits(unsigned char const x,
+                 unsigned int  const n){
+/*----------------------------------------------------------------------------
+  Return rightmost (=low) n bits of x.
+
+  Return arbitrary value if n > 8.
+-----------------------------------------------------------------------------*/
+    unsigned char retval;
+
+    retval = x;
+    retval <<= (8-n);
+    retval >>= (8-n);
+
+    return retval;
+}
+
+#endif