forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCountBitsFlip.java
More file actions
63 lines (56 loc) · 1.8 KB
/
CountBitsFlip.java
File metadata and controls
63 lines (56 loc) · 1.8 KB
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
package com.thealgorithms.bitmanipulation;
/**
* Implementation to count number of bits to be flipped to convert A to B
*
* Problem: Given two numbers A and B, count the number of bits needed to be
* flipped to convert A to B.
*
* Example:
* A = 10 (01010 in binary)
* B = 20 (10100 in binary)
* XOR = 30 (11110 in binary) - positions where bits differ
* Answer: 4 bits need to be flipped
*
* Time Complexity: O(log n) - where n is the number of set bits
* Space Complexity: O(1)
*
*@author [Yash Rajput](https://github.com/the-yash-rajput)
*/
public final class CountBitsFlip {
private CountBitsFlip() {
throw new AssertionError("No instances.");
}
/**
* Counts the number of bits that need to be flipped to convert a to b
*
* Algorithm:
* 1. XOR a and b to get positions where bits differ
* 2. Count the number of set bits in the XOR result
* 3. Use Brian Kernighan's algorithm: n & (n-1) removes rightmost set bit
*
* @param a the source number
* @param b the target number
* @return the number of bits to flip to convert A to B
*/
public static long countBitsFlip(long a, long b) {
int count = 0;
// XOR gives us positions where bits differ
long xorResult = a ^ b;
// Count set bits using Brian Kernighan's algorithm
while (xorResult != 0) {
xorResult = xorResult & (xorResult - 1); // Remove rightmost set bit
count++;
}
return count;
}
/**
* Alternative implementation using Long.bitCount().
*
* @param a the source number
* @param b the target number
* @return the number of bits to flip to convert a to b
*/
public static long countBitsFlipAlternative(long a, long b) {
return Long.bitCount(a ^ b);
}
}