- Notifications
You must be signed in to change notification settings - Fork 366
/
Copy pathLeetcode190.java
30 lines (27 loc) · 838 Bytes
/
Leetcode190.java
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
packagecom.company;
publicclassLeetcode190 {
publicstaticvoidmain(String[] args) {
intn = 25;
intanswer = reverseBits(n);
System.out.println(answer);
}
staticintreverseBit(intn) {
intrev = 0;
for (inti = 0; i <= 31; i++) {
rev = rev << 1;
rev = rev + (n & 1);
n = n >> 1;
}
returnrev;
}
staticintreverseBits(intn) {
intres=0;
for(inti=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
}
returnres;
}
}