本文介绍了如何实现乘法,而无需使用乘法运算符在.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现两个整数乘法,而无需使用乘法运算符,在.NET

I want to implement multiplication of two integer numbers without using multiplication operator, in .NET

public uint MultiplyNumbers(uint x, uint y)
{

}

任何想法!

推荐答案

我假设这是功课...否则有你想要做任何理智的理由。所以我只给提示...

I'm assuming this is homework... otherwise there's no sane reason you'd want to do it. Therefore I'll just give hints...

  • 如果性能不是非常重要,考虑到 X * 3 = X + X + X ...考虑使用一个循环。

  • If performance isn't terribly important, consider that x * 3 = x + x + x... think about using a loop.

如果性能很重要,但你知道的的一个数的将是小,环路上的较小的数字。

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

如果性能是重要的,这两个数字可能会很大,你需要考虑位变换。请记住, X * 2 是 X<< 1 ,并从那里。

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

这篇关于如何实现乘法,而无需使用乘法运算符在.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 10:42