BAEL-4286 How to get the value of a bit at a certain position from a byte (#11396)

* Convert Byte Array to its Numeric Representation

* Remove Redundant Getter Method

* BAEL-4286 How to get the value of a bit at a certain position from a byte

* BAEL-4286(update): remove redundant test methods
This commit is contained in:
lsieun 2021-12-24 15:45:44 +08:00 committed by GitHub
parent a7e7caaadf
commit 867d6573b3
2 changed files with 83 additions and 0 deletions

View File

@ -15,6 +15,11 @@
</parent>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>

View File

@ -0,0 +1,78 @@
package com.baeldung.getbit;
import org.junit.Test;
import java.math.BigInteger;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class GetABitFromIntegralValueUnitTest {
@Test
public void givenAByte_whenUsingAHardCodedMask_thenGetBitValue() {
byte val1 = 0b0110_0100;
byte val2 = 0b0110_0010;
byte mask = 0b0000_0100;
boolean isSet1 = (val1 & mask) > 0;
boolean isSet2 = (val2 & mask) > 0;
assertTrue(isSet1);
assertFalse(isSet2);
}
@Test
public void givenAnIntValue_whenUsingACalculatedMask_thenGetBitValue() {
int val = 0b0110_0100;
int pos = 2;
int mask = 1 << pos;
boolean isSet = (val & mask) > 0;
assertTrue(isSet);
}
@Test
public void givenAnIntValue_whenUsingALeftShiftedValue1_thenGetBitValue() {
int val = 0b0110_0100;
int pos = 2;
boolean isSet = ((val << (31 - pos)) < 0);
assertTrue(isSet);
}
@Test
public void givenAnIntValue_whenUsingALeftShiftedValue2_thenGetBitValue() {
int val = 0b0110_0100;
int pos = 2;
boolean isSet = ((val << (~pos & 31)) < 0);
assertTrue(isSet);
}
@Test
public void givenAnIntValue_whenUsingALeftShiftedValue3_thenGetBitValue() {
int val = 0b0110_0100;
int pos = 2;
boolean isSet = ((val << ~pos) < 0);
assertTrue(isSet);
}
@Test
public void givenAnIntValue_whenUsingARightShiftedValue_thenGetBitValue() {
int val = 0b0110_0100;
int pos = 2;
boolean isSet = ((val >> pos) & 1) == 1;
assertTrue(isSet);
}
@Test
public void givenAnIntValue_whenUsingBigInteger_thenGetBitValue() {
int val = 0b0110_0100;
int pos = 2;
boolean isSet = BigInteger.valueOf(val).testBit(pos);
assertTrue(isSet);
}
}