forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParity.java
72 lines (63 loc) · 2.13 KB
/
Parity.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package me.ramswaroop.bits;
/**
* Created by IntelliJ IDEA.
*
* @author: ramswaroop
* @date: 6/3/15
* @time: 3:50 PM
*/
/**
* PARITY: Parity of a number refers to whether it
* contains an odd or even number of 1-bits. The number
* has “odd parity”, if it contains odd number of 1-bits
* and is “even parity” if it contains even number of 1-bits.
*/
public class Parity {
/**
* Uses BRIAN KERNIGAN'S bit counting. Acc. to this, the right most/least significant set bit is unset
* in each iteration. The time complexity is proportional to the number of bits set.
*
* {@see http://stackoverflow.com/questions/12380478/bits-counting-algorithm-brian-kernighan-in-an-integer-time-complexity}
* {@see http://graphics.stanford.edu/~seander/bithacks.html#ParityNaive}
*
* @param n
* @return
*/
public static boolean isEvenParity(long n) {
boolean parity = true;
while (n > 0) {
parity = !parity;
n = n & (n - 1);
}
return parity;
}
/**
* Old school method
*
* @param n
* @return true if {@param n} has even number of 1-bits
*/
public static boolean isEvenParityByCountingSetBits(long n) {
int setBitsCount = 0;
while (n > 0) {
if ((n & 1) == 1) setBitsCount++;
n >>= 1;
}
return setBitsCount % 2 == 0;
}
public static void main(String a[]) {
System.out.println(isEvenParity(0));
System.out.println(isEvenParity(1));
System.out.println(isEvenParity(5));
System.out.println(isEvenParity(6));
System.out.println(isEvenParity(7));
System.out.println(isEvenParity(12));
System.out.println("==========================");
System.out.println(isEvenParityByCountingSetBits(0));
System.out.println(isEvenParityByCountingSetBits(1));
System.out.println(isEvenParityByCountingSetBits(5));
System.out.println(isEvenParityByCountingSetBits(6));
System.out.println(isEvenParityByCountingSetBits(7));
System.out.println(isEvenParityByCountingSetBits(12));
}
}