Skip to content

Add LongBitField class for 64-bit operations with tests#1552

Closed
theodoral22 wants to merge 2 commits intoapache:masterfrom
theodoral22:long-bitfield-feature
Closed

Add LongBitField class for 64-bit operations with tests#1552
theodoral22 wants to merge 2 commits intoapache:masterfrom
theodoral22:long-bitfield-feature

Conversation

@theodoral22
Copy link
Copy Markdown

Summary

This PR introduces a new class LongBitField that extends the concept of BitField to support 64-bit values (long).
The existing BitField only supports int, short and byte, which is insufficient for modern use cases involving large flags or 64-bit bitsets.

Features

  • Supports all basic bit operations: set, clear, setBoolean, getValue, getRawValue, isSet, isAllSet.
  • Operates on long values to allow 64-bit flags.
  • Fully unit-tested in LongBitFieldTest.java, covering:
    -> Single-bit and multi-bit masks
    -> Edge cases (lowest and highest bits)
    -> Boolean set/clear operations
    -> Setting and getting values
  • Designed to be consistent with BitField API for easy adoption.

Motivation

  • 'BitField' is limited to 32-bit integers (int). Many modern applications require 64-bit flags or large bitsets.
  • This class provides a direct, intuitive solution for such cases without needing to manually manipulate long values.

Testing

  • All methods are covered by JUnit tests in LongBitFieldTest.java.
  • Tests validate correctness of all bit operations, including edge cases.
  • Tests can be run using standard Maven/Gradle build for Apache Commons Lang.

Copy link
Copy Markdown
Member

@garydgregory garydgregory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@theodoral22
Thank you for the PR. Some minor comments to address throughout.

* System.out.println(high8.getValue(value)); // 0x56
* </pre>
*
* @since 3.13 (example)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong.

}

/**
* Obtains the value for the specified LongBitField, unshifted.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A getter "Gets..."


import org.junit.jupiter.api.Test;

public class LongBitFieldTest {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The class and test methods should not be public to follow JUnit 5 guidelines.

public long setValue(final long holder, final long value) {
return holder & ~mask | (value << shiftCount) & mask;
}
} No newline at end of file
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file must end in an empty line.

public void testSetBoolean() {
LongBitField field = new LongBitField(0x1000L); // bit 12
long holder = 0x0000L;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove empty lines.

}

/**
* Replaces the bits with new values.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A setter "Sets..."

/**
* Tests whether any bit in the field is set.
*
* @param holder the long data containing the bits
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sentences should end in a period.

@theodoral22
Copy link
Copy Markdown
Author

Thank you for reviewing my PR.
I have made some changes based on your feedback.

@garydgregory
Copy link
Copy Markdown
Member

Adding another class seems heavy. What do you think about updating BitField instead like #1561 ?

@theodoral22
Copy link
Copy Markdown
Author

Yeah sure. That's perfect. Are you going to use the #1561?

@garydgregory
Copy link
Copy Markdown
Member

garydgregory commented Jan 19, 2026

@theodoral22
Yes, I think it would be better to use the same class as in #1561 , then this PR could be closed.

@theodoral22
Copy link
Copy Markdown
Author

Ok. Thank you for your cooperation.

@theodoral22
Copy link
Copy Markdown
Author

Just to make sure, do you want me to make other changes or everything’s ok?

@garydgregory
Copy link
Copy Markdown
Member

Hello @theodoral22
What you can do is apply PR #1561 to your local git repository and see if your use case works. Unless you can explain why this PR is better than #1561 or you find a bug in #1561, you can close this PR.
Thank you!

@theodoral22
Copy link
Copy Markdown
Author

Thank you for pointing that out. I checked PR #1561 and it covers my use case perfectly. I'll go ahead and close this PR in favor of #1561.

@garydgregory garydgregory mentioned this pull request Jan 19, 2026
6 tasks
@garydgregory
Copy link
Copy Markdown
Member

Hello @theodoral22

I merged #1561 and credited you in changes.xml. Please build from git master or use a 3.21.0-SNAPSHOT build from https://repository.apache.org/content/repositories/snapshots/ and test your use case.

Thank you!

@theodoral22
Copy link
Copy Markdown
Author

Hello @garydgregory
Thank you very much for the credit in changes.xml, I appreciate it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants