Java Bits Convert to bitFieldToIndexArray(long bitfield)

Here you can find the source of bitFieldToIndexArray(long bitfield)

Description

Get the indexes of the values

License

Open Source License

Declaration

public static short[] bitFieldToIndexArray(long bitfield) 

Method Source Code

//package com.java2s;
/******************************************************************************
 * Copyright (c) 2004-2008 Whirlwind Match Limited. All rights reserved.
 *
 * This is open source software; you can use, redistribute and/or modify
 * it under the terms of the Open Software Licence v 3.0 as published by the 
 * Open Source Initiative./*w  w  w . j  a  va 2s  .  c  o m*/
 *
 * You should have received a copy of the Open Software Licence along with this
 * application. if not, contact the Open Source Initiative (www.opensource.org)
 *****************************************************************************/

public class Main {
    /**
     * Get the indexes of the values
     */
    public static short[] bitFieldToIndexArray(long bitfield) {
        int entries = countBitsSet(bitfield);
        short[] vals = new short[entries];
        int currEntry = 0;
        for (short i = 0; i < Long.SIZE; i++) {
            if ((bitfield & 0x01) != 0) {
                vals[currEntry++] = i;
            }
            bitfield >>>= 1;
        }
        return vals;
    }

    /**
     * Get the indexes of the values
     */
    public static short[] bitFieldToIndexArray(int bitfield) {
        int entries = countBitsSet(bitfield);
        short[] vals = new short[entries];
        int currEntry = 0;
        for (short i = 0; i < Long.SIZE; i++) {
            if ((bitfield & 0x01) != 0) {
                vals[currEntry++] = i;
            }
            bitfield >>>= 1;
        }
        return vals;
    }

    public static int countBitsSet(int bitfield) {
        int count = 0;
        for (short i = 0; i < Integer.SIZE; i++) {
            if ((bitfield & 0x01) != 0) {
                count++;
            }
            bitfield >>>= 1;
        }
        return count;
    }

    public static int countBitsSet(long bitfield) {
        int count = 0;
        for (short i = 0; i < Long.SIZE; i++) {
            if ((bitfield & 0x01) != 0) {
                count++;
            }
            bitfield >>>= 1;
        }
        return count;
    }
}

Related

  1. bitArrayToByte(byte[] bytes)
  2. BitArrayToString(Boolean[] asciiBinary)
  3. bitboardToString(final long l)
  4. bitboardToString(long bitboard)
  5. bitch(Throwable t)
  6. bitFieldToString(boolean[] bits)
  7. bitfieldToString(String[] statenames, int value)
  8. bitFlagByteLength(final int nbits)
  9. bitHistogram(int[] data)