Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace MultiplyIntegersBitwise
- {
- class MultiplyTwoIntegersBitwiseOpeartion
- {
- static void Main()
- {
- // Multiply integers using bitwise operators.
- // http://en.wikipedia.org/wiki/Ancient_Egyptian_multiplication
- Console.WriteLine("Please enter first number n1 = ");
- int n1 = Int32.Parse(Console.ReadLine());
- Console.WriteLine("Please enter second number n2 = ");
- int n2 = Int32.Parse(Console.ReadLine());
- int product=0;
- if (n1 % 2 != 0)
- {
- product = n2;
- }
- do
- {
- n1 = (n1 >> 1);
- n2 = (n2 << 1);
- if (n1 % 2 != 0)
- {
- product = n2 + product;
- }
- }
- while (n1 >= 1);
- Console.WriteLine(product);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement