[New] Added functions to ByteHelper

This commit is contained in:
Robert von Burg 2017-05-25 18:22:37 +02:00
parent 9f53ce8a55
commit 928d7e94ee
1 changed files with 44 additions and 0 deletions

View File

@ -21,18 +21,62 @@ package li.strolch.utils.helper;
public class ByteHelper {
public static boolean isBitSet(byte data, int position) {
if (position > 7)
throw new IllegalStateException("Position " + position + " is not available in a byte!");
return ((data >> position) & 1) == 1;
}
public static boolean isBitSet(short data, int position) {
if (position > 15)
throw new IllegalStateException("Position " + position + " is not available in a short!");
return ((data >> position) & 1) == 1;
}
public static boolean isBitSet(int data, int position) {
if (position > 31)
throw new IllegalStateException("Position " + position + " is not available in an int!");
return ((data >> position) & 1) == 1;
}
public static boolean isBitSet(long data, int position) {
if (position > 63)
throw new IllegalStateException("Position " + position + " is not available in a long!");
return ((data >> position) & 1) == 1;
}
public static boolean isBitNotSet(byte data, int position) {
if (position > 7)
throw new IllegalStateException("Position " + position + " is not available in a byte!");
return ((data >> position) & 1) == 0;
}
public static boolean isBitNotSet(short data, int position) {
if (position > 15)
throw new IllegalStateException("Position " + position + " is not available in a short!");
return ((data >> position) & 1) == 0;
}
public static boolean isBitNotSet(int data, int position) {
if (position > 31)
throw new IllegalStateException("Position " + position + " is not available in an int!");
return ((data >> position) & 1) == 0;
}
public static boolean isBitNotSet(long data, int position) {
if (position > 63)
throw new IllegalStateException("Position " + position + " is not available in a long!");
return ((data >> position) & 1) == 0;
}
public static byte setBit(byte data, int position) {
if (position > 7)
throw new IllegalStateException("Position " + position + " is not available in a byte!");
return (byte) (data | (1 << position));
}
public static byte clearBit(byte data, int position) {
if (position > 7)
throw new IllegalStateException("Position " + position + " is not available in a byte!");
return (byte) (data & ~(1 << position));
}