Reverse bits of the number.
Example:
Input: n=43261596
Output: 964176192
Approach
Java
public class ReverseBits {public static void main(String[] args) {int n = 43261596;int x = reverseBits(n);System.out.println(x);}private static int reverseBits(int n) {int x = 0;for (int i = 31; i >= 0; i--) {x |= (n & 1) << i;n = n >> 1;}return x;}}
C++
#include <bits/stdc++.h>using namespace std;//function to reverse bits//of 32 bit numberunsigned int reverseBits(unsigned int n){unsigned int x= 0;for(int i = 31; i>=0;i-- ){x |= (n & 1) << i;n =n>>1;}return x;}int main(){unsigned int n = 43261596;unsigned int x =reverseBits(n);cout<<x<<"\n";return 0;}
No comments:
Post a Comment