2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ベン図を分かりやすくGIFアニメーションにしてみた

Last updated at Posted at 2019-01-01

AND回路

AND回路は以下のようになります。
AとBの入力があり重なった赤いCが出力になります。
ベン図AND.gif

OR回路

OR回路は以下のようになります。
AとBの入力がありAかつBの範囲Cが出力になります。
ベン図OR.gif

NAND回路

NAND回路は以下のようになります。
初めに出てきたANDの否定です。(NOT AND)
赤いCの出力がANDの反転なのがわかります。
AとBが両方TRUEの時にFALSEを出力する回路です。
ベン図NAND.gif

NOR回路

NOR回路は以下のようになります。
ORの否定です。(NOT OR)
ベン図NOR.gif

XOR回路

XOR回路は以下のようになります。
AとBが両方TRUEもしくは両方FALSEの時にFALSEを出力します。
ベン図XOR.gif

XOR回路の分解

XOR回路をNAND、OR、ANDを使って求めるには以下のようになります。
黄色いNANDと紫のORを重ねた時に重なる範囲(AND)がXORになります。(黄色と紫を重ねて白っぽくなっている範囲)

ベン図1_03.gif

XOR回路のJavaによる実装

上のXOR回路の分解で示した図をJavaで実装してみます。

Main.java

public class Main {

    public static void main(String[] args) {
        // XOR回路の出力
        final boolean[][] AB = {
                    { true, true },
                    { true, false },
                    { false, true },
                    { false, false }
                };

        System.out.printf("XOR回路の出力\n");
        for (int i = 0; i < AB.length; i++) {
            boolean b = xor(AB[i][0], AB[i][1]);
            System.out.println("(A :" + AB[i][0] + ") + (B :" + AB[i][1] + ") = " + b);
            }
    }
    public static boolean and(boolean a, boolean b) {
        if (a && b) {
            return true;
        }
        return false;
    }

    public static boolean or(boolean a, boolean b) {
        if (a || b) {
            return true;
        }
        return false;
    }
    public static boolean nand(boolean a, boolean b) {
        return !and(a, b);
    }
    public static boolean xor(boolean a, boolean b) {
        if (and(nand(a, b), or(a,b))) {
            return true;
        }
        return false;
    }
}

//XOR回路の出力
//(A :true) + (B :true) = false
//(A :true) + (B :false) = true
//(A :false) + (B :true) = true
//(A :false) + (B :false) = false

XOR回路のJavaによる実装 その2

上記のJavaのコードで使ってるxorメソッドの中身を下記に変えてもXOR回路が出来ます。
下記はaとnand(a, b)のandをとったaの右側が欠けた三日月のような出力と、bとnand(a, b)のandをとったbの左側が欠けた三日月のような出力をorで合体させるように出力します。

Main.java

    public static boolean xor(boolean a, boolean b) {
        if (or(and(a, nand(a, b)), and(b, nand(a, b)))) {
	    	return true;
	    }
    	return false;
    }
2
5
2

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
2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?