Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reverse bits in a bitset?

For example, I have the integer

a = 10;

and it's binary representation (for a 32 bit integer) is

00000000000000000000000000001010

and reversed, it becomes

01010000000000000000000000000000

Now I've seen this code, from this topcoder article that can accomplish this

x = ((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1);
x = ((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2);
x = ((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4);
x = ((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8);
x = ((x & 0xffff0000) >> 16) | ((x & 0x0000ffff) << 16);

Now is there some straightforward way to achieve the same effect. Perhaps by converting our bitset into a string, and then reversing that? The constructors and method for converting bitset to a string of a bitset are so complicated I can't seem to figure out how to do this.

Here's what I tried so far

#include <bitset>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <typeinfo>
#include <string>

using namespace std;

int main() {

    const unsigned int k = 32;


    int x = 10;
    bitset<k> nf(x);

    cout << nf << endl;

    string str =
        nf.to_string<char,string::traits_type,string::allocator_type>();

    reverse(str.begin(), str.end() + str.size());

    cout << str << endl;

    return 0;
}

But I'm getting this as the output:

00000000000000000000000000001010
G;ÿJG¥±žGsÿkìöUàä˜\éä˜\é
like image 713
Rockstar5645 Avatar asked Feb 01 '18 06:02

Rockstar5645


People also ask

Which operator is used to reverse the bits?

Bitwise complement operator is used to reverse the bits of an expression.

How do I convert bitset to all bits to 1?

bitset set() function in C++ STL bitset::set() is a built-in STL in C++ which sets the bit to a given value at a particular index. If no parameter is passed, it sets all bits to 1. If only a single parameter is passed, it sets the bit at that particular index to 1.


4 Answers

This is the trivial inplace approach straight on a bitset:

template<std::size_t N>
void reverse(std::bitset<N> &b) {
    for(std::size_t i = 0; i < N/2; ++i) {
        bool t = b[i];
        b[i] = b[N-i-1];
        b[N-i-1] = t;
    }
}
like image 142
Matteo Italia Avatar answered Oct 05 '22 02:10

Matteo Italia


Less code will win you some time in TopCoder SRMs. Following is what I would use in TopCoder SRMs (see it live here):

#include <algorithm>
#include <bitset>
#include <iostream>
#include <string>

int main() {
  auto x = std::bitset<32>(10);
  std::cout << x << std::endl;

  auto str = x.to_string();
  std::reverse(str.begin(), str.end());
  auto y = std::bitset<32>(str);
  std::cout << y << std::endl;

  return 0;
}
like image 30
Lingxi Avatar answered Oct 05 '22 03:10

Lingxi


Without using any standard library functions (aside from printing the result):

#include<iostream>
#include<bitset>

const int size = sizeof(int)*CHAR_BIT;

int main()
{
    int x = 10;
    int r = 0;
    for(int i = 0; i < size; i++)
    {
        r = r << 1 | (x & 1);
        x >>= 1;
    }
    std::bitset<size> bits(r);
    std::cout << "Reverse " << bits << std::endl;

}

like image 45
Mats Petersson Avatar answered Oct 05 '22 03:10

Mats Petersson


uint32_t reverseBits(uint32_t n) {
    bitset<32> b(n);                       // store in binary form 
    string s=b.to_string();               //put it in string
    string k(s.rbegin(),s.rend());       //reverse string
    bitset<32>d(k);                      //again put new string k  in bitset
    return  d.to_ulong();               //get value in decimal form
    
    
}
like image 34
Deepesh arya Avatar answered Oct 05 '22 03:10

Deepesh arya