Design Pattern Examples
Overview of object-oriented design patterns
uint32_to_binary.cpp
Go to the documentation of this file.
1
6
7#include <sstream>
8#include <vector>
9
10#include "uint32_to_binary.h"
11
12namespace Helpers
13{
14
16 // uint32_to_binary()
18 std::string uint32_to_binary(uint32_t number)
19 {
20 int numBits = (sizeof(uint32_t) * 8);
21 int maxBitIndex = numBits - 1;
22 uint32_t mask = 1;
23 std::vector<char> buffer;
24 buffer.resize(numBits + 1);
25
26 for (int bitIndex = maxBitIndex; bitIndex >= 0; bitIndex--)
27 {
28 buffer[bitIndex] = (number & mask) ? '1' : '0';
29 mask <<= 1;
30 }
31 return std::string(&buffer[0]);
32 }
33
34} // end namespace
Declaration of the uint32_to_binary() function, that converts a 32-bit unsigned integer to a binary s...
The namespace containing all the "helper" functions in the C++ code.
std::string uint32_to_binary(uint32_t number)
Function to convert a 32-bit unsigned integer into a string representation containing all 32 bits.