예제 #1
0
 private static NumberSwapper BinarySwap(NumberSwapper numbers)
 {
     numbers.num1 = numbers.num1 ^ numbers.num2;//xor
     numbers.num2 = numbers.num2 ^ numbers.num1;
     numbers.num1 = numbers.num1 ^ numbers.num2;
     return(numbers);
 }
예제 #2
0
        private static NumberSwapper ClassicSwap(NumberSwapper numbers)
        {
            numbers.num1 = numbers.num1 - numbers.num2;
            numbers.num2 = numbers.num1 + numbers.num2;
            numbers.num1 = numbers.num2 - numbers.num1;

            return(numbers);
        }
예제 #3
0
        public static void SwapNumbers()
        {
            Console.WriteLine("Please enter Number 1");
            int n1 = int.Parse(Console.ReadLine());

            Console.WriteLine("Please enter Number2");
            int           n2      = int.Parse(Console.ReadLine());
            NumberSwapper numbers = new NumberSwapper(n1, n2);

            numbers = NumberSwapper.ClassicSwap(numbers);
            DisplayNumbers(numbers);
            Console.WriteLine("performing BinarySwap on the swapped numbers");
            numbers = NumberSwapper.BinarySwap(numbers);
            DisplayNumbers(numbers);
            Console.Read();
        }
예제 #4
0
 private static void DisplayNumbers(NumberSwapper numbers)
 {
     Console.WriteLine(numbers.num1 + "\r\n is the first Number after swap");
     Console.WriteLine(numbers.num2 + "\r\n is the second Number after swap");
 }