After spending the entire day trying to figure out how to shift bits to the right in JavaScript, I realized that my test program written in C language was not giving the correct results for comparison.
If anyone can guide me in the right direction, please let me know.
const poly = BigInt('0x42F0E1EBA9EA3693');
let crc = BigInt(1);
for (let k = 0; k < 8; k++) {
crc = poly ^ (crc >> 1n);
const c1 = crc & 0xFFFFFFFFn;
console.log('crc:', c1.toString(16).padStart(8, '0'));
}
// output:
// crc: a9ea3693
// crc: 7d1f2dda
// crc: 9765a07e
// crc: e258e6ac
// crc: 58c645c5
// crc: 05891471
// crc: ab2ebcab
// crc: 7c7d68c6
#include <stdio.h>
#include <stdint.h>
int main()
{
uint64_t poly = 0x42F0E1EBA9EA3693;
unsigned int crc = 1;
for (unsigned int k = 0; k < 8; k++)
{
crc = poly ^ (crc >> 1);
printf("crc: %x\n", crc);
}
return 0;
}
// output
// crc: a9ea3693
// crc: fd1f2dda
// crc: d765a07e
// crc: c258e6ac
// crc: c8c645c5
// crc: cd891471
// crc: cf2ebcab
// crc: ce7d68c6
In an attempt to replicate the C language's behavior, I tried using Uint8Array instead of BigInt in JavaScript, but I still encountered errors. Any suggestions on how to achieve the desired result?