Esempio n. 1
0
        public void IsEven_ShouldReturnValidResult_ForInt64()
        {
            // Arrange.
            Int64 negativeTwo = -2;
            Int64 negativeOne = -1;
            Int64 zero        = 0;
            Int64 one         = 1;
            Int64 two         = 2;
            Int64 three       = 3;
            Int64 four        = 4;

            // Act.
            var resultOne   = negativeTwo.IsEven();
            var resultTwo   = negativeOne.IsEven();
            var resultThree = zero.IsEven();
            var resultFour  = one.IsEven();
            var resultFive  = two.IsEven();
            var resultSix   = three.IsEven();
            var resultSeven = four.IsEven();

            // Assert.
            resultOne.Should().BeTrue();
            resultTwo.Should().BeFalse();
            resultThree.Should().BeTrue();
            resultFour.Should().BeFalse();
            resultFive.Should().BeTrue();
            resultSix.Should().BeFalse();
            resultSeven.Should().BeTrue();
        }
        /// <summary>
        ///		Validates that the <paramref name="valueArgument"/> is odd.
        /// </summary>
        /// <param name="valueArgument">The <see cref="T:System.Int64"/> argument value to validate is odd.</param>
        /// <param name="nameArgument">The argument name of the <see cref="T:System.Int64"/> value.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="nameArgument"/> is <see langword="null"/>.</exception>
        /// <exception cref="ArgumentOutOfRangeException">The <paramref name="valueArgument"/> is even.</exception>
        public static void RequireArgumentIsOdd(Int64 valueArgument,
                                                [NotNull] String nameArgument)
        {
            nameArgument.RequireArgumentHasNonWhiteSpaceValue("nameArgument");

            if (valueArgument.IsEven())
            {
                throw new ArgumentOutOfRangeException(nameArgument, valueArgument, String.Format(CultureInfo.InvariantCulture, "{0}.", Resources.ValueMustBeOdd));
            }
        }
Esempio n. 3
0
        private static Int64 BinaryGreatestCommonDivisorAlgorithm(Int64 a, Int64 b)
        {
            // Solange 'a' und 'b' beide gerade Zahlen sind, teile die Zahlen durch 2
            // und merke wie oft dies möglich war in 'k'.
            int k;

            for (k = 0; (a | b).IsEven(); ++k)
            {
                a >>= 1; // a = (a / 2);
                b >>= 1; // b = (b / 2);
            }

            // Teile 'a' solange durch 2 bis die Zahl ungerade ist.
            while (a.IsEven())
            {
                a >>= 1; // a = (a / 2);
            }

            // Ab hier ist 'a' definitiv ungerade. Für 'b' muss dies allerdings noch nicht gelten!
            do
            {
                // Teile 'b' solange durch 2 bis die Zahl ungerade ist.
                while (b.IsEven())
                {
                    b >>= 1; // b = (b / 2);
                }

                // 'a' und 'b' sind hier beide ungerade. Falls 'a' >= 'b'
                // muss der Inhalt beider Variablen geswappt werden,
                // damit die notwendige Subtraktion durchgeführt werden
                // kann.
                if (a > b)
                {
                    var temp = b;
                    b = a;
                    a = temp;
                }

                b = b - a;
            } while (b != 0);

            return(a << k); // a * 2^k
        }