• home
  • about
  • 全ての投稿
  • ソフトウェア・ハードウェアの設定のまとめ
  • 分析関連のまとめ
  • ヘルスケア関連のまとめ
  • 生涯学習関連のまとめ

bit演算 select fist flag

date: 2021-04-22 excerpt: bit演算 select first flagについて

tag: bit operationbit演算


bit演算 select first flagについて

概要

  • ..xxx1010のようなbit arrayがある時、..xxx0010を取り出すアルゴリズム
  • Y & -Yを計算するだけなので簡単

具体な挙動

  • 00000000000001010 = Yがあるとき、このbitの反転は~Yである
  • 11111111111110101 = ~Y
  • -Y = ~Y + 1であるから11111111111110110 = ~Y + 1 = -Y
  • 00000000000000010 = Y & -Y

参考

  • Elegant, fast and easy-understand Python solution/LeetCode


bit operationbit演算 Share Tweet