-
Notifications
You must be signed in to change notification settings - Fork 20.4k
Description
What would you like to Propose?
you have a neat implementation for bit swapping , but following improvements can be made
Add Method Overloads
Support swapping bits in a long (64-bit).
Support short or byte for completeness.
For example
/**
- Example:
- int data = 29; // 11101 (binary)
- int result = BitSwap.bitSwap(data, 0, 3);
- // result = 21 (10101 in binary)
*/
Performance Micro-Optimization
Instead of calling SingleBitOperations.getBit(...) twice, use bitwise directly.
For example
boolean bitA = ((data >> posA) & 1) != 0;
boolean bitB = ((data >> posB) & 1) != 0;
if (bitA != bitB) {
data ^= (1 << posA) | (1 << posB);
}
Count Set Bits
For example
public static int countSetBits(int data) {
int count = 0;
while (data != 0) {
data &= (data - 1);
count++;
}
return count;
}
Issue details
--
Additional Information
I Think these changes can be made but the first code for implementing bit swapping is absolutely correct .
what will be your opinion on these sir/madam .