Skip to content

Instantly share code, notes, and snippets.

@Ch-sriram
Created May 30, 2020 04:07
Show Gist options
  • Select an option

  • Save Ch-sriram/4f8b4e1517d37fb71d26d71ece1b190a to your computer and use it in GitHub Desktop.

Select an option

Save Ch-sriram/4f8b4e1517d37fb71d26d71ece1b190a to your computer and use it in GitHub Desktop.
Given an unsigned 32-bit integer N, reverse the bits in the binary representation of the integer, and print the new integer formed.
#include <iostream>
using namespace std;
#define ui uint32_t
ui rev_bits(ui n) {
ui rev_num = 0;
for(int i = 0; i < 31; ++i, n>>=1, rev_num<<=1)
if(n&1U) rev_num |= 1;
return rev_num;
}
int main() {
int t; cin >> t;
while(t--) {
ui n; cin >> n;
cout << rev_bits(n) << "\n";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment