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?

NOR 演算の基本 !(A | B)   おまけ:| と || の違い

Posted at

0と1しか出てこないのに、なぜかめちゃくちゃ奥が深い…。
今回解いたのは、Paizaの「NOR演算」


🧩 問題概要:

入力は 0 1 の2つの数値で、出力は NOR(NOT OR)の結果。



NGコード:

const result = !(A | B);
console.log(result); // ❌ true or false が出る

これは論理値(boolean)を出してるだけ。
数値(0か1)で出さないとジャッジが通らない


✅ OKコード:

const [A, B] = require("fs").readFileSync("/dev/stdin", "utf8").trim().split(" ").map(Number);
const result = Number(!(A | B));
console.log(result); // ちゃんと 0 or 1!
  • A | B はビットOR

  • ! はNOT

  • Number()true / false1 / 0 に変換


NORも万能ゲートらしい
例:

  • NOT A = A NOR A
  • A OR B = (A NOR B) NOR (A NOR B)
  • A AND B = (A NOR A) NOR (B NOR B)

NORだけで全ての論理演算を再現できる


📌 まとめ

NORは「ORの否定」と覚えればいい!



僕の失敗談(´;ω;`)と解決策🐈🚀


おまけ : JavaScriptにおける | と || の違い、まとめてみた👇

✅ |(ビット単位の OR)

ビット演算子(Bitwise OR) ってやつ。

各ビットごとに比較して、どちらかが1なら1を返す

例:

console.log(5 | 3);  // => 7
// 5 = 0101
// 3 = 0011
// ---------
//     0111 = 7


✅ ||(論理 OR)

論理演算子(Logical OR) ってやつ。

  • 左が「truthy」ならそれを返す。
  • 左が「falsy」なら右を評価してそれを返す。
    -「条件分岐」でよく使う!

例:

console.log(true || false);      // => true
console.log(0 || 100);           // => 100
console.log("hello" || "world"); // => "hello"


👀 補足:今回の論理演算にはどっち?

Paizaの「0か1の論理演算」系は、0か1の数値として処理されるから:

|& など ビット演算子を使うのがいいかも。
(でも ||&& を使っても 0と1なら正しく動いちゃう )

📌 例:混乱しやすいやつ

console.log(0 | 1);   // => 1  ← 数値(ビット演算)
console.log(0 || 1);  // => 1  ← 真偽値判定(0は falsy)

//でも:

console.log(0 || 2);  // => 2 ← 条件っぽく動く

「演算として使う」のか、「条件として使う」のか、そこが判断の分かれ目!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?