Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #undef __STRICT_ANSI__
- #include <iostream>
- #include <algorithm>
- #include <string>
- #include <vector>
- using namespace std;
- typedef signed long long int int64;
- #define all(a) (a).begin(), (a).end()
- template<typename T> inline unsigned int getBitsInType() {
- static const unsigned int bitsInByte = 8;
- return bitsInByte * sizeof(T);
- }
- template<typename T> inline bool isTrueBit(const T &number, const int &bitIndex) {
- return number & ((T)1 << bitIndex);
- }
- template<typename T> string binaryRepresentation(const T &number) {
- unsigned int bits = getBitsInType<T> ();
- string binary(bits, '0');
- for (int i = 0; i < bits; ++i) {
- if (isTrueBit(number, i)) {
- binary[i] = '1';
- }
- }
- while (binary.size() > 1 && binary.back() == '0') {
- binary.pop_back();
- }
- reverse(all(binary));
- return "b" + binary;
- }
- template<typename T> vector<unsigned int> listOfSetBits(const T &number) {
- unsigned int bits = getBitsInType<T> ();
- vector<unsigned int> listOfBits;
- for (int i = 0; i < bits; ++i) {
- if (isTrueBit(number, i)) {
- listOfBits.push_back(i);
- }
- }
- return listOfBits;
- }
- int main() {
- int64 n;
- cin >> n;
- cout << binaryRepresentation(n) << endl;
- for (int index : listOfSetBits(n)) {
- cout << index << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement