Skip to content

Instantly share code, notes, and snippets.

@sghall
Last active January 25, 2017 15:35
Show Gist options
  • Save sghall/b3d55273f480875e799e16dbf204fb9a to your computer and use it in GitHub Desktop.
Save sghall/b3d55273f480875e799e16dbf204fb9a to your computer and use it in GitHub Desktop.
toNegaBinary and toNegaQuaternary
https://en.wikipedia.org/wiki/Negative_base#Calculation
function toNegaBinary( number ) {
var Schroeppel2 = 0xAAAAAAAA;
return ( ( number + Schroeppel2 ) ^ Schroeppel2 ).toString(2);
}
function toNegaQuaternary( number ) {
var Schroeppel4 = 0xCCCCCCCC;
return ( ( number + Schroeppel4 ) ^ Schroeppel4 ).toString(4);
}
Also..
const negaBinaryToDeci = (arr) => {
const len = arr.length;
let sum = 0;
for (let i = 0; i < len; i++) {
sum += arr[i] * Math.pow(-2, i);
}
return sum;
}
const deciToNegaBinary = (d) => {
const result = [];
while (d != 0) {
let remainder = d % -2;
d = Math.ceil(d / -2);
result.push(Math.abs(remainder));
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment