Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/org/apache/commons/lang3/BitField.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public short getShortValue(final short holder) {
* @see #setValue(int,int)
*/
public int getValue(final int holder) {
return getRawValue(holder) >> shiftCount;
return getRawValue(holder) >>> shiftCount;
}

/**
Expand All @@ -212,7 +212,7 @@ public int getValue(final int holder) {
* @since 3.21.0
*/
public long getValue(final long holder) {
return getRawValue(holder) >> shiftCount;
return getRawValue(holder) >>> shiftCount;
}

/**
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/org/apache/commons/lang3/BitFieldTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,26 @@ void testGetValue() {
assertEquals(BF_ZERO.getValue(0), 0);
}

/**
* Tests that {@link BitField#getValue(int)} and {@link BitField#getValue(long)} shift the selected bits right without sign extension when the field occupies
* the top bit of the holder (bit 31 for int, bit 63 for long).
*/
@Test
void testGetValueTopBit() {
final BitField bit31 = new BitField(0x80000000);
assertEquals(bit31.getValue(-1), 1);
assertEquals(bit31.getValue(0), 0);
final BitField topByte = new BitField(0xFF000000);
assertEquals(topByte.getValue(0xFF000000), 255);
// The int and long overloads must agree for the same field and holder.
assertEquals(topByte.getValue(0xFF000000L), 255L);
Comment on lines +155 to +156
final BitField bit63 = new BitField(0x8000000000000000L);
assertEquals(bit63.getValue(0x8000000000000000L), 1L);
assertEquals(bit63.getValue(0L), 0L);
final BitField topNibble = new BitField(0xF000000000000000L);
assertEquals(topNibble.getValue(topNibble.setValue(0L, 15L)), 15L);
}

/**
* Tests that an int mask with the high bit set is treated as 32 unsigned bits on the long methods, instead of being sign-extended into bits 32-63.
*/
Expand Down
Loading