mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add method
This commit is contained in:
parent
5550617976
commit
14e53d6e3c
@ -638,10 +638,11 @@ public class ByteUtil {
|
|||||||
*
|
*
|
||||||
* @param buf 无符号bytes
|
* @param buf 无符号bytes
|
||||||
* @return 为 1 的个数
|
* @return 为 1 的个数
|
||||||
|
* @see Integer#bitCount(int)
|
||||||
*/
|
*/
|
||||||
public static int bitCount(final byte[] buf) {
|
public static int bitCount(final byte[] buf) {
|
||||||
int sum = 0;
|
int sum = 0;
|
||||||
for (byte b : buf) {
|
for (final byte b : buf) {
|
||||||
sum += Integer.bitCount((b & 0xFF));
|
sum += Integer.bitCount((b & 0xFF));
|
||||||
}
|
}
|
||||||
return sum;
|
return sum;
|
||||||
@ -654,9 +655,9 @@ public class ByteUtil {
|
|||||||
* @return 位数为1的索引集合
|
* @return 位数为1的索引集合
|
||||||
*/
|
*/
|
||||||
public static List<Integer> toUnsignedBitIndex(final byte[] bytes) {
|
public static List<Integer> toUnsignedBitIndex(final byte[] bytes) {
|
||||||
List<Integer> idxList = new LinkedList<>();
|
final List<Integer> idxList = new LinkedList<>();
|
||||||
StringBuilder sb = new StringBuilder();
|
final StringBuilder sb = new StringBuilder();
|
||||||
for (byte b : bytes) {
|
for (final byte b : bytes) {
|
||||||
sb.append(StrUtil.padPre(Integer.toBinaryString((b & 0xFF)), 8, "0"));
|
sb.append(StrUtil.padPre(Integer.toBinaryString((b & 0xFF)), 8, "0"));
|
||||||
}
|
}
|
||||||
final String bitStr = sb.toString();
|
final String bitStr = sb.toString();
|
||||||
|
@ -196,16 +196,15 @@ public class ByteUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void toUnsignedBitIndex() {
|
public void toUnsignedBitIndex() {
|
||||||
byte[] bytes = {0, 13, -64, -31, 101, 88, 47, -64};
|
final byte[] bytes = {0, 13, -64, -31, 101, 88, 47, -64};
|
||||||
List<Integer> list = ByteUtil.toUnsignedBitIndex(bytes);
|
final List<Integer> list = ByteUtil.toUnsignedBitIndex(bytes);
|
||||||
Console.log(list);
|
Assertions.assertEquals("[12, 13, 15, 16, 17, 24, 25, 26, 31, 33, 34, 37, 39, 41, 43, 44, 50, 52, 53, 54, 55, 56, 57]", list.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void bitCount() {
|
public void bitCount() {
|
||||||
byte[] bytes = {0, 13, -64, -31, 101, 88, 47, -64};
|
final byte[] bytes = {0, 13, -64, -31, 101, 88, 47, -64};
|
||||||
int count = ByteUtil.bitCount(bytes);
|
final int count = ByteUtil.bitCount(bytes);
|
||||||
Console.log(count);
|
|
||||||
Assertions.assertEquals(count, ByteUtil.toUnsignedBitIndex(bytes).size());
|
Assertions.assertEquals(count, ByteUtil.toUnsignedBitIndex(bytes).size());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user