Java icon indicating copy to clipboard operation
Java copied to clipboard

[FEATURE REQUEST] BitSwap.java

Open shashanknr172-beep opened this issue 1 year ago • 7 comments

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 .

shashanknr172-beep avatar Sep 28 '25 11:09 shashanknr172-beep

Yes your suggestions improve usability and performance:

  1. Method overloading can make the code more versatile.
  2. Using direct bitwise operations instead of calling SingleBitOperations.getBit(...) twice is cleaner and slightly faster.
  3. 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.

kunwarbhattal avatar Sep 28 '25 20:09 kunwarbhattal

Thankyou for the reply and i am sorry for the bug , surely the original implementation is correct .

shashanknr172-beep avatar Sep 29 '25 10:09 shashanknr172-beep

Please assign me this

Amitesh-exp avatar Oct 01 '25 08:10 Amitesh-exp

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!

RiyaJoshi0103 avatar Oct 11 '25 12:10 RiyaJoshi0103

your most welcome , always supportive for the innovative things .

ALL THE BEST

shashanknr172-beep avatar Oct 13 '25 13:10 shashanknr172-beep

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!

github-actions[bot] avatar Nov 13 '25 00:11 github-actions[bot]

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!

github-actions[bot] avatar Nov 21 '25 00:11 github-actions[bot]