[FEATURE REQUEST] BitSwap.java
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 .
Yes your suggestions improve usability and performance:
- Method overloading can make the code more versatile.
- Using direct bitwise operations instead of calling SingleBitOperations.getBit(...) twice is cleaner and slightly faster.
- Including a countSetBits method is a handy addition for common bit manipulation tasks.
Overall, I think the suggestions are useful enhancements, but the original implementation is correct and functional. PS your example has a bug, /** Example: int data = 29; // 11101 (binary) int result = BitSwap.bitSwap(data, 0, 3); // result = 21 (10101 in binary) */ here the result will be 29 only, as 0th and 3rd bit in 11101 are same.
Thankyou for the reply and i am sorry for the bug , surely the original implementation is correct .
Please assign me this
Hi, I would like to work on this issue as part of Hacktoberfest 2025. I’ll make sure to follow the contribution guidelines and write clean, well-documented Java code. Thank you!
your most welcome , always supportive for the innovative things .
ALL THE BEST
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution!
Please reopen this issue once you have made the required changes. If you need help, feel free to ask in our Discord server or ping one of the maintainers here. Thank you for your contribution!