Java Utililty Methods Bits Convert to

List of utility methods to do Bits Convert to

Description

The list of methods to do Bits Convert to are organized into topic(s).

Method

StringbitStr(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();
StringbitString(boolean value)
Return a string representation of "0" or "1" as the value of a boolean.
return value ? "1" : "0";
StringbitString(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();
bytebitString2byte(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)) {
...
intbitStringToInt(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;
intbitsUsed(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;
booleanbitTest(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');
booleanbitToBoolean(byte b)
bit To Boolean
if (0 == b)
    return false;
else
    return true;
booleanbitToBoolean(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;
longbitToLong(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;