carl  24.04
Computer ARithmetic Library
logging_utils.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <bitset>
4 #include <cstdint>
5 #include <sstream>
6 
7 namespace carl {
8 
9 /**
10  * Return the binary representation given value as bit string.
11  * Note that this method is tailored to little endian systems.
12  * @param a A value of any type
13  * @param spacing Specifies if the bytes shall be separated by a space.
14  * @return Bit string representing a.
15  */
16 template<typename T>
17 std::string binary(const T& a, const bool& spacing = true)
18 {
19  std::stringstream ss;
20  const std::uint8_t* begin = reinterpret_cast<const std::uint8_t*>(&a); // NOLINT
21  const std::uint8_t* end = begin + sizeof(T);
22  while (begin != end) {
23  --end;
24  ss << std::bitset<8>(std::uint8_t(*end));
25  if (spacing && (begin != end)) ss << " ";
26  }
27  return ss.str();
28 }
29 
30 /**
31  * Return the basename of a given filename.
32  */
33 inline std::string basename(const std::string& filename) {
34  auto slash1 = filename.rfind('/') + 1;
35  auto slash2 = filename.rfind('\\') + 1;
36  if (slash2 > slash1) slash1 = slash2;
37  return filename.substr(slash1);
38 }
39 
40 }
carl is the main namespace for the library.
std::string binary(const T &a, const bool &spacing=true)
Return the binary representation given value as bit string.
Definition: logging_utils.h:17
std::string basename(const std::string &filename)
Return the basename of a given filename.
Definition: logging_utils.h:33