Example #1
0
        public void Test_LowestOneBit_Int64()
        {
            assertEquals(0x10L, BitOperation.LowestOneBit(0xF0L));

            assertEquals(0x10L, BitOperation.LowestOneBit(0x90L));
            assertEquals(0x10L, BitOperation.LowestOneBit(0xD0L));

            assertEquals(0x10L, BitOperation.LowestOneBit(0x123490L));
            assertEquals(0x10L, BitOperation.LowestOneBit(0x1234D0L));

            assertEquals(0x100000L, BitOperation.LowestOneBit(0x900000L));
            assertEquals(0x100000L, BitOperation.LowestOneBit(0xD00000L));

            assertEquals(0x40L, BitOperation.LowestOneBit(0x40L));
            assertEquals(0x40L, BitOperation.LowestOneBit(0xC0L));

            assertEquals(0x4000L, BitOperation.LowestOneBit(0x4000L));
            assertEquals(0x4000L, BitOperation.LowestOneBit(0xC000L));

            assertEquals(0x4000L, BitOperation.LowestOneBit(0x99994000L));
            assertEquals(0x4000L, BitOperation.LowestOneBit(0x9999C000L));

            assertEquals(0L, BitOperation.LowestOneBit(0L));
            assertEquals(1L, BitOperation.LowestOneBit(1L));
            assertEquals(1L, BitOperation.LowestOneBit(-1L));
        }
Example #2
0
        public void Test_BitCount_Int64()
        {
            assertEquals(0, BitOperation.PopCount(0x0L));
            assertEquals(1, BitOperation.PopCount(0x1L));
            assertEquals(1, BitOperation.PopCount(0x2L));
            assertEquals(2, BitOperation.PopCount(0x3L));
            assertEquals(1, BitOperation.PopCount(0x4L));
            assertEquals(2, BitOperation.PopCount(0x5L));
            assertEquals(2, BitOperation.PopCount(0x6L));
            assertEquals(3, BitOperation.PopCount(0x7L));
            assertEquals(1, BitOperation.PopCount(0x8L));
            assertEquals(2, BitOperation.PopCount(0x9L));
            assertEquals(2, BitOperation.PopCount(0xAL));
            assertEquals(3, BitOperation.PopCount(0xBL));
            assertEquals(2, BitOperation.PopCount(0xCL));
            assertEquals(3, BitOperation.PopCount(0xDL));
            assertEquals(3, BitOperation.PopCount(0xEL));
            assertEquals(4, BitOperation.PopCount(0xFL));

            assertEquals(8, BitOperation.PopCount(0xFFL));
            assertEquals(12, BitOperation.PopCount(0xFFFL));
            assertEquals(16, BitOperation.PopCount(0xFFFFL));
            assertEquals(20, BitOperation.PopCount(0xFFFFFL));
            assertEquals(24, BitOperation.PopCount(0xFFFFFFL));
            assertEquals(28, BitOperation.PopCount(0xFFFFFFFL));
            assertEquals(64, BitOperation.PopCount(unchecked ((long)0xFFFFFFFFFFFFFFFFL)));
        }
Example #3
0
        public void Test_LowestOneBit_Int32()
        {
            assertEquals(0x10, BitOperation.LowestOneBit(0xF0));

            assertEquals(0x10, BitOperation.LowestOneBit(0x90));
            assertEquals(0x10, BitOperation.LowestOneBit(0xD0));

            assertEquals(0x10, BitOperation.LowestOneBit(0x123490));
            assertEquals(0x10, BitOperation.LowestOneBit(0x1234D0));

            assertEquals(0x100000, BitOperation.LowestOneBit(0x900000));
            assertEquals(0x100000, BitOperation.LowestOneBit(0xD00000));

            assertEquals(0x40, BitOperation.LowestOneBit(0x40));
            assertEquals(0x40, BitOperation.LowestOneBit(0xC0));

            assertEquals(0x4000, BitOperation.LowestOneBit(0x4000));
            assertEquals(0x4000, BitOperation.LowestOneBit(0xC000));

            assertEquals(0x4000, BitOperation.LowestOneBit(unchecked ((int)0x99994000)));
            assertEquals(0x4000, BitOperation.LowestOneBit(unchecked ((int)0x9999C000)));

            assertEquals(0, BitOperation.LowestOneBit(0));
            assertEquals(1, BitOperation.LowestOneBit(1));
            assertEquals(1, BitOperation.LowestOneBit(-1));
        }
Example #4
0
        public void Test_BitCount_Int32()
        {
            assertEquals(0, BitOperation.PopCount(0x0));
            assertEquals(1, BitOperation.PopCount(0x1));
            assertEquals(1, BitOperation.PopCount(0x2));
            assertEquals(2, BitOperation.PopCount(0x3));
            assertEquals(1, BitOperation.PopCount(0x4));
            assertEquals(2, BitOperation.PopCount(0x5));
            assertEquals(2, BitOperation.PopCount(0x6));
            assertEquals(3, BitOperation.PopCount(0x7));
            assertEquals(1, BitOperation.PopCount(0x8));
            assertEquals(2, BitOperation.PopCount(0x9));
            assertEquals(2, BitOperation.PopCount(0xA));
            assertEquals(3, BitOperation.PopCount(0xB));
            assertEquals(2, BitOperation.PopCount(0xC));
            assertEquals(3, BitOperation.PopCount(0xD));
            assertEquals(3, BitOperation.PopCount(0xE));
            assertEquals(4, BitOperation.PopCount(0xF));

            assertEquals(8, BitOperation.PopCount(0xFF));
            assertEquals(12, BitOperation.PopCount(0xFFF));
            assertEquals(16, BitOperation.PopCount(0xFFFF));
            assertEquals(20, BitOperation.PopCount(0xFFFFF));
            assertEquals(24, BitOperation.PopCount(0xFFFFFF));
            assertEquals(28, BitOperation.PopCount(0xFFFFFFF));
            assertEquals(32, BitOperation.PopCount(unchecked ((int)0xFFFFFFFF)));
        }
Example #5
0
 public void Test_ReverseBytes_Int64()
 {
     unchecked
     {
         assertEquals((long)0xAABBCCDD00112233L, BitOperation.ReverseBytes(0x33221100DDCCBBAAL));
         assertEquals(0x1122334455667788L, BitOperation.ReverseBytes((long)0x8877665544332211L));
     }
     assertEquals(0x0011223344556677L, BitOperation.ReverseBytes(0x7766554433221100L));
     assertEquals(0x2000000000000002L, BitOperation.ReverseBytes(0x0200000000000020L));
 }
Example #6
0
 public void Test_ReverseBytes_Int32()
 {
     unchecked
     {
         assertEquals((int)0xAABBCCDD, BitOperation.ReverseBytes((int)0xDDCCBBAA));
     }
     assertEquals(0x11223344, BitOperation.ReverseBytes(0x44332211));
     assertEquals(0x00112233, BitOperation.ReverseBytes(0x33221100));
     assertEquals(0x20000002, BitOperation.ReverseBytes(0x02000020));
 }
Example #7
0
 public void Test_RotateRight_Int64()
 {
     assertEquals(0xFL, BitOperation.RotateRight(0xF0L, 4));
     assertEquals(0xFL, BitOperation.RotateRight(0xF00L, 8));
     assertEquals(0xFL, BitOperation.RotateRight(0xF000L, 12));
     assertEquals(0xFL, BitOperation.RotateRight(0xF0000L, 16));
     assertEquals(0xFL, BitOperation.RotateRight(0xF00000L, 20));
     assertEquals(0xFL, BitOperation.RotateRight(0xF000000L, 24));
     assertEquals(0xFL, BitOperation.RotateRight(0xF0000000L, 28));
     unchecked
     {
         assertEquals((long)0xF000000000000000L, BitOperation.RotateRight((long)0xF000000000000000L, 64));
         assertEquals((long)0xF000000000000000L, BitOperation.RotateRight((long)0xF000000000000000L, 0));
     }
 }
Example #8
0
 public void Test_RotateRight_Int32()
 {
     assertEquals(0xF, BitOperation.RotateRight(0xF0, 4));
     assertEquals(0xF, BitOperation.RotateRight(0xF00, 8));
     assertEquals(0xF, BitOperation.RotateRight(0xF000, 12));
     assertEquals(0xF, BitOperation.RotateRight(0xF0000, 16));
     assertEquals(0xF, BitOperation.RotateRight(0xF00000, 20));
     assertEquals(0xF, BitOperation.RotateRight(0xF000000, 24));
     unchecked
     {
         assertEquals(0xF, BitOperation.RotateRight((int)0xF0000000, 28));
         assertEquals((int)0xF0000000, BitOperation.RotateRight((int)0xF0000000, 32));
         assertEquals((int)0xF0000000, BitOperation.RotateRight((int)0xF0000000, 0));
     }
 }
Example #9
0
        public void Test_HighestOneBit_Int64()
        {
            assertEquals(0x08L, BitOperation.HighestOneBit(0x0AL));
            assertEquals(0x08L, BitOperation.HighestOneBit(0x0BL));
            assertEquals(0x08L, BitOperation.HighestOneBit(0x0CL));
            assertEquals(0x08L, BitOperation.HighestOneBit(0x0FL));
            assertEquals(0x80L, BitOperation.HighestOneBit(0xFFL));

            assertEquals(0x080000L, BitOperation.HighestOneBit(0x0F1234L));
            assertEquals(0x800000L, BitOperation.HighestOneBit(0xFF9977L));

            assertEquals(unchecked ((long)0x8000000000000000L), BitOperation.HighestOneBit(unchecked ((long)0xFFFFFFFFFFFFFFFFL)));

            assertEquals(0L, BitOperation.HighestOneBit(0L));
            assertEquals(1L, BitOperation.HighestOneBit(1L));
            assertEquals(unchecked ((long)0x8000000000000000L), BitOperation.HighestOneBit(-1L));
        }
Example #10
0
        public void Test_HighestOneBit_Int32()
        {
            assertEquals(0x08, BitOperation.HighestOneBit(0x0A));
            assertEquals(0x08, BitOperation.HighestOneBit(0x0B));
            assertEquals(0x08, BitOperation.HighestOneBit(0x0C));
            assertEquals(0x08, BitOperation.HighestOneBit(0x0F));
            assertEquals(0x80, BitOperation.HighestOneBit(0xFF));

            assertEquals(0x080000, BitOperation.HighestOneBit(0x0F1234));
            assertEquals(0x800000, BitOperation.HighestOneBit(0xFF9977));

            assertEquals(unchecked ((int)0x80000000), BitOperation.HighestOneBit(unchecked ((int)0xFFFFFFFF)));

            assertEquals(0, BitOperation.HighestOneBit(0));
            assertEquals(1, BitOperation.HighestOneBit(1));
            assertEquals(unchecked ((int)0x80000000), BitOperation.HighestOneBit(-1));
        }
Example #11
0
        public void Test_NumberOfLeadingZeros_Int64()
        {
            assertEquals(64L, BitOperation.LeadingZeroCount(0x0L));
            assertEquals(63L, BitOperation.LeadingZeroCount(0x1L));
            assertEquals(62L, BitOperation.LeadingZeroCount(0x2L));
            assertEquals(62L, BitOperation.LeadingZeroCount(0x3L));
            assertEquals(61L, BitOperation.LeadingZeroCount(0x4L));
            assertEquals(61L, BitOperation.LeadingZeroCount(0x5L));
            assertEquals(61L, BitOperation.LeadingZeroCount(0x6L));
            assertEquals(61L, BitOperation.LeadingZeroCount(0x7L));
            assertEquals(60L, BitOperation.LeadingZeroCount(0x8L));
            assertEquals(60L, BitOperation.LeadingZeroCount(0x9L));
            assertEquals(60L, BitOperation.LeadingZeroCount(0xAL));
            assertEquals(60L, BitOperation.LeadingZeroCount(0xBL));
            assertEquals(60L, BitOperation.LeadingZeroCount(0xCL));
            assertEquals(60L, BitOperation.LeadingZeroCount(0xDL));
            assertEquals(60L, BitOperation.LeadingZeroCount(0xEL));
            assertEquals(60L, BitOperation.LeadingZeroCount(0xFL));
            assertEquals(59L, BitOperation.LeadingZeroCount(0x10L));
            assertEquals(56L, BitOperation.LeadingZeroCount(0x80L));
            assertEquals(56L, BitOperation.LeadingZeroCount(0xF0L));
            assertEquals(55L, BitOperation.LeadingZeroCount(0x100L));
            assertEquals(52L, BitOperation.LeadingZeroCount(0x800L));
            assertEquals(52L, BitOperation.LeadingZeroCount(0xF00L));
            assertEquals(51L, BitOperation.LeadingZeroCount(0x1000L));
            assertEquals(48L, BitOperation.LeadingZeroCount(0x8000L));
            assertEquals(48L, BitOperation.LeadingZeroCount(0xF000L));
            assertEquals(47L, BitOperation.LeadingZeroCount(0x10000L));
            assertEquals(44L, BitOperation.LeadingZeroCount(0x80000L));
            assertEquals(44L, BitOperation.LeadingZeroCount(0xF0000L));
            assertEquals(43L, BitOperation.LeadingZeroCount(0x100000L));
            assertEquals(40L, BitOperation.LeadingZeroCount(0x800000L));
            assertEquals(40L, BitOperation.LeadingZeroCount(0xF00000L));
            assertEquals(39L, BitOperation.LeadingZeroCount(0x1000000L));
            assertEquals(36L, BitOperation.LeadingZeroCount(0x8000000L));
            assertEquals(36L, BitOperation.LeadingZeroCount(0xF000000L));
            assertEquals(35L, BitOperation.LeadingZeroCount(0x10000000L));
            assertEquals(32L, BitOperation.LeadingZeroCount(0x80000000L)); // J2N: Changed test to match observed behavior in JDK (32L rather than 0L)
            assertEquals(32L, BitOperation.LeadingZeroCount(0xF0000000L)); // J2N: Changed test to match observed behavior in JDK (32L rather than 0L)

            assertEquals(1L, BitOperation.LeadingZeroCount(long.MaxValue));
            assertEquals(0L, BitOperation.LeadingZeroCount(long.MinValue));
        }
Example #12
0
        public void Test_NumberOfLeadingZeros_Int32()
        {
            assertEquals(32, BitOperation.LeadingZeroCount(0x0));
            assertEquals(31, BitOperation.LeadingZeroCount(0x1));
            assertEquals(30, BitOperation.LeadingZeroCount(0x2));
            assertEquals(30, BitOperation.LeadingZeroCount(0x3));
            assertEquals(29, BitOperation.LeadingZeroCount(0x4));
            assertEquals(29, BitOperation.LeadingZeroCount(0x5));
            assertEquals(29, BitOperation.LeadingZeroCount(0x6));
            assertEquals(29, BitOperation.LeadingZeroCount(0x7));
            assertEquals(28, BitOperation.LeadingZeroCount(0x8));
            assertEquals(28, BitOperation.LeadingZeroCount(0x9));
            assertEquals(28, BitOperation.LeadingZeroCount(0xA));
            assertEquals(28, BitOperation.LeadingZeroCount(0xB));
            assertEquals(28, BitOperation.LeadingZeroCount(0xC));
            assertEquals(28, BitOperation.LeadingZeroCount(0xD));
            assertEquals(28, BitOperation.LeadingZeroCount(0xE));
            assertEquals(28, BitOperation.LeadingZeroCount(0xF));
            assertEquals(27, BitOperation.LeadingZeroCount(0x10));
            assertEquals(24, BitOperation.LeadingZeroCount(0x80));
            assertEquals(24, BitOperation.LeadingZeroCount(0xF0));
            assertEquals(23, BitOperation.LeadingZeroCount(0x100));
            assertEquals(20, BitOperation.LeadingZeroCount(0x800));
            assertEquals(20, BitOperation.LeadingZeroCount(0xF00));
            assertEquals(19, BitOperation.LeadingZeroCount(0x1000));
            assertEquals(16, BitOperation.LeadingZeroCount(0x8000));
            assertEquals(16, BitOperation.LeadingZeroCount(0xF000));
            assertEquals(15, BitOperation.LeadingZeroCount(0x10000));
            assertEquals(12, BitOperation.LeadingZeroCount(0x80000));
            assertEquals(12, BitOperation.LeadingZeroCount(0xF0000));
            assertEquals(11, BitOperation.LeadingZeroCount(0x100000));
            assertEquals(8, BitOperation.LeadingZeroCount(0x800000));
            assertEquals(8, BitOperation.LeadingZeroCount(0xF00000));
            assertEquals(7, BitOperation.LeadingZeroCount(0x1000000));
            assertEquals(4, BitOperation.LeadingZeroCount(0x8000000));
            assertEquals(4, BitOperation.LeadingZeroCount(0xF000000));
            assertEquals(3, BitOperation.LeadingZeroCount(0x10000000));
            assertEquals(0, BitOperation.LeadingZeroCount(unchecked ((int)0x80000000)));
            assertEquals(0, BitOperation.LeadingZeroCount(unchecked ((int)0xF0000000)));

            assertEquals(1, BitOperation.LeadingZeroCount(int.MaxValue));
            assertEquals(0, BitOperation.LeadingZeroCount(int.MinValue));
        }
Example #13
0
        public void Test_NumberOfTrailingZeros_Int64()
        {
            assertEquals(64L, BitOperation.TrailingZeroCount(0x0L));
            assertEquals(63L, BitOperation.TrailingZeroCount(long.MinValue));
            assertEquals(0L, BitOperation.TrailingZeroCount(long.MaxValue));

            assertEquals(0L, BitOperation.TrailingZeroCount(0x1L));
            assertEquals(3L, BitOperation.TrailingZeroCount(0x8L));
            assertEquals(0L, BitOperation.TrailingZeroCount(0xFL));

            assertEquals(4L, BitOperation.TrailingZeroCount(0x10L));
            assertEquals(7L, BitOperation.TrailingZeroCount(0x80L));
            assertEquals(4L, BitOperation.TrailingZeroCount(0xF0L));

            assertEquals(8L, BitOperation.TrailingZeroCount(0x100L));
            assertEquals(11L, BitOperation.TrailingZeroCount(0x800L));
            assertEquals(8L, BitOperation.TrailingZeroCount(0xF00L));

            assertEquals(12L, BitOperation.TrailingZeroCount(0x1000L));
            assertEquals(15L, BitOperation.TrailingZeroCount(0x8000L));
            assertEquals(12L, BitOperation.TrailingZeroCount(0xF000L));

            assertEquals(16L, BitOperation.TrailingZeroCount(0x10000L));
            assertEquals(19L, BitOperation.TrailingZeroCount(0x80000L));
            assertEquals(16L, BitOperation.TrailingZeroCount(0xF0000L));

            assertEquals(20L, BitOperation.TrailingZeroCount(0x100000L));
            assertEquals(23L, BitOperation.TrailingZeroCount(0x800000L));
            assertEquals(20L, BitOperation.TrailingZeroCount(0xF00000L));

            assertEquals(24L, BitOperation.TrailingZeroCount(0x1000000L));
            assertEquals(27L, BitOperation.TrailingZeroCount(0x8000000L));
            assertEquals(24L, BitOperation.TrailingZeroCount(0xF000000L));

            assertEquals(28L, BitOperation.TrailingZeroCount(0x10000000L));
            assertEquals(31L, BitOperation.TrailingZeroCount(0x80000000L));
            assertEquals(28L, BitOperation.TrailingZeroCount(0xF0000000L));
        }
Example #14
0
        public void Test_NumberOfTrailingZeros_Int32()
        {
            assertEquals(32, BitOperation.TrailingZeroCount(0x0));
            assertEquals(31, BitOperation.TrailingZeroCount(int.MinValue));
            assertEquals(0, BitOperation.TrailingZeroCount(int.MaxValue));

            assertEquals(0, BitOperation.TrailingZeroCount(0x1));
            assertEquals(3, BitOperation.TrailingZeroCount(0x8));
            assertEquals(0, BitOperation.TrailingZeroCount(0xF));

            assertEquals(4, BitOperation.TrailingZeroCount(0x10));
            assertEquals(7, BitOperation.TrailingZeroCount(0x80));
            assertEquals(4, BitOperation.TrailingZeroCount(0xF0));

            assertEquals(8, BitOperation.TrailingZeroCount(0x100));
            assertEquals(11, BitOperation.TrailingZeroCount(0x800));
            assertEquals(8, BitOperation.TrailingZeroCount(0xF00));

            assertEquals(12, BitOperation.TrailingZeroCount(0x1000));
            assertEquals(15, BitOperation.TrailingZeroCount(0x8000));
            assertEquals(12, BitOperation.TrailingZeroCount(0xF000));

            assertEquals(16, BitOperation.TrailingZeroCount(0x10000));
            assertEquals(19, BitOperation.TrailingZeroCount(0x80000));
            assertEquals(16, BitOperation.TrailingZeroCount(0xF0000));

            assertEquals(20, BitOperation.TrailingZeroCount(0x100000));
            assertEquals(23, BitOperation.TrailingZeroCount(0x800000));
            assertEquals(20, BitOperation.TrailingZeroCount(0xF00000));

            assertEquals(24, BitOperation.TrailingZeroCount(0x1000000));
            assertEquals(27, BitOperation.TrailingZeroCount(0x8000000));
            assertEquals(24, BitOperation.TrailingZeroCount(0xF000000));

            assertEquals(28, BitOperation.TrailingZeroCount(0x10000000));
            assertEquals(31, BitOperation.TrailingZeroCount(unchecked ((int)0x80000000)));
            assertEquals(28, BitOperation.TrailingZeroCount(unchecked ((int)0xF0000000)));
        }
Example #15
0
 public void Test_Reverse_Int64()
 {
     assertEquals(0L, BitOperation.Reverse(0L));
     assertEquals(-1L, BitOperation.Reverse(-1L));
     assertEquals(unchecked ((long)0x8000000000000000L), BitOperation.Reverse(1L));
 }
Example #16
0
 public void Test_Reverse_Int32()
 {
     assertEquals(-1, BitOperation.Reverse(-1));
     assertEquals(unchecked ((int)0x80000000), BitOperation.Reverse(1));
 }