Created
May 30, 2020 04:07
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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