Skip to content

[FEATURE REQUEST] BitSwap.java #6549

@shashanknr172-beep

Description

@shashanknr172-beep

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 .

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions