Skip to content

Instantly share code, notes, and snippets.

@naughty-code
Created March 8, 2017 10:31
Show Gist options
  • Save naughty-code/f9efc75ef475325af59192ae5afc90c7 to your computer and use it in GitHub Desktop.
Save naughty-code/f9efc75ef475325af59192ae5afc90c7 to your computer and use it in GitHub Desktop.
CRC division algorithm
//variables
var Trama;//cadena con los datos a enciar que se quiere enviar//
var TramaCodificada;
var TramaCodificadaConRellenoDeBitConBanderas;
var TramaCodificadaConRellenoDeBit;
const generator = "10001000000100001"
//----main()------//
//trama de ejemplo //
Trama="1111110101011111110"; // se tabaja la trama como cadena
console.log(Trama);
TramaCodificada=CRC16(Trama);//CRC16 recibe un cadena
console.log(TramaCodificada.join(""));
//TramaCodificadaConRellenoDeBit=RellenoDeBit(TramaCodificada)
//console.log(TramaCodificadaConRellenoDeBit.join(""));
//TramaCodificadaConRellenoDeBitConBanderas = ColocarBanderas(TramaCodificadaConRellenoDeBit);
//console.log(TramaCodificadaConRellenoDeBitConBanderas);
const CRCDivision = (dividend, divisor) => {
let XOR = (a, b) => a.map((bit, index) => bit != b[index] ? '1' : '0');
let CRCDivisionRecursive = (dividend, lastRemainder) => {
let remainderWithoutLeftZeroes = lastRemainder.slice(lastRemainder.indexOf('1'));
let bitsNeeded = divisor.length - remainderWithoutLeftZeroes.length;
let remainder = remainderWithoutLeftZeroes.concat(dividend.slice(0, bitsNeeded));
if(remainder.length < divisor.length){
return lastRemainder.concat(dividend).slice();
}else{
return CRCDivisionRecursive(dividend.slice(lastRemainderLength),
XOR(remainder, divisor));
}
};
return CRCDivisionRecursive(dividend.slice(divisor.length), XOR(dividend.slice(0, divisor.length), divisor));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment