[New] ByteHelper.getHighestBit()

This commit is contained in:
Robert von Burg 2021-02-17 18:06:40 +01:00
parent 3ae5b766d2
commit 2ea91eb5d3
1 changed files with 53 additions and 8 deletions

View File

@ -116,42 +116,69 @@ public class ByteHelper {
return (data & ~(1 << position)); return (data & ~(1 << position));
} }
public static int countSetBits(byte activeChannels) { public static int countSetBits(byte data) {
int setBits = 0; int setBits = 0;
for (int i = 0; i < 8; i++) { for (int i = 0; i < 8; i++) {
if (isBitSet(activeChannels, i)) if (isBitSet(data, i))
setBits++; setBits++;
} }
return setBits; return setBits;
} }
public static int countSetBits(short activeChannels) { public static int countSetBits(short data) {
int setBits = 0; int setBits = 0;
for (int i = 0; i < 16; i++) { for (int i = 0; i < 16; i++) {
if (isBitSet(activeChannels, i)) if (isBitSet(data, i))
setBits++; setBits++;
} }
return setBits; return setBits;
} }
public static int countSetBits(int activeChannels) { public static int countSetBits(int data) {
int setBits = 0; int setBits = 0;
for (int i = 0; i < 32; i++) { for (int i = 0; i < 32; i++) {
if (isBitSet(activeChannels, i)) if (isBitSet(data, i))
setBits++; setBits++;
} }
return setBits; return setBits;
} }
public static int countSetBits(long activeChannels) { public static int countSetBits(long data) {
int setBits = 0; int setBits = 0;
for (int i = 0; i < 64; i++) { for (int i = 0; i < 64; i++) {
if (isBitSet(activeChannels, i)) if (isBitSet(data, i))
setBits++; setBits++;
} }
return setBits; return setBits;
} }
public static byte getHighestBit(byte data) {
byte pos = 0;
for (byte i = 0; i < Byte.SIZE; i++) {
if (isBitSet(data, i))
pos = i;
}
return pos;
}
public static byte getHighestBit(short data) {
byte pos = 0;
for (byte i = 0; i < Short.SIZE; i++) {
if (isBitSet(data, i))
pos = i;
}
return pos;
}
public static byte getHighestBit(int data) {
byte pos = 0;
for (byte i = 0; i < Integer.SIZE; i++) {
if (isBitSet(data, i))
pos = i;
}
return pos;
}
/** /**
* Creates a long of the given byte array. They byte array must be 8 bytes long. The byte at index 0 is the highest * Creates a long of the given byte array. They byte array must be 8 bytes long. The byte at index 0 is the highest
* byte * byte
@ -260,6 +287,24 @@ public class ByteHelper {
return (byte) (0x0f & b); return (byte) (0x0f & b);
} }
public static byte parseBinaryToByte(String value) {
if (value.length() != Byte.SIZE)
throw new IllegalStateException("parsing binary to byte requires exactly " + Byte.SIZE + " digits!");
return (byte) Integer.parseInt(value, 2);
}
public static short parseBinaryToShort(String value) {
if (value.length() != Short.SIZE)
throw new IllegalStateException("parsing binary to short requires exactly " + Short.SIZE + " digits!");
return (short) Integer.parseInt(value, 2);
}
public static int parseBinaryToInt(String value) {
if (value.length() != Integer.SIZE)
throw new IllegalStateException("parsing binary to int requires exactly " + Integer.SIZE + " digits!");
return Integer.parseInt(value, 2);
}
/** /**
* Formats the given byte array to a binary string, separating each byte by a space * Formats the given byte array to a binary string, separating each byte by a space
* *