List of utility methods to do Bits Convert to
String | bitStr(long value) bit Str StringBuffer buf = new StringBuffer(); for (int i = 0; i < Long.SIZE; i++) { buf.append((value & 0x01) + ""); value >>= 1; buf.reverse(); return buf.toString(); |
String | bitString(boolean value) Return a string representation of "0" or "1" as the value of a boolean. return value ? "1" : "0"; |
String | bitString(byte b) bit String StringBuilder buff = new StringBuilder(); boolean[] array = byteToBitArray(b); for (int i = 0; i < array.length; i++) { buff.append(array[i] ? 1 : 0); return buff.toString(); |
byte | bitString2byte(String str) bit Stringbyte int res = 0; if (str.length() > 8) throw new UnsupportedOperationException("string length may be max 8"); for (int i = 0; i < str.length(); i++) { res = res << 1; if ('1' == str.charAt(i)) { res |= 1; } else if ('0' == str.charAt(i)) { ... |
int | bitStringToInt(String bits, char one) bit String To Int int result = 0; int len = bits.length(); for (int i = 0; i < len; i++) { if (bits.charAt(i) == one) result |= 1 << (len - i - 1); return result; |
int | bitsUsed(long value) bits Used if (value < 0) return 64; for (int i = 62; i > 0; i--) if (((value >> i) & 1) == 1) return i + 1; return 1; |
boolean | bitTest(String value, int pos) Bit Test - test the bit for the given position in the given string Passing pos = 0 and value = "100" would yield false Passing pos = 1 and value = "100" would yield false Passing pos = 2 and value = "100" would yield true int valueLength = value.length(); if (pos >= valueLength) { return false; int charPos = valueLength - pos - 1; char bit = value.charAt(charPos); return (bit == '1'); |
boolean | bitToBoolean(byte b) bit To Boolean if (0 == b) return false; else return true; |
boolean | bitToBoolean(final byte value, final int bitNbr) Gets the bit state of a specific bit from a byte as boolean .
if (bitNbr < 0 || bitNbr > 7) { throw new IllegalArgumentException("Parameter bitNbr is out of range (0-7)"); return (value & BIT[bitNbr]) != 0; |
long | bitToLong(byte[] bytes, int offset, int length) Interpret a BIT value as a long. long valueAsLong = 0; for (int i = 0; i < length; i++) { valueAsLong = valueAsLong << 8 | bytes[offset + i] & 0xff; return valueAsLong; |