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.cpp
Go to the documentation of this file.
1 //
2 // bitwise_ops.cpp
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 #include "bitwise_ops.h"
10 
11 namespace StochHMM {
12 
13  std::string intToBinString(uint32_t val){
14  std::string output("");
15  for(size_t i = 31; i != SIZE_MAX ;--i){
16  if (val & (1 << i)){
17  output+="1";
18  }
19  else{
20  output+="0";
21  }
22  }
23  return output;
24  }
25 
26  //Finds most significant bit set
27  size_t msb(uint32_t val){
28  const uint32_t mask[] = {
29  0x00000FFFF,
30  0x0000000FF,
31  0x00000000F,
32  0x000000003,
33  0x000000001
34  };
35  int hi = 32;
36  int lo = 0;
37  int i = 0;
38 
39  if (val == 0)
40  return 0;
41 
42  for (i = 0; i < 5; i++) {
43  int mi = lo + (hi - lo) / 2;
44 
45  if ((val >> mi) != 0)
46  lo = mi;
47  else if ((val & (mask[i] << lo)) != 0)
48  hi = mi;
49  }
50 
51  return lo;
52  }
53 
54 
55  //Find lowest lsb
56  size_t lowestBitSet(uint32_t x){
57  static const unsigned int MultiplyDeBruijnBitPosition[32] =
58  {
59  // precomputed lookup table
60  0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
61  31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
62  };
63 
64  // leave only lowest bit
65  x &= -int(x);
66  // DeBruijn constant
67  x *= 0x077CB531;
68  // get upper 5 bits
69  x >>= 27;
70  // convert to actual position
71  return MultiplyDeBruijnBitPosition[x];
72  }
73 
74  //!Population distance (Hamming Distance)
75  //!Returns the number of bits set in the 32bit integer
76  int8_t popCount(uint32_t val){
77  val = val - ((val >> 1) & 0x55555555); // reuse input as temporary
78  val = (val & 0x33333333) + ((val >> 2) & 0x33333333); // temp
79  return ((val + (val >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; // count
80  }
81 
82 
83 
84  //!Returns integer's higher bits from position up to msb
85  //!Lower bits are set to zero
86  uint32_t maskLeft(uint32_t val,uint8_t pos){
87  if (pos>=32){
88  return 0;
89  }
90 
91  return val & ~((1 << pos) - 1);
92  }
93 
94  //!Returns integer's lower bits from position down to lsb
95  //!Higher bits are set to zero
96  uint32_t maskRight(uint32_t val, uint8_t pos){
97  if (pos >31){
98  return 0;
99  }
100  else if (pos == 31){
101  return val & (1<<31);
102  }
103  return val & ((1 << pos+1) - 1);
104  }
105 
106  ////Rotate left with carry through
107  //uint32_t ror(uint32_t& val, uint8_t shift_register) {
108  // uint32_t temp(val);
109  // //Shifted integer
110  // val = val << shift_register;
111  //
112  // //Bits shifted off register
113  // return temp >> (32 - shift_register);
114  // //return (val << shift_register) | (val >> (32 - shift_register));
115  //}
116  //
117  ////Rotate right with carry through
118  //uint32_t ror(uint32_t& val, uint8_t shift_register) {
119  // uint32_t temp(val);
120  // //Shifted integer
121  // val = val >> shift_register;
122  //
123  // //Bits shifted off register
124  // return temp >> (32 - shift_register);
125  // //return (val >> shift_register) | (val << (32 - shift_register));
126  //}
127 
128 
129 
130 
131 
132 
133  //unsigned char rol (unsigned char x)
134  //{
135  // return ((x >> 7) | (x << 1));
136  //}
137  //
138  //unsigned char ror (unsigned char x)
139  //{
140  // return ((x << 7) | (x >> 1));
141  //}
142 
143 }