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?

論理積( AND )の基本 &

Posted at

AND演算の使い分けについて技術だけ抜き出してまとめてみた!


問題概要

0 または 1 の整数 A と B が与えられます。 A AND B の結果を出力してください。


入力例:

0 1

出力例:

0


❌ NGコード

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

今回の問題では条件分岐を使わないのが条件。
(&& はブール判定用なので、「真偽値」を返す式)


✅ OKコード

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

& は「ビットAND」。1 & 11、それ以外 → 0


💡 技術メモ

  • &:整数同士のビット演算(論理積)
  • &&:ブール値の論理演算(短絡評価)
  • if文より論理演算子で一発解決のほうがコードがスッキリ
  • x & 1 === 0 で偶数判定にも使える


📘 新しく学んだことまとめ

  • a & b でビット単位のANDが可能
  • & vs &&、用途に応じて使い分け
  • ビット演算はフラグ管理や高速な条件判定に便利



僕の失敗談と解決話!

0
0
3

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?