Skip to content

Commit 8722598

Browse files
authored
Add ParityCheck algorithm (#5704)
1 parent e263edc commit 8722598

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

DIRECTORY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
* [NumberAppearingOddTimes](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimes.java)
4141
* [NumbersDifferentSigns](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/NumbersDifferentSigns.java)
4242
* [OnesComplement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/OnesComplement.java)
43+
* [ParityCheck](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ParityCheck.java)
4344
* [ReverseBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ReverseBits.java)
4445
* [SingleBitOperations](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SingleBitOperations.java)
4546
* [SingleElement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SingleElement.java)
@@ -670,6 +671,7 @@
670671
* [NumberAppearingOddTimesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumberAppearingOddTimesTest.java)
671672
* [NumbersDifferentSignsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/NumbersDifferentSignsTest.java)
672673
* [OnesComplementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/OnesComplementTest.java)
674+
* [ParityCheckTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ParityCheckTest.java)
673675
* [ReverseBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ReverseBitsTest.java)
674676
* [SingleBitOperationsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleBitOperationsTest.java)
675677
* [SingleElementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleElementTest.java)
@@ -801,6 +803,7 @@
801803
* [LevelOrderTraversalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/LevelOrderTraversalTest.java)
802804
* [PostOrderTraversalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/PostOrderTraversalTest.java)
803805
* [PreOrderTraversalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/PreOrderTraversalTest.java)
806+
* [QuadTreeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/QuadTreeTest.java)
804807
* [SameTreesCheckTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/SameTreesCheckTest.java)
805808
* [SplayTreeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/SplayTreeTest.java)
806809
* [TreapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/trees/TreapTest.java)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* The ParityCheck class provides a method to check the parity of a given number.
5+
* <p>
6+
* Parity is a mathematical term that describes the property of an integer's binary representation.
7+
* The parity of a binary number is the number of 1s in its binary representation.
8+
* If the number of 1s is even, the parity is even; otherwise, it is odd.
9+
* <p>
10+
* For example, the binary representation of 5 is 101, which has two 1s, so the parity of 5 is even.
11+
* The binary representation of 6 is 110, which has two 1s, so the parity of 6 is even.
12+
* The binary representation of 7 is 111, which has three 1s, so the parity of 7 is odd.
13+
*
14+
* @author Hardvan
15+
*/
16+
public final class ParityCheck {
17+
private ParityCheck() {
18+
}
19+
20+
/**
21+
* This method checks the parity of the given number.
22+
*
23+
* @param n the number to check the parity of
24+
* @return true if the number has even parity, false otherwise
25+
*/
26+
public static boolean checkParity(int n) {
27+
int count = 0;
28+
while (n > 0) {
29+
count += n & 1;
30+
n >>= 1;
31+
}
32+
return count % 2 == 0;
33+
}
34+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
import static org.junit.jupiter.api.Assertions.assertFalse;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
public class ParityCheckTest {
9+
@Test
10+
public void testIsOddParity() {
11+
assertTrue(ParityCheck.checkParity(5)); // 101 has 2 ones (even parity)
12+
assertFalse(ParityCheck.checkParity(7)); // 111 has 3 ones (odd parity)
13+
assertFalse(ParityCheck.checkParity(8)); // 1000 has 1 one (odd parity)
14+
}
15+
}

0 commit comments

Comments
 (0)