C++ fast division/mod by 10^x

后端 未结 10 647
我寻月下人不归
我寻月下人不归 2020-12-03 05:13

In my program I use a lot of integer division by 10^x and integer mod function of power 10.

For example:

unsigned __int64 a = 12345;
a = a / 100;
...         


        
10条回答
  •  心在旅途
    2020-12-03 05:21

    In fact you don't need to do anything. The compiler is smart enough to optimize multiplications/divisions with constants. You can find many examples here

    • Why does GCC use multiplication by a strange number in implementing integer division?
    • Divide by 10 using bit shifts?
    • Fast Division on GCC/ARM

    You can even do a fast divide by 5 then shift right by 1

提交回复
热议问题