Skip to content

Instantly share code, notes, and snippets.

@SethVandebrooke
Last active January 18, 2021 21:56
Show Gist options
  • Save SethVandebrooke/e6bf787e87a0788f79c1e323a8b0c726 to your computer and use it in GitHub Desktop.
Save SethVandebrooke/e6bf787e87a0788f79c1e323a8b0c726 to your computer and use it in GitHub Desktop.
Code for making decisions off of binary values
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