0
0

小道具:CountTrailingZeros (CTZ)

Posted at

整数の下位ビットから連続した "0" を数えます。

def CTZ(n: int) -> int:
    return (n & -n).bit_length() - 1

"0" のときは "-1" を返します。これは

  • Intel x86 プロセッサでは BSF 命令
    • 0 のときは不定
  • C++ 言語では std::countr_zero(x)
    • 0 のときは x のビット幅

に相当します。2進数とCTZの関係は以下の通り

2進数 CTZ
???? 1 0
??? 10 1
?? 100 2
? 1000 3
... ...

参考資料

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