Canonical Representation of XOR

// xor
trace(0 ^ 0);
trace(0 ^ 1);
trace(1 ^ 0);
trace(1 ^ 1);

trace("canonical representation of xor");
trace(xor(0, 0));
trace(xor(0, 1));
trace(xor(1, 0));
trace(xor(1, 1));

function xor(a:int, b:int):int{
	//1-a   is the same as   int(!a)
	return 1-a & b | a & 1-b;
}

/*
outputs:
0
1
1
0
canonical representation of xor
0
1
1
0
*/

I learned about this from reading The Elements of Computing Systems: Building a Modern Computer from First Principles By Noam Nisan and Shimon Schocken

Check out chapter 1 from the above link for an easy to understand description of the canonical representation of a boolean function.

Just a side note… this happens to be the 100th post on actionsnippet.com

2 comments

  1. “Just a side note… this happens to be the 100th post on actionsnippet.com” – Congratulations! I really enjoy feeding from all your snippets.

Leave a Reply

Your email address will not be published. Required fields are marked *