如何实现乘法,而无需使用乘法运算符在.NET乘法、如何实现、运算符、NET

2023-09-03 02:51:04 作者:祝我與孤獨終老i

我想实现两个整数乘法,而无需使用乘法运算符,在.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.

 
精彩推荐
图片推荐