From 928d7e94ee72320d66e8c7ec4d9adb816cb5b0eb Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Thu, 25 May 2017 18:22:37 +0200 Subject: [PATCH] [New] Added functions to ByteHelper --- .../li/strolch/utils/helper/ByteHelper.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/li.strolch.utils/src/main/java/li/strolch/utils/helper/ByteHelper.java b/li.strolch.utils/src/main/java/li/strolch/utils/helper/ByteHelper.java index 2c68a8c13..f67317621 100644 --- a/li.strolch.utils/src/main/java/li/strolch/utils/helper/ByteHelper.java +++ b/li.strolch.utils/src/main/java/li/strolch/utils/helper/ByteHelper.java @@ -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)); }