Ejemplo n.º 1
0
        /// <summary>
        ///  从低位向高位扫描,返回第一次出现1的位置,同时将该位置为0
        ///  例如:二进制数 (高位在右)0001001001100(低位在左),返回2,同时bb更改为0001001001000
        /// </summary>
        /// <param name="u64">一个64位整数,注意:不能为0</param>
        /// <returns>返回值从0到63, 最低的1所在的位置</returns>
        public static int BitScanForwardWithReset(ref ulong u64)
        { // also called dropForward
            int idx = BitHack64.BitScanForward(u64);

            u64 &= u64 - 1; // reset bit outside
            return(idx);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 得到棋子的类型,是7种类型之一,实际对应着从1到7的整数
        /// 通过把它们从0到6编号,可以在编程中减少许多if...else if或switch语句
        /// </summary>
        /// <param name="piece">棋子的ID号:RED_ROOK,RED_KNIGHT,...RED_KING,BLACK_ROOK,BLACK_KNIGHT,...BLACK_KING</param>
        /// <returns>返回TYPE_ROOK,...TYPE_KING这7个整数之一;其它情况都返回-1</returns>
        public static int PieceType(int piece)
        {
            // 提高性能,因为b0,b1,...,b6分别代表不同的棋子,这样只要找到1出现的位置就是棋子的类型
            // 参看TYPE_ROOK和RED_ROOK, BLACK_ROOK等常量的定义即可
            return(BitHack64.Log2((ulong)piece & 0x7F));

            //if (piece == RED_ROOK) return TYPE_ROOK;
            //else if (piece == RED_KNIGHT) return TYPE_KNIGHT;
            //else if (piece == RED_CANNON) return TYPE_CANNON;
            //else if (piece == RED_BISHOP) return TYPE_BISHOP;
            //else if (piece == RED_ADVISOR) return TYPE_ADVISOR;
            //else if (piece == RED_PAWN) return TYPE_PAWN;
            //else if (piece == RED_KING) return TYPE_KING;

            //else if (piece == BLACK_ROOK) return TYPE_ROOK;
            //else if (piece == BLACK_KNIGHT) return TYPE_KNIGHT;
            //else if (piece == BLACK_CANNON) return TYPE_CANNON;
            //else if (piece == BLACK_BISHOP) return TYPE_BISHOP;
            //else if (piece == BLACK_ADVISOR) return TYPE_ADVISOR;
            //else if (piece == BLACK_PAWN) return TYPE_PAWN;
            //else if (piece == BLACK_KING) return TYPE_KING;
            //else return -1;
        }