From a2506b927a3e6229ecfbbf5fb1b06e93af4871e2 Mon Sep 17 00:00:00 2001 From: Speiger Date: Tue, 30 Nov 2021 16:20:08 +0100 Subject: [PATCH] New Small feature - Added: 2 Helper functions to find out how many bits are required to store a Number. --- Changelog.md | 3 +++ .../src/collections/utils/HashUtil.java | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/Changelog.md b/Changelog.md index 7e6710b5..e4af2703 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,8 @@ # Changelog of versions +### Version 0.4.6 +- Added: 2 Helper functions to find out how many bits are required to store a Number. + ### Version 0.4.5 - Added: removeAll/retainAll(Collection c, Consumer r) which receives all the elements that got deleted from the collection - Fixed: Supplier get function wasn't referencing original function. diff --git a/src/main/java/speiger/src/collections/utils/HashUtil.java b/src/main/java/speiger/src/collections/utils/HashUtil.java index e3ae80e6..02235259 100644 --- a/src/main/java/speiger/src/collections/utils/HashUtil.java +++ b/src/main/java/speiger/src/collections/utils/HashUtil.java @@ -79,6 +79,26 @@ public class HashUtil return x + 1L; } + /** + * Function that finds out how many bits are required to store the number Provided + * @param value to get the required bits to store. + * @return the required bits to store that number + * @note Really useful for compression. Reducing data that is inserted into a GZIP algorithm. + */ + public static int getRequiredBits(int value) { + return Integer.bitCount(nextPowerOfTwo(value+1)-1); + } + + /** + * Function that finds out how many bits are required to store the number Provided + * @param value to get the required bits to store. + * @return the required bits to store that number + * @note Really useful for compression. Reducing data that is inserted into a GZIP algorithm. + */ + public static int getRequiredBits(long value) { + return Long.bitCount(nextPowerOfTwo(value+1L)-1L); + } + /** * Helper function that creates the ideal array size for HashMap * @param size the original array size