Last active
January 18, 2021 21:56
-
-
Save SethVandebrooke/e6bf787e87a0788f79c1e323a8b0c726 to your computer and use it in GitHub Desktop.
Code for making decisions off of binary values
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
function bd(binaryString) { | |
return parseInt(binaryString, 2); | |
} | |
function db(num) { | |
return num.toString(2); | |
} | |
function rl(binaryString,l) { | |
var zeros = ""; | |
for (var i = 0; i < l - binaryString.length; i++) { | |
zeros += "0"; | |
} | |
return zeros + binaryString; | |
} | |
function overlayBitMask_and(a,b,c) { | |
var r = []; | |
if (a.length == b.length && b.length == c.length) { | |
throw new Error("OverlayBitMaskThe length of all three parameters must be the same."); | |
} | |
rl(db(bd(a) & bd(b)), a.length) | |
.split('') | |
.forEach( | |
(bit,i) => bit == '1' ? r.push(c[i]) : null | |
); | |
return r; | |
} | |
function overlayBitMask_or(a,b,c) { | |
var r = []; | |
if (a.length == b.length && b.length == c.length) { | |
throw new Error("OverlayBitMaskThe length of all three parameters must be the same."); | |
} | |
rl(db(bd(a) | bd(b)), a.length) | |
.split('') | |
.forEach( | |
(bit,i) => bit == '1' ? r.push(c[i]) : null | |
); | |
return r; | |
} | |
function overlayBitMask_xor(a,b,c) { | |
var r = []; | |
if (a.length == b.length && b.length == c.length) { | |
throw new Error("OverlayBitMaskThe length of all three parameters must be the same."); | |
} | |
rl(db(bd(a) ^ bd(b)), a.length) | |
.split('') | |
.forEach( | |
(bit,i) => bit == '1' ? r.push(c[i]) : null | |
); | |
return r; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment