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?

XNOR 演算の基本 !(A ^ B)

Posted at

Paizaの論理演算シリーズ、今回は「XNOR」演算!


🎯 問題とステップ:

0 または 1 の整数 A, B が与えられる。A XNOR B の結果を出力せよ。

要は、AとBが同じなら1、違ったら0を出力する。



✅コード例

const [A, B] = require('fs').readFileSync('/dev/stdin', 'utf8').trim().split(' ').map(Number);

// 方法1:AとBが等しいかで判断
console.log(A === B ? 1 : 0);
//方法2:ビット演算
const xnor = Number(!(A ^ B)); // XORの否定がXNOR!
console.log(xnor);

どっちも「AとBが同じなら1」を表現してるだけ。
でも 個人的には === のほうが分かりやすくて好き。


🔍 まとめ・補足ポイント

  • !(A ^ B) だけだと true/false型になる → Number() で数値に変換!
  • ^ はビットXOR → 違うときだけ1になる
  • XNORはその逆:同じときだけ1!(A ^ B) で表現可能



🐻僕の失敗談と解決話!🐈

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?