1
0

複数のboolean型の処理

Posted at
  • isA
  • isB
  • isC
  • isD

上記のような複数のチェックボックスの条件を組み合わせて複雑な条件分岐を組む必要があったので、個人的メモ。

// boolean変数をビットに変換
var condition = 
(isA ? 1 : 0) << 3 | (isB ? 1 : 0) << 2 | (isC ? 1 : 0) << 1 | (isD ? 1 : 0);

switch (condition) {
    case 0b1000:
        // isA && !isB && !isC && !isD
        break;
    case 0b1010:
        // isA && !isB && isC && !isD
        break;
    default:
        // その他
}

ビット型に変換することで、複数の条件が0bABCDという見た目でもわかりやすい形にかわるため、条件の整理がしやすくなった。

1
0
4

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
1
0