From 35060392991d0bb2d09f71843161869bcbf923ca Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Mon, 19 Jun 2017 15:47:45 +0200 Subject: [PATCH] [New] Added methods ByteHelper --- .../li/strolch/utils/helper/ByteHelper.java | 76 ++++++++++++++++++- 1 file changed, 72 insertions(+), 4 deletions(-) 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 f67317621..d4606cff3 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 @@ -74,12 +74,84 @@ public class ByteHelper { return (byte) (data | (1 << position)); } + public static short setBit(short data, int position) { + if (position > 15) + throw new IllegalStateException("Position " + position + " is not available in a short!"); + return (short) (data | (1 << position)); + } + + public static int setBit(int data, int position) { + if (position > 31) + throw new IllegalStateException("Position " + position + " is not available in an int!"); + return (data | (1 << position)); + } + + public static long setBit(long data, int position) { + if (position > 63) + throw new IllegalStateException("Position " + position + " is not available in a long!"); + return (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)); } + public static short clearBit(short data, int position) { + if (position > 15) + throw new IllegalStateException("Position " + position + " is not available in a short!"); + return (short) (data & ~(1 << position)); + } + + public static int clearBit(int data, int position) { + if (position > 31) + throw new IllegalStateException("Position " + position + " is not available in a int!"); + return (data & ~(1 << position)); + } + + public static long clearBit(long data, int position) { + if (position > 63) + throw new IllegalStateException("Position " + position + " is not available in a long!"); + return (data & ~(1 << position)); + } + + public static int countSetBits(byte activeChannels) { + int setBits = 0; + for (int i = 0; i < 8; i++) { + if (isBitSet(activeChannels, i)) + setBits++; + } + return setBits; + } + + public static int countSetBits(short activeChannels) { + int setBits = 0; + for (int i = 0; i < 16; i++) { + if (isBitSet(activeChannels, i)) + setBits++; + } + return setBits; + } + + public static int countSetBits(int activeChannels) { + int setBits = 0; + for (int i = 0; i < 32; i++) { + if (isBitSet(activeChannels, i)) + setBits++; + } + return setBits; + } + + public static int countSetBits(long activeChannels) { + int setBits = 0; + for (int i = 0; i < 64; i++) { + if (isBitSet(activeChannels, i)) + setBits++; + } + return setBits; + } + /** * 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 @@ -146,10 +218,6 @@ public class ByteHelper { return (short) (((high & 0xff) << 8) | (low & 0xff)); } - public static void main(String[] args) { - System.out.println(asBinary(toShort((byte) 0x01, (byte) 0x03))); - } - /** * Formats the given byte array to a binary string, separating each byte by a space *