A list of bits. : Binary Bit « Language Basics « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Language Basics » Binary BitScreenshots 
A list of bits.
     
/*
 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
 * Version 1.0, and under the Eclipse Public License, Version 1.0
 * (http://h2database.com/html/license.html).
 * Initial Developer: H2 Group
 */
//package org.h2.util;

/**
 * A list of bits.
 */
public final class BitField {

    private static final int ADDRESS_BITS = 6;
    private static final int BITS = 64;
    private static final int ADDRESS_MASK = BITS - 1;
    private long[] data;
    private int maxLength;

    public BitField() {
        this(64);
    }

    public BitField(int capacity) {
        data = new long[capacity >>> 3];
    }

    /**
     * Get the index of the next bit that is not set.
     *
     @param fromIndex where to start searching
     @return the index of the next disabled bit
     */
    public int nextClearBit(int fromIndex) {
        int i = fromIndex >> ADDRESS_BITS;
        int max = data.length;
        for (; i < max; i++) {
            if (data[i== -1) {
                continue;
            }
            int j = Math.max(fromIndex, i << ADDRESS_BITS);
            for (int end = j + 64; j < end; j++) {
                if (!get(j)) {
                    return j;
                }
            }
        }
        return max << ADDRESS_BITS;
    }

    /**
     * Get the bit at the given index.
     *
     @param i the index
     @return true if the bit is enabled
     */
    public boolean get(int i) {
        int addr = i >> ADDRESS_BITS;
        if (addr >= data.length) {
            return false;
        }
        return (data[addr& getBitMask(i)) != 0;
    }

    /**
     * Get the next 8 bits at the given index.
     * The index must be a multiple of 8.
     *
     @param i the index
     @return the next 8 bits
     */
    public int getByte(int i) {
        int addr = i >> ADDRESS_BITS;
        if (addr >= data.length) {
            return 0;
        }
        return (int) (data[addr>>> (i & (<< 3)) 255);
    }

    /**
     * Combine the next 8 bits at the given index with OR.
     * The index must be a multiple of 8.
     *
     @param i the index
     @param x the next 8 bits (0 - 255)
     */
    public void setByte(int i, int x) {
        int addr = i >> ADDRESS_BITS;
        checkCapacity(addr);
        data[addr|= ((longx<< (i & (<< 3));
        if (maxLength < i && x != 0) {
            maxLength = i + 7;
        }
    }

    /**
     * Set bit at the given index to 'true'.
     *
     @param i the index
     */
    public void set(int i) {
        int addr = i >> ADDRESS_BITS;
        checkCapacity(addr);
        data[addr|= getBitMask(i);
        if (maxLength < i) {
            maxLength = i;
        }
    }

    /**
     * Set bit at the given index to 'false'.
     *
     @param i the index
     */
    public void clear(int i) {
        int addr = i >> ADDRESS_BITS;
        if (addr >= data.length) {
            return;
        }
        data[addr&= ~getBitMask(i);
    }

    private static long getBitMask(int i) {
        return 1L << (i & ADDRESS_MASK);
    }

    private void checkCapacity(int size) {
        if (size >= data.length) {
            expandCapacity(size);
        }
    }

    private void expandCapacity(int size) {
        while (size >= data.length) {
            int newSize = data.length == : data.length * 2;
            long[] d = new long[newSize];
            System.arraycopy(data, 0, d, 0, data.length);
            data = d;
        }
    }

    /**
     * Enable or disable a number of bits.
     *
     @param fromIndex the index of the first bit to enable or disable
     @param toIndex one plus the index of the last bit to enable or disable
     @param value the new value
     */
    public void set(int fromIndex, int toIndex, boolean value) {
        // go backwards so that OutOfMemory happens
        // before some bytes are modified
        for (int i = toIndex - 1; i >= fromIndex; i--) {
            set(i, value);
        }
        if (value) {
            if (toIndex > maxLength) {
                maxLength = toIndex;
            }
        else {
            if (toIndex >= maxLength) {
                maxLength = fromIndex;
            }
        }
    }

    private void set(int i, boolean value) {
        if (value) {
            set(i);
        else {
            clear(i);
        }
    }

    /**
     * Get the index of the highest set bit plus one, or 0 if no bits are set.
     *
     @return the length of the bit field
     */
    public int length() {
        int m = maxLength >> ADDRESS_BITS;
        while (m > && data[m== 0) {
            m--;
        }
        maxLength = (m << ADDRESS_BITS+
            (64 - Long.numberOfLeadingZeros(data[m]));
        return maxLength;
    }

}

   
    
    
    
    
  
Related examples in the same category
1. Utility for byte swapping of all java data types
2.Bitwise DemoBitwise Demo
3.Binary Digits
4.Using the bitwise operatorsUsing the bitwise operators
5.Bitwise complement (~): inverts ones and zeroes in a number
6.Convert a number to negative and back
7.Bitwise AND (&)
8.Bitwise OR (|)
9.Bitwise XOR (^)
10.Left shift (<<)
11.Performing Bitwise Operations on a Bit Vector
12.Converting Between a BitSet and a Byte Array
13.Returns a byte array of at least length 1
14.Shift to the left
15.Signed shift to the right
16.Unsigned shift to the right
17.Bit-level unpacking of floating-point data
18.Gets the a single bit of the target.
19.Returns 16 bits from the long number.
20.Sets a specific bit of an int.
21.Fuses the lower 16 bits of two ints.
22.Class to represent unsigned 32-bit numbers.
23.Gets the specified bit (0-31) from the integer argument.
24.Sets the specified bit (0-31) in the integer argument.
25.Clears a range of bits in the specified integer.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.