libgsjj  1
Deterministic finite automata learning library
binary.cpp
Go to the documentation of this file.
1 
9 #include "gsjj/binary.h"
10 
11 namespace gsjj {
12  std::vector<bool> decimal_to_binary(unsigned int dec_number, unsigned int size) {
13  if (dec_number == 0) {
14  return std::vector<bool>(size, 0);
15  }
16  unsigned int tmp = dec_number;
17  std::vector<bool> res;
18  res.reserve(size);
19  while (tmp > 0) {
20  if (tmp % 2 == 0) {
21  res.push_back(0);
22  tmp = tmp / 2;
23  }
24  else {
25  res.push_back(1);
26  tmp = tmp - 1;
27  tmp = tmp / 2;
28  }
29  }
30 
31  while (res.size() < size) {
32  res.push_back(0);
33  }
34  return res;
35  }
36 
37  unsigned int binary_to_decimal(const std::vector<bool> &binary_number) {
38  unsigned int res = 0;
39  unsigned int actual_exp = 1;
40  for (unsigned int i = 0 ; i < binary_number.size() ; i++) {
41  unsigned int bit = binary_number[i];
42  res += actual_exp * bit;
43  actual_exp *= 2;
44  }
45  return res;
46  }
47 }
The namespace of the library.
Definition: binary.h:11
This file declares the function to convert a number in its decimal form to its binary form and vice-v...
std::vector< bool > decimal_to_binary(unsigned int dec_number, unsigned int size)
Converts a number in decimal form to binary form.
Definition: binary.cpp:12
unsigned int binary_to_decimal(const std::vector< bool > &binary_number)
Converts a number in binary form to decimal form.
Definition: binary.cpp:37