Last active
June 23, 2016 18:30
-
-
Save embarq/eee5c73bb66c98efd3406e91d6992378 to your computer and use it in GitHub Desktop.
`Gray` to `Additional` code conversion
This file contains 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
"use strict"; | |
let rand = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min; | |
// Перевод двоичного числа в десятичное | |
// Двоичное число приходит в ф-цию в виде строки | |
let toDecimal = (value) => | |
value.split('') // Преобразуем строку в массив символов | |
// Считаем по формуле - https://goo.gl/eJ8KcJ | |
// Доп. читай про ф-ции `map` и `reduce`: | |
// https://learn.javascript.ru/array-iteration | |
.map((char, i, arr) => | |
parseInt(char) * Math.pow(2, arr.length - (i + 1))) | |
.reduce((sum, current) => sum + current); | |
// Перевод числа из одной нотации в другую | |
let toNotation = (notation, int) => { | |
let convert = (int, buffer) => { | |
buffer.push(int % notation); | |
let current = Math.floor(int / notation); | |
return current ? convert(current, buffer) : buffer.reverse(); | |
}; | |
return convert(int, []).join(''); | |
}; | |
// Перевод десятичного числа в его двоичное представление | |
let toBinary = decimal => toNotation(2, decimal); | |
// Преобразование символа в число | |
let atoi = alpha => parseInt(alpha); | |
// Да | |
let invert = int => int ? 0 : 1; | |
// Получение дополнительного кода | |
let toAdditional = decimal => { | |
if (decimal > 0) { | |
return toBinary(decimal); | |
} else { | |
let binary = toBinary(decimal).split(''); // Преобразуем в массив строк | |
while (binary.length != 8) binary.unshift("0"); // Дополнение до 8-го розряда | |
let inverted = binary.map(atoi).map(invert); // Преобразуем в обратный код | |
let additional = toDecimal(inverted.join('')) + 1; // Отнимаем от него единицу | |
return toBinary(additional); // Возвращаем двоичное число | |
} | |
} | |
// https://goo.gl/pD0mHc | |
let grayEncode = decimal => decimal ^ (decimal >> 1); | |
let grayDecode = gray => { | |
let result = 0; | |
while (gray) { | |
result ^= gray; | |
gray >>= 1; | |
} | |
return result; | |
} | |
console.log("dec\tbin\tgcd\tgdc\tdec(gdc) add\n"); | |
let iterations = 10; | |
while (iterations--) { | |
let origin = rand(3, 100); | |
let binary = (origin).toString(2); | |
let encoded = grayEncode(toDecimal(binary)); | |
console.log("%s\t%s\t%s\t%s\t%s\t%s", | |
/*dec-origin*/ origin, | |
/*bin-origin */ binary, | |
/*gray-encoded*/ toBinary(encoded), | |
/*gray-decoded*/ toBinary(grayDecode(encoded)), | |
/*encoded-decimal*/ encoded, | |
/*additional*/ toAdditional(encoded)); | |
} |
This file contains 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
"use strict"; | |
let rand = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min; | |
let toDecimal = (value) => | |
value.split('').map((char, i, arr) => | |
parseInt(char) * Math.pow(2, arr.length - (i + 1))) | |
.reduce((sum, current) => sum + current); | |
let toNotation = (notation, int) => { | |
let convert = (int, buffer) => { | |
buffer.push(int % notation); | |
let current = Math.floor(int / notation); | |
return current ? convert(current, buffer) : buffer.reverse(); | |
}; | |
return convert(int, []).join(''); | |
}; | |
let toBinary = decimal => toNotation(2, decimal); | |
let atoi = alpha => parseInt(alpha); | |
let invert = int => int ? 0 : 1; | |
let toAdditional = decimal => { | |
if (decimal > 0) { | |
return toBinary(decimal); | |
} else { | |
let binary = toBinary(decimal).split(''); | |
while(binary.length != 8) binary.unshift("0"); | |
let inverted = binary.map(atoi).map(invert); | |
let additional = toDecimal(inverted.join('')) + 1; | |
return toBinary(additional); | |
} | |
} | |
let grayEncode = decimal => decimal ^ (decimal >> 1); | |
let grayDecode = gray => { | |
let result = 0; | |
while (gray) { | |
result ^= gray; | |
gray >>= 1; | |
} | |
return result; | |
} | |
console.log("dec\tbin\tgcd\tgdc\tdec(gdc) add\n"); | |
let iterations = 10; | |
while (iterations--) { | |
let origin = rand(3, 100); | |
let binary = (origin).toString(2); | |
let encoded = grayEncode(toDecimal(binary)); | |
console.log("%s\t%s\t%s\t%s\t%s\t%s", | |
/*dec-origin*/ origin, | |
/*bin-origin */ binary, | |
/*gray-encoded*/ toBinary(encoded), | |
/*gray-decoded*/ toBinary(grayDecode(encoded)), | |
/*encoded-decimal*/ encoded, | |
/*additional*/ toAdditional(encoded)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment