forked from codemistic/Data-Structures-and-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode190.java
More file actions
30 lines (27 loc) · 838 Bytes
/
Leetcode190.java
File metadata and controls
30 lines (27 loc) · 838 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package com.company;
public class Leetcode190 {
public static void main(String[] args) {
int n = 25;
int answer = reverseBits(n);
System.out.println(answer);
}
static int reverseBit(int n) {
int rev = 0;
for (int i = 0; i <= 31; i++) {
rev = rev << 1;
rev = rev + (n & 1);
n = n >> 1;
}
return rev;
}
static int reverseBits(int n) {
int res=0;
for(int i=0;i<32;i++) //length of word is 32
{
res=res<<1; //this is like multiplying the number with 10 in decimal here we left shift it as in multiplying by 2
res+=(n&1); //add number after taking it's & with 1
n=n>>1; //divide number by 2 to get next digit
}
return res;
}
}