Java Bits Convert to bitscanForward(long bitboard)

Here you can find the source of bitscanForward(long bitboard)

Description

Returns the next 1 bit in the bitboard.

License

BSD License

Declaration

public static final int bitscanForward(long bitboard) 

Method Source Code

//package com.java2s;
/**// w w w. j  a v a 2 s  .c  om
 * New BSD License
 * http://www.opensource.org/licenses/bsd-license.php
 * Copyright 2009-2011 RaptorProject (http://code.google.com/p/raptor-chess-interface/)
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 * Neither the name of the RaptorProject nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

public class Main {
    private static final long DE_BRUJIN = 0x03f79d71b4cb0a89L;
    private static final int[] DE_BRUJIN_MAGICS_TABLE = { 0, 1, 48, 2, 57, 49, 28, 3, 61, 58, 50, 42, 38, 29, 17, 4,
            62, 55, 59, 36, 53, 51, 43, 22, 45, 39, 33, 30, 24, 18, 12, 5, 63, 47, 56, 27, 60, 41, 37, 16, 54, 35,
            52, 21, 44, 32, 23, 11, 46, 26, 40, 15, 34, 20, 31, 10, 25, 14, 19, 9, 13, 8, 7, 6, };

    /**
     * Returns the next 1 bit in the bitboard. Returns 0 if bitboard is 0.
     */
    public static final int bitscanForward(long bitboard) {
        // Slower on intel
        // return Long.numberOfTrailingZeros(bitboard);
        return bitScanForwardDeBruijn64(bitboard);
    }

    public static int bitScanForwardDeBruijn64(long b) {
        int idx = (int) ((b & -b) * DE_BRUJIN >>> 58);
        return DE_BRUJIN_MAGICS_TABLE[idx];
    }
}

Related

  1. bitrv216neg(double[] a, int offa)
  2. bitrv2conj(int n, int[] ip, double[] a, int offa)
  3. bits2float(int i)
  4. bits2Numeric(boolean[] in)
  5. bitsArrayToByte(byte[] bits)
  6. bitsCleanup(int base, int[] bits)
  7. BitsNeeded(int n)
  8. bitsRequired(double minValue, double maxValue)
  9. bitsRequired(int value)