Canonical Representation of XOR

Actionscript:
  1. // xor
  2. trace(0 ^ 0);
  3. trace(0 ^ 1);
  4. trace(1 ^ 0);
  5. trace(1 ^ 1);
  6.  
  7. trace("canonical representation of xor");
  8. trace(xor(0, 0));
  9. trace(xor(0, 1));
  10. trace(xor(1, 0));
  11. trace(xor(1, 1));
  12.  
  13. function xor(a:int, b:int):int{
  14.     //1-a   is the same as   int(!a)
  15.     return 1-a & b | a & 1-b;
  16. }
  17.  
  18. /*
  19. outputs:
  20. 0
  21. 1
  22. 1
  23. 0
  24. canonical representation of xor
  25. 0
  26. 1
  27. 1
  28. 0
  29. */

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

This entry was posted in Operators, one-liners and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Comments

  1. Ted
    Posted January 30, 2009 at 2:31 am | Permalink

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

  2. Posted January 30, 2009 at 10:02 am | Permalink

    Thanks Ted… glad you enjoy it :)

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*