List of utility methods to do Bits Convert to
byte | BitsNeeded(int n) Bits Needed byte ret = 1; if (n-- == 0) return 0; while ((n >>= 1) != 0) ++ret; return ret; |
int | bitsRequired(double minValue, double maxValue) computes the required number of bits to represent an range minValue...maxValue / 0..max(minValue,maxValue) In case on of the minValue < 0 also the bit for the sign is included long absmax = (long) Math.max(Math.abs(minValue), maxValue) + 1; int bits = 64 - Long.numberOfLeadingZeros(absmax); if (minValue < 0.0) bits++; return bits; |
int | bitsRequired(int value) Returns the number of bits required to store a number. int bits = 0; while (value != 0) { bits++; value >>= 1; return bits; |
int | bitsRequiredForFraction(String floatnumber) Determines in a Float String how much bits a necessary to represent the given Fraction with less error than the string. if (floatnumber.contains("eE")) { System.out.println("e float represenation not yet supported!"); int pos = floatnumber.indexOf("."); return (int) Math.ceil((floatnumber.length() - pos - 1) * 2.31); |
String | bitsString2(byte b) bits String int i = 0; String s = ""; while (i <= 7) { s += (b & (1 << i)) == 0 ? "0" : "1"; i++; return s; |
byte[] | bitsTo8Bytes(boolean[] bits) bits To Bytes byte[] b = new byte[8]; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { b[i] += (((bits[(8 * i) + j]) ? 1 : 0) << (7 - j)); return b; |
char | bitsToBase64(byte data) Converts a 6-bit value to the corresponding Base64 char if (data >= 0 && data < 26) { return (char) (data + 'A'); if (data >= 26 && data < 52) { return (char) (data - 26 + 'a'); if (data >= 52 && data < 62) { return (char) (data - 52 + '0'); ... |
double | bitsToDouble(String bits, double min, double max, int splits) Turns a bit string (0s and 1s) into a double. double j = 0; for (int i = 0; i < bits.length(); i++) { if (bits.charAt(i) == '1') { j = j + Math.pow(2, bits.length() - i - 1); j = Math.min(j, splits); return (min + j * ((max - min) / (double) (splits - 1))); ... |
float[] | bitsToFloats(boolean[] b) bits To Floats float[] f = new float[b.length]; for (int i = 0; i < b.length; i++) { f[i] = b[i] ? 1 : 0; return f; |
int | bitsToTransform(int src, int target) bits To Transform int counter = 0; while (!(src == 0 && target == 0)) { if (((src & 1) ^ (target & 1)) == 1) counter++; src >>= 1; target >>= 1; return counter; ... |