What is the correct way to utilize the BigInt right shift operator and XOR in JavaScript?

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?

Answer №1

The JavaScript crc you are using is a BigInt that has unlimited width. In contrast, your C crc is an unsigned integer with a fixed width of 32 bits. This means that when performing the calculation crc = poly ^ (crc >> 1);, the higher 32 bits of your poly are not utilized. To achieve the same result, you need to ensure that there are 64 bits in that operation:

uint64_t crc = 1;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having difficulties in dynamically swapping audio sources using JavaScript?

It seems like I'm overlooking something simple here. The issue I'm facing involves creating a series of buttons with different audio sources as titles. When a user clicks on a button, the corresponding source should update in the audio player. T ...

jQuery loading and updating

I am faced with a scenario where I have both a 'test.html' and a 'test.php' files. The content of 'test.html' is as follows: <html> <head> <script type="text/javascript" src="/js/jquery-1.8.2.js">< ...

Ensure that the promise is fulfilled only if a specific condition is met

I have a complex if-else condition in my code that includes different promises. Once the logic determines which condition to enter and executes the corresponding promise, I need to ensure that a final promise is always executed. if (a < 5) { vm.pr ...

Sending information (prop) from _app.js to getServerSideProps in a page on the most up-to-date version of NextJS

I have a unique custom _app.js that I created: const CustomLayout = ({ children }) => (children); const myApp = ({ Component, pageProps }) => { pageProps.url = 'another url'; return ( <CustomLayout> <Co ...

Conceal the <p> tag if the content inside is either "0" or blank

I'm currently working on a basic calculator project and I've hit a roadblock. I need to hide certain elements based on conditions. The code snippet with explanations is provided below. function calculateArea() { var length = document.getElem ...

Integrating TypeScript into an established create-react-app project

Struggling to integrate TypeScript into an existing create-react-app? I've always added it at the beginning of a project using create-react-app my-app --scripts-version=react-scripts-ts, but that's not working this time. The only "solution" I co ...

Is NodeJS primarily used as a socket library for network communication?

Here is a server program written in C language using socket functionality provided by libC # include <unistd.h> # include <sys/socket.h> # include <sys/types.h> # include <string.h> #include <netinet/in.h> main(){ int listfd ...

Having trouble connecting to Azure SQL server from my Node.js application

I'm attempting to run a basic query to my Azure SQL server from NodeJs, but I'm encountering errors indicating that I cannot establish a connection to the server. The details provided in my code are correct because I've used them successfull ...

What is the best way to create 7 or 8 column grids with Vuetify's v-row and v-col components?

I understand that vuetify's grid system is based on a 12-column flex-box layout, but I would like to customize it to have 7 or 8 columns by default instead of the usual 12. In the code snippet below, you can see my attempt: <v-row> <v-col ...

Problem encountered with 'extern': issue with undefined external symbol

I encountered the error message below while compiling my code, even though I'm certain that the variables are declared in an external Project2.c file. Could someone please provide me with a hint on what I might have done wrong? Thank you. 1>main.o ...

Is it possible to switch between different fabricJS canvases seamlessly?

Consider this scenario where I have three canvas elements: <canvas id="c1" width="400" height="300"></canvas> <canvas id="c2" width="400" height="300"></canvas> <canvas ...

Creating a double pointer structure to initialize a 2D array of double type pointers

Currently, I am attempting the following: #include <stdio.h> #include <stdlib.h> typedef struct { double **arr; }communication; int main() { int r = 3, c = 4, i, j, count; communication *comm; comm->arr = (double **)mall ...

Organizing Files: Importing Sub-Components in Node.js

Here is my Node.js module/npm package structure: |- dist/ |- - requirejs/ |- - - [content in amd pattern ...] |- - node/ |- - - index.js |- - - submodules/ |- - - - submodule1.js |- - - - [submodule2.js and so on] |- package.json |- README.md I can easil ...

Console output shows that the function results in undefined

When I pass a string parameter to a function, I expect the console to display "reff", but it is showing "undefined" instead. Here is the code snippet: var _ref; function foo(_ref='reff') { var bar = _ref.bar; return console.log(bar); } foo ...

JQuery: Issues with attaching .on handlers to dynamically added elements

I'm currently developing a comment system. Upon loading the page, users will see a box to create a new comment, along with existing comments that have reply buttons. Clicking on a reply button will duplicate and add the comment text box like this: $( ...

Vue - Display components next to sidebar

Having an issue with the vue-sidebar-menu. The sidebar is appearing over the components instead of beside them. Here is a screenshot of the problem: https://i.sstatic.net/cVgI6.jpg <template> <div id="app"> <sidebar-menu :menu="menu" ...

The integration of VueJS with Axios and the Google Maps API

Currently following [this][1] guide to develop a Google Map and now I am looking to execute a GET request with Axios: axios.get("http://localhost:8080/mapjson").then(function(response) { }) in order to integrate the information from my JSON file into the ...

Issue a response with an error message when making an $http request

When using Angular, I encounter a situation where I need to handle error messages returned from an $http request. However, I am unsure of the best approach. In my Express code, I typically handle errors like this: res.send(400, { errors: 'blah' ...

Attempting to eliminate an HTML element using JavaScript

Currently, I am working on creating an image rotator in JavaScript. The CSS fade animation I have applied only seems to work during element creation, so I am forced to remove the element on each loop iteration. The main issue I am encountering is related ...

Puppeteer patiently waits for the keyboard.type function to complete typing a lengthy text

Currently, I am utilizing puppeteer for extracting information from a particular website. There is only one straightforward issue with the code snippet below: await page.keyboard.type(data) await page.click(buttonSelector) The initial line involves typin ...