Skip to content

Commit 88c9f98

Browse files
committed
Add bitAt() feature
1 parent 20fef49 commit 88c9f98

File tree

6 files changed

+55
-19
lines changed

6 files changed

+55
-19
lines changed

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Releases
22

3+
## v0.4.1
4+
5+
* add bitAt() utility
6+
37
## v0.4.0
48

59
* logical expressions in validators

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ It's main features include:
3636
* **Validators** with the ability to arbitrarily combine multiple ones
3737
* **Parsing and Encoding** in most common binary-to-text-encodings: [hex](https://en.wikipedia.org/wiki/Hexadecimal), [base64](https://en.wikipedia.org/wiki/Base64), ...
3838
* **Immutable, Mutable and Read-Only** versions
39-
* **Utility Features** like `indexOf`, `count`, `isEmpty`, `byteAt`, ...
39+
* **Utility Features** like `indexOf`, `count`, `isEmpty`, `bitAt`, ...
4040

4141
It is written in Java 7 to keep backwards compatibility with *Android* and older *Java* applications.
4242

@@ -259,6 +259,7 @@ Bytes.wrap(array).isEmpty();
259259
And others:
260260

261261
```java
262+
Bytes.wrap(array).bitAt(4);
262263
Bytes.wrap(array).byteAt(14);
263264
Bytes.wrap(array).count(0x01); //occurrence of 0x01
264265
Bytes.wrap(array).entropy();

pom.xml

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66

77
<groupId>at.favre.lib</groupId>
88
<artifactId>bytes</artifactId>
9-
<version>0.4.0</version>
9+
<version>0.4.1</version>
1010
<packaging>jar</packaging>
1111

12-
<name>Bytes Java Primitive Tools</name>
13-
<description>Convenience toolkit for easier handling, creating, converting and encoding of any form of byte arrays.
14-
Per default everything is immutable, but also supports faster mutable version.
12+
<name>Bytes Utility Library</name>
13+
<description>Bytes is a utility library that makes it easy to create, parse, transform, validate and convert byte
14+
arrays in Java. It supports endianness as well as immutability and mutability, so the caller may decide to favor
15+
performance.
1516
</description>
1617
<url>https://github.com/patrickfav/bytes-java</url>
1718
<inceptionYear>2017</inceptionYear>
@@ -163,18 +164,6 @@
163164
<version>4.12</version>
164165
<scope>test</scope>
165166
</dependency>
166-
<dependency>
167-
<groupId>org.apache.commons</groupId>
168-
<artifactId>commons-lang3</artifactId>
169-
<version>3.6</version>
170-
<scope>test</scope>
171-
</dependency>
172-
<dependency>
173-
<groupId>commons-codec</groupId>
174-
<artifactId>commons-codec</artifactId>
175-
<version>1.10</version>
176-
<scope>test</scope>
177-
</dependency>
178167
</dependencies>
179168
<developers>
180169
<developer>

src/main/java/at/favre/lib/bytes/Bytes.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,7 @@ public int lastIndexOf(byte target) {
926926
*
927927
* @param index the index of the {@code byte} value.
928928
* @return the {@code byte} value at the specified index of the underlying byte array.
929-
* @throws IndexOutOfBoundsException if the {@code index} argument is negative or not less than the length of this string.
929+
* @throws IndexOutOfBoundsException if the {@code index} argument is negative or not less than the length of this array.
930930
*/
931931
public byte byteAt(int index) {
932932
if (index < 0 || index > length()) {
@@ -935,6 +935,20 @@ public byte byteAt(int index) {
935935
return internalArray()[index];
936936
}
937937

938+
/**
939+
* Returns the {@code bit} value as boolean at the specified index.
940+
*
941+
* @param bitIndex the index of the {@code bit} value.
942+
* @return true if bit at given index is set, false otherwise
943+
* @throws IndexOutOfBoundsException if the {@code bitIndex} argument is negative or not less than the length of this array in bits.
944+
*/
945+
public boolean bitAt(int bitIndex) {
946+
if (bitIndex < 0 || bitIndex > length() * 8) {
947+
throw new IndexOutOfBoundsException("cannot get bit from index out of bounds: " + bitIndex);
948+
}
949+
return (byteAt(bitIndex / 8) >> (bitIndex % 8) & 1) == 1;
950+
}
951+
938952
/**
939953
* Traverses the internal byte array counts the occurrences of given byte.
940954
* This has a time complexity of O(n).

src/main/java/at/favre/lib/bytes/BytesTransformer.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,6 @@ public byte[] transform(byte[] currentArray, boolean inPlace) {
303303
throw new IllegalArgumentException("bit index " + (position * 8) + " out of bounds");
304304
}
305305

306-
307306
int bytePosition = currentArray.length - 1 - position / 8;
308307
if (newBitValue == null) {
309308
out[bytePosition] ^= (1 << position % 8);

src/test/java/at/favre/lib/bytes/BytesMiscTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,35 @@ public void byteAt() throws Exception {
160160
}
161161
}
162162

163+
@Test
164+
public void bitAt() throws Exception {
165+
for (int i = 0; i < 8; i++) {
166+
assertFalse(Bytes.allocate(1).bitAt(i));
167+
}
168+
169+
for (int i = 0; i < 8; i++) {
170+
assertTrue(Bytes.from((byte) 0xFF).bitAt(i));
171+
}
172+
173+
assertFalse(Bytes.from((byte) 8).bitAt(0));
174+
assertFalse(Bytes.from((byte) 8).bitAt(1));
175+
assertFalse(Bytes.from((byte) 8).bitAt(2));
176+
assertTrue(Bytes.from((byte) 8).bitAt(3));
177+
assertFalse(Bytes.from((byte) 8).bitAt(4));
178+
179+
try {
180+
Bytes.allocate(1).bitAt(8);
181+
fail();
182+
} catch (IndexOutOfBoundsException e) {
183+
}
184+
185+
try {
186+
Bytes.allocate(16).bitAt(-1);
187+
fail();
188+
} catch (IndexOutOfBoundsException e) {
189+
}
190+
}
191+
163192
@Test
164193
public void count() throws Exception {
165194
assertEquals(0, Bytes.allocate(0).count((byte) 0));

0 commit comments

Comments
 (0)