Last active
January 25, 2017 15:35
-
-
Save sghall/b3d55273f480875e799e16dbf204fb9a to your computer and use it in GitHub Desktop.
toNegaBinary and toNegaQuaternary
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
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