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?

論理和( OR )の基本 |

Posted at

今回はPaizaの「OR演算」問題で、条件分岐を封印して論理和をスマートに書く方法を学んだ!


🧩 問題概要

入力で 0 または 1 の整数 A, B が与えられます。
「A OR B」の結果を出力せよ、という内容。

例)
入力: 0 1 → 出力: 1
入力: 1 1 → 出力: 1

「OR(論理和)」は、どっちかが 1 なら 1 を返すルール。


https://paiza.jp/works/mondai/logical_operation/logical_operation__basic_step2


🙅‍♂️ NGコード

if (A === 1 || B === 1) {
  console.log(1);
} else {
  console.log(0);
}


✅ OKコード

const [A, B] = input.split(' ');
console.log(A | B);

ビットOR演算子 | を使えば、条件分岐なしで「論理和」がそのまま表現できる。



0 | 0  0

0 | 1  1

1 | 0  1

1 | 1  1


💡 ポイントまとめ

  • | はビット演算子、論理和に使える
  • ||(論理演算子)と混同しないこと!
  • if文を使わずスッキリ処理 → 可読性UP
  • 入力が 0/1 に限定されてる場合は積極的に使いたい



僕の失敗談と解決話!

0
0
1

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?