Created
April 30, 2011 20:33
-
-
Save bxt/949970 to your computer and use it in GitHub Desktop.
Boolean operators ! & | xor rewritten as arithmetic ones (+ * - on numeric input 0/1) in JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Just for fun :) AND, OR and XOR with only "+ * - 2", plus "( ) 1" for NOT | |
| */ | |
| function not(a) { | |
| return (a-1)*(a-1); | |
| return a*(a-1)+(a+1)-2*a; | |
| return (a-2)*a+1; | |
| return and(a-1,a-1); | |
| } | |
| function and(a,b) { | |
| return a*b; | |
| } | |
| function or(a,b) { | |
| return a+b-a*b; | |
| return and(a,b)+xor(a,b); | |
| } | |
| function xor(a,b) { | |
| return a+b-2*a*b; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment