StochHMM  v0.34
Flexible Hidden Markov Model C++ Library and Application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
bitwise_ops.h
Go to the documentation of this file.
1 //
2 // bitwise_ops.h
3 // DynamicBitset
4 //
5 // Created by Paul Lott on 3/1/13.
6 // Copyright (c) 2013 Korf Lab, Genome Center, UC Davis, Davis, CA. All rights reserved.
7 //
8 
9 #ifndef __DynamicBitset__bitwise_ops__
10 #define __DynamicBitset__bitwise_ops__
11 
12 #include <iostream>
13 #include <math.h>
14 #include <stdint.h>
15 #include <stdlib.h>
16 
17 namespace StochHMM {
18 #ifndef CHAR_BIT
19 #define CHAR_BIT 8
20 #endif
21 
22 
23 #ifndef SIZE_MAX
24 #define SIZE_MAX ((size_t)-1)
25 #endif
26 
27 
28  std::string intToBinString(uint32_t val);
29  size_t msb(uint32_t val);
30  size_t lowestBitSet(uint32_t x);
31  int8_t popCount(uint32_t val);
32  uint32_t maskLeft(uint32_t val,uint8_t pos);
33  uint32_t maskRight(uint32_t val, uint8_t pos);
34 
35  uint32_t rol(uint32_t& val, uint8_t shift_register);
36  uint32_t ror(uint32_t& val, uint8_t shift_register);
37 
38  //!Rotate Left with carry over
39  template <typename T>
40  T rol(T val, uint8_t shift_register) {
41  return (val << shift_register) | (val >> ((sizeof(T)*CHAR_BIT) - shift_register));
42  };
43 
44 
45  //!Rotate Right with carry over
46  template <typename T>
47  T ror(T val, uint8_t shift_register) {
48  return (val >> shift_register) | (val << ((sizeof(T)*CHAR_BIT) - shift_register));
49  }
50 
51  //!Left Shift with shift off
52  //!Shifts the value to the left by n bits.
53  //! The bits that would get shifted off value are returned in lsb
54  template <typename T>
55  T lso(T& val, uint8_t n) {
56  T temp(val);
57  if (n>=(sizeof(T)*CHAR_BIT)){
58  val = 0;
59  }
60  else{
61  val <<=n;
62  }
63  n%=32;
64  return temp >> ((sizeof(T)*CHAR_BIT) - n);
65  };
66 
67  //!Right Shift with shift off
68  //!Shifts the value to the right by n bits.
69  //! The bits that would get shifted off value are returned in msb
70  template <typename T>
71  T rso(T& val, uint8_t n) {
72  T temp(val);
73  if (n>=(sizeof(T)*CHAR_BIT)){
74  val = 0;
75  }
76  else{
77  val >>=n;
78  }
79  n%=32;
80  return temp << ((sizeof(T)*CHAR_BIT) - n);
81  }
82 
83 
84  //!Left Shift with shift off
85  //!Shifts the value to the left by n bits.
86  //! The bits that get shifted off val are returned in lsb of return value
87  //!\param [in,out] val Value to shift
88  //!\param [in] old_val Values to shift on to val
89  //!\returns value with bits that are shifted off val in lsb
90  template <typename T>
91  T lsoso(T& val, T& old_val, uint8_t n) {
92  T temp(val);
93  val = (val << n) | old_val;
94  return temp >> ((sizeof(T)*CHAR_BIT) - n);
95  };
96 
97  //!Right Shift with shift off
98  //!Shifts the value to the right by n bits.
99  //! The bits that would get shifted off value are returned in msb of return value
100  //!\param [in,out] val Value to shift
101  //!\param [in] old_val Values to shift on to val
102  template <typename T>
103  T rsoso(T& val, T& old_val, uint8_t n) {
104  T temp(val);
105  val = (val>>n) | old_val;
106  return temp << ((sizeof(T)*CHAR_BIT) - n);
107  }
108 
109 }
110 
111 #endif /* defined(__DynamicBitset__bitwise_ops__) */