Skip to content

Instantly share code, notes, and snippets.

@bxt
Created April 30, 2011 20:33
Show Gist options
  • Select an option

  • Save bxt/949970 to your computer and use it in GitHub Desktop.

Select an option

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
/*
* 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