0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

10の割り算 割られる数が1000ぐらいまで正しい(おまけ)

Last updated at Posted at 2023-11-26

前回の記事(コメントも見てください)

目的
たいへん、ありがたい事に
32ビットintの10の割り算で
最適なアルゴリズムがあると指摘されたので
試してみました。 結果は、(真)です。(正しいです。)

3435973837は、1 0000 0000Hを10で割って
左に三回シフトした数だそうです。

オンラインコンパイラ



#include <iostream>
using namespace std;
int main(void){
    // Your code here!
    
    long al;
    long long ax;
    int bl=0;
    
    al=-29;
    if(al < 0) {bl=1;al=0-al;}
    ax = (long long)(3435973837) * (long long)(al);
    al = ax >> 35;
    if(bl==1) al=0-al;
    printf("%d\n",al);

}



結果


2

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?