How to implement multiplication without using the multiplication operator in .NET.

I want to implement multiplication of two integers without using the multiplication operator, in .NET

public uint MultiplyNumbers(uint x, uint y) { } 

Any idea!

+3
source share
7 answers

I guess this is homework ... otherwise there is no reasonable reason why you want to do this. So I’ll just give hints ...

  • If performance is not very important, consider x * 3 = x + x + x ... consider using a loop.

  • If performance is important, but you know that one of the numbers will be small, loop a smaller number.

  • If performance is important, and both numbers can be large, you need to think about bit-twiddling. Remember that x * 2 - x << 1 , and from there.

+25
source

This is contrary to the spirit of the assignment, but I would do it for kicks ...

Create your own class, overload the + operator to multiply.

Create your homework project; add your first project as a link. Write your code

 return new SuperInt(x) + SuperInt(y); 

Everyone else is going to change the beat or addition variations. Half of the children are going to publish the exact code returned by Google search anyway. At least in this way, you will be unique.

The purpose itself is simply an exercise in lateral thinking. Any sane person will use the * operator when working in .Net.

EDIT: If you really want to be a class clown, overload the * operator and implement it with bitwise operations and additions.

Additional answer # 1 (if you want to change your method signature ...) How about this?

 static void Main(string[] args) { Console.WriteLine(string.Format("{0} * {1} = {2}", 5, 6, MultiplyNumbers(5, 6))); Console.WriteLine(string.Format("{0} * {1} = {2}", -5, 6, MultiplyNumbers(-5, 6))); Console.WriteLine(string.Format("{0} * {1} = {2}", -5, -6, MultiplyNumbers(-5, -6))); Console.WriteLine(string.Format("{0} * {1} = {2}", 5, 1, MultiplyNumbers(5, 1))); Console.Read(); } static double MultiplyNumbers(double x, double y) { return x / (1 / y); } 

Outputs:

 5 * 6 = 30 -5 * 6 = -30 -5 * -6 = 30 5 * 1 = 5 

One straight line of code.

But still, if you take this approach, be prepared to argue a bit. It multiplies integers; by implicitly converting them to paired numbers in a call. Your question did not say that you can only use integers, just so that he would have to multiply two integers without using '*'.

EDIT: Since you say you cannot change the signature of MultiplyNumbers, you can execute it without doing this:

 static uint MultiplyNumbers(uint x, uint y) { return MultiplyDouble(x, y); } static uint MultiplyDouble(double x, double y) { return Convert.ToUInt32(x / (1 / y)); } 

Additional answer # 2 This is my favorite approach.

Take the values, send them to Google, analyze the result.

 static uint MultiplyNumbers(uint x, uint y) { System.Net.WebClient myClient = new System.Net.WebClient(); string sData = myClient.DownloadString(string.Format("http://www.google.com/search?q={0}*{1}&btnG=Search",x,y)); string ans = x.ToString() + " * " + y.ToString() + " = "; int iBegin = sData.IndexOf(ans,50) + ans.Length ; int iEnd = sData.IndexOf('<',iBegin); return Convert.ToUInt32(sData.Substring(iBegin, iEnd - iBegin).Trim()); } 
+12
source

Look, ma, no * operator!

 using System; using System.Reflection.Emit; static class Program { delegate uint UintOpDelegate(uint a, uint b); static void Main() { var method = new DynamicMethod("Multiply", typeof(uint), new Type[] { typeof(uint), typeof(uint) }); var gen = method.GetILGenerator(); gen.Emit(OpCodes.Ldarg_0); gen.Emit(OpCodes.Ldarg_1); gen.Emit(OpCodes.Mul); gen.Emit(OpCodes.Ret); var del = (UintOpDelegate)method.CreateDelegate(typeof(UintOpDelegate)); var product = del(2, 3); //product is now 6! } } 

Strike>

Even better:

 using System; using System.Runtime.InteropServices; delegate uint BinaryOp(uint a, uint b); static class Program { [DllImport("kernel32.dll", SetLastError = true)] static extern bool VirtualProtect( IntPtr address, IntPtr size, uint protect, out uint oldProtect); static void Main() { var bytes = IntPtr.Size == sizeof(int) //32-bit? It slower BTW ? Convert.FromBase64String("i0QkBA+vRCQIww==") : Convert.FromBase64String("D6/Ki8HD"); var handle = GCHandle.Alloc(bytes, GCHandleType.Pinned); try { uint old; VirtualProtect(handle.AddrOfPinnedObject(), (IntPtr)bytes.Length, 0x40, out old); var action = (BinaryOp)Marshal.GetDelegateForFunctionPointer( handle.AddrOfPinnedObject(), typeof(BinaryOp)); var temp = action(3, 2); //6! } finally { handle.Free(); } } } 
+12
source

You can simply loop for x times by adding y to the current total at each iteration.

+4
source

Re-adding will work. Add "x" to the current total "y".

 var total = 0; for(int i = 0; i < y; i++) { total += x; } 
+4
source

You can use bitwise operators for multiplication.

 x<<1 

equal to x * 2, etc.

You still have to add something.

  result=0; while(b != 0) { if (b&01) { result=result+a; } a<<=1; b>>=1; } 

From: Multiplying two integers using bitwise operators

+4
source
 public uint MultiplyNumbers(uint x, uint y) { if (x == 0 || y == 0) return 0; uint answer = x; for (uint i = 1; i < y; ++i) answer += x; return answer; } 
+1
source

Source: https://habr.com/ru/post/887485/


All Articles