We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f7c023c commit 2ececc0Copy full SHA for 2ececc0
Java/number-complement.java
@@ -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