FileDocCategorySizeDatePackage
Bits.javaAPI DocAndroid 1.5 API7121Wed May 06 22:41:02 BST 2009com.android.dx.util

Bits

public final class Bits extends Object
Utilities for treating int[]s as bit sets.

Fields Summary
Constructors Summary
private Bits()
This class is uninstantiable.

        // This space intentionally left blank.
    
Methods Summary
public static booleananyInRange(int[] bits, int start, int end)
Returns whether any bits are set to true in the specified range.

param
bits non-null; bit set to operate on
param
start >= 0; index of the first bit in the range (inclusive)
param
end >= 0; index of the last bit in the range (exclusive)
return
true if any bit is set to true in the indicated range

        int idx = findFirst(bits, start);
        return (idx >= 0) && (idx < end);
    
public static intbitCount(int[] bits)
Gets the number of bits set to true in the given bit set.

param
bits non-null; bit set to operate on
return
>= 0; the bit count (aka population count) of the set

        int len = bits.length;
        int count = 0;

        for (int i = 0; i < len; i++) {
            count += Integer.bitCount(bits[i]);
        }

        return count;
    
public static voidclear(int[] bits, int idx)
Sets the given bit to false.

param
bits non-null; bit set to operate on
param
idx >= 0, < getMax(set); which bit

        int arrayIdx = idx >> 5;
        int bit = 1 << (idx & 0x1f);
        bits[arrayIdx] &= ~bit;
    
public static intfindFirst(int[] bits, int idx)
Finds the lowest-order bit set at or after the given index in the given bit set.

param
bits non-null; bit set to operate on
param
idx >= 0; minimum index to return
return
>= -1; lowest-order bit set at or after idx, or -1 if there is no appropriate bit index to return

        int len = bits.length;
        int minBit = idx & 0x1f;

        for (int arrayIdx = idx >> 5; arrayIdx < len; arrayIdx++) {
            int word = bits[arrayIdx];
            if (word != 0) {
                int bitIdx = findFirst(word, minBit);
                if (bitIdx >= 0) {
                    return (arrayIdx << 5) + bitIdx;
                }
            }
            minBit = 0;
        }

        return -1;
    
public static intfindFirst(int value, int idx)
Finds the lowest-order bit set at or after the given index in the given int.

param
value the value in question
param
idx 0..31 the minimum bit index to return
return
>= -1; lowest-order bit set at or after idx, or -1 if there is no appropriate bit index to return

        value &= ~((1 << idx) - 1); // Mask off too-low bits.
        int result = Integer.numberOfTrailingZeros(value);
        return (result == 32) ? -1 : result;
    
public static booleanget(int[] bits, int idx)
Gets the value of the bit at the given index.

param
bits non-null; bit set to operate on
param
idx >= 0, < getMax(set); which bit
return
the value of the indicated bit

        int arrayIdx = idx >> 5;
        int bit = 1 << (idx & 0x1f);
        return (bits[arrayIdx] & bit) != 0;
    
public static intgetMax(int[] bits)
Gets the maximum index (exclusive) for the given bit set.

param
bits non-null; bit set in question
return
>= 0; the maximum index (exclusive) that may be set

        return bits.length * 0x20;
    
public static booleanisEmpty(int[] bits)
Returns whether or not the given bit set is empty, that is, whether no bit is set to true.

param
bits non-null; bit set to operate on
return
true iff all bits are false

        int len = bits.length;

        for (int i = 0; i < len; i++) {
            if (bits[i] != 0) {
                return false;
            }
        }

        return true;
    
public static int[]makeBitSet(int max)
Constructs a bit set to contain bits up to the given index (exclusive).

param
max >= 0; the maximum bit index (exclusive)
return
non-null; an appropriately-constructed instance

        int size = (max + 0x1f) >> 5;
        return new int[size];
    
public static voidor(int[] a, int[] b)
Ors bit array b into bit array a. a.length must be greater than or equal to b.length.

param
a non-null; int array to be ored with other argument. This argument is modified.
param
b non-null; int array to be ored into a. This argument is not modified.

        for (int i = 0; i < b.length; i++) {
            a[i] |= b[i];
        }
    
public static voidset(int[] bits, int idx, boolean value)
Sets the given bit to the given value.

param
bits non-null; bit set to operate on
param
idx >= 0, < getMax(set); which bit
param
value the new value for the bit

        int arrayIdx = idx >> 5;
        int bit = 1 << (idx & 0x1f);

        if (value) {
            bits[arrayIdx] |= bit;
        } else {
            bits[arrayIdx] &= ~bit;
        }
    
public static voidset(int[] bits, int idx)
Sets the given bit to true.

param
bits non-null; bit set to operate on
param
idx >= 0, < getMax(set); which bit

        int arrayIdx = idx >> 5;
        int bit = 1 << (idx & 0x1f);
        bits[arrayIdx] |= bit;
    
public static java.lang.StringtoHuman(int[] bits)

        StringBuilder sb = new StringBuilder();

        boolean needsComma = false;

        sb.append('{");

        int bitsLength = 32 * bits.length;
        for (int i = 0; i < bitsLength; i++) {
            if (Bits.get(bits, i)) {
                if (needsComma) {
                    sb.append(',");
                }
                needsComma = true;
                sb.append(i);
            }
        }
        sb.append('}");

        return sb.toString();