File: IntToBits.cpp

package info (click to toggle)
librepfunc 1.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 424 kB
  • sloc: cpp: 1,601; makefile: 270
file content (30 lines) | stat: -rw-r--r-- 846 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*******************************************************************************
 * librepfunc - a collection of common functions, classes and tools.
 * See the README file for copyright information and how to reach the author.
 ******************************************************************************/
#include <repfunc.h>
#include <string>    // std::string


template<class T>
std::basic_string<T> BitsT(std::intmax_t n, size_t width) {
  std::basic_string<T> s(width,(T)'0');
  size_t p = width - 1;
  for(size_t i=0; i<width; i++) {
     if (n & 1)
        s[p] = (T)'1';
     else
        s[p] = (T)'0';
     n >>= 1;
     p--;
     }
  return s;
}

std::string IntToBits(std::intmax_t n, size_t width) {
  return BitsT<char>(n, width);
}

std::wstring IntToBitsW(std::intmax_t n, size_t width) {
  return BitsT<wchar_t>(n, width);
}