Ntz() public static method

Returns number of trailing zeros in a 32 bit int value.
public static Ntz ( int val ) : int
val int
return int
Ejemplo n.º 1
0
        /// <summary>Returns the index of the first set bit starting at the index specified.
        /// -1 is returned if there are no more set bits.
        /// </summary>
        public virtual int NextSetBit(int index)
        {
            int i = index >> 6;

            if (i >= wlen)
            {
                return(-1);
            }
            int  subIndex = index & 0x3f;            // index within the word
            long word     = bits[i] >> subIndex;     // skip all the bits to the right of index

            if (word != 0)
            {
                return((i << 6) + subIndex + BitUtil.Ntz(word));
            }

            while (++i < wlen)
            {
                word = bits[i];
                if (word != 0)
                {
                    return((i << 6) + BitUtil.Ntz(word));
                }
            }

            return(-1);
        }
Ejemplo n.º 2
0
        /// <summary>Returns the index of the first set bit starting at the index specified.
        /// -1 is returned if there are no more set bits.
        /// </summary>
        public virtual long NextSetBit(long index)
        {
            int i = (int)(index >> 6);

            if (i >= wlen)
            {
                return(-1);
            }
            int  subIndex = (int)index & 0x3f;                  // index within the word
            long word     = (long)((ulong)bits[i] >> subIndex); // skip all the bits to the right of index

            if (word != 0)
            {
                return((((long)i) << 6) + (subIndex + BitUtil.Ntz(word)));
            }

            while (++i < wlen)
            {
                word = bits[i];
                if (word != 0)
                {
                    return((((long)i) << 6) + BitUtil.Ntz(word));
                }
            }

            return(-1);
        }
Ejemplo n.º 3
0
        public virtual void TestBitUtils()
        {
            rand = NewRandom();
            long num = 100000;
            Assert.AreEqual(5, BitUtil.Ntz(num));
            Assert.AreEqual(5, BitUtil.Ntz2(num));
            Assert.AreEqual(5, BitUtil.Ntz3(num));

            num = 10;
            Assert.AreEqual(1, BitUtil.Ntz(num));
            Assert.AreEqual(1, BitUtil.Ntz2(num));
            Assert.AreEqual(1, BitUtil.Ntz3(num));

            for (int i = 0; i < 64; i++)
            {
                num = 1L << i;
                Assert.AreEqual(i, BitUtil.Ntz(num));
                Assert.AreEqual(i, BitUtil.Ntz2(num));
                Assert.AreEqual(i, BitUtil.Ntz3(num));
            }
        }