Skip to content

Commit a309809

Browse files
committed
using hamming weight, calculate parity (how many 1s in binary representation) & if parity is odd
1 parent 48165e0 commit a309809

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

src/binary/hammingWeight/HammingWeight.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ static int getHammingWeight2(int input) {
2020
return res;
2121
}
2222

23+
// Parity
24+
public static int findParity(int n) {
25+
return getHammingWeight(n) % 2;
26+
}
27+
28+
public static boolean isParityEven(int n) {
29+
return findParity(n) / 2 == 0;
30+
}
31+
2332
// String logic
2433
static int getHammingWeight(int input) {
2534
var toBinary = Integer.toBinaryString(input);

0 commit comments

Comments
 (0)