Skip to content

Commit 2ececc0

Browse files
Create number-complement.java
1 parent f7c023c commit 2ececc0

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Java/number-complement.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int findComplement(int num) {
3+
4+
int numberOfBits = numberOfBits(num);
5+
6+
return num ^ ((1 << numberOfBits) - 1);
7+
}
8+
9+
/**
10+
* Method to find out the total number of bits in a number.
11+
**/
12+
public static int numberOfBits(int num){
13+
int countOfBits = 0;
14+
15+
while(num != 0){
16+
num = num >> 1;
17+
countOfBits++;
18+
}
19+
20+
return countOfBits;
21+
}
22+
}

0 commit comments

Comments
 (0)