From 2ea91eb5d39bfd4c33ad68438c64645aa15e8e0b Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Wed, 17 Feb 2021 18:06:40 +0100 Subject: [PATCH] [New] ByteHelper.getHighestBit() --- .../li/strolch/utils/helper/ByteHelper.java | 61 ++++++++++++++++--- 1 file changed, 53 insertions(+), 8 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 6d6cca4da..e912c74b6 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 @@ -116,42 +116,69 @@ public class ByteHelper { return (data & ~(1 << position)); } - public static int countSetBits(byte activeChannels) { + public static int countSetBits(byte data) { int setBits = 0; for (int i = 0; i < 8; i++) { - if (isBitSet(activeChannels, i)) + if (isBitSet(data, i)) setBits++; } return setBits; } - public static int countSetBits(short activeChannels) { + public static int countSetBits(short data) { int setBits = 0; for (int i = 0; i < 16; i++) { - if (isBitSet(activeChannels, i)) + if (isBitSet(data, i)) setBits++; } return setBits; } - public static int countSetBits(int activeChannels) { + public static int countSetBits(int data) { int setBits = 0; for (int i = 0; i < 32; i++) { - if (isBitSet(activeChannels, i)) + if (isBitSet(data, i)) setBits++; } return setBits; } - public static int countSetBits(long activeChannels) { + public static int countSetBits(long data) { int setBits = 0; for (int i = 0; i < 64; i++) { - if (isBitSet(activeChannels, i)) + if (isBitSet(data, i)) 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 * byte @@ -260,6 +287,24 @@ public class ByteHelper { 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 *