开发者

20 bit integer math

How do I add/subtract/multiply开发者_如何学Python and divide integers using only 20 bits instead of 32 bits in C#?

Will these operations be faster than 32bit precision?

For example this .NET library is featuring 20 and 30 bit arithmetic with different speeds: http://complex-a5.ru/polyboolean/index.html

Thanks.


There are arithmetic units in processors so that it is really fast to do operations with 32bit numbers. It's faster than any code you can write because it is "wired" in processor.

Operations with 20bit number can be simulated with modulo arithmetic (i.e. mod 2^20).


How do I add/subtract/multiply and divide integers using only 20 bits instead of 32 bits in C#?

Use bitmasking to zero out the top 12 bits of 32-bit ints:

int twentyBitSum = (a + b) & 0xFFFFF;

Will these operations be faster than 32bit procision?

No. Doing arithmetic with a size your hardware doesn't natively support is extra work.


You can't, without writing your own code which will either do bit twiddling manually - or use 32 bit operations and then apply masking to limit the available range.

They certainly wouldn't be faster than 32-bit operations, as that's what the processor natively supports.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜