Problem with big array of bytes that not can convert the CRC Value to unsigned integer #22

Closed
opened 2022-09-16 21:43:21 +00:00 by RickardPettersson · 1 comment
RickardPettersson commented 2022-09-16 21:43:21 +00:00 (Migrated from github.com)

I have a byte array that i trying to do CRC sum on and i getting a signed Integer as the library describe but when i try to convert the signed integer to a unsigned integer i get the same value

Here is a example where i put in a array of integers and gets crcValue = 1070418610 in the console:

var crcValue = CRC32.buf([0, 128, 0, 1, 35, 91, 123, 34, 73, 115, 69, 109, 112, 116, 121, 34, 58, 102, 97, 108, 115, 101, 44, 34, 88, 34, 58, 50, 56, 50, 44, 34, 89, 34, 58, 55, 52, 53, 125, 93]); console.log('crcValue', crcValue);

and here i try to convert between signed and unsigned 32-bit integers and get the same value back 1070418610:

var crcValue = CRC32.buf([0, 128, 0, 1, 35, 91, 123, 34, 73, 115, 69, 109, 112, 116, 121, 34, 58, 102, 97, 108, 115, 101, 44, 34, 88, 34, 58, 50, 56, 50, 44, 34, 89, 34, 58, 55, 52, 53, 125, 93]) >>> 0; console.log('crcValue', crcValue);

but if i make a small array it works like this returnes -1950775789:

var crcValue = CRC32.buf([0, 1, 2, 3]); console.log('crcValue', crcValue);

and this returns 2344191507:

var crcValue = CRC32.buf([0, 1, 2, 3]) >>> 0; console.log('crcValue', crcValue);

What is wrong with the big array?

I have a byte array that i trying to do CRC sum on and i getting a signed Integer as the library describe but when i try to convert the signed integer to a unsigned integer i get the same value Here is a example where i put in a array of integers and gets crcValue = 1070418610 in the console: `var crcValue = CRC32.buf([0, 128, 0, 1, 35, 91, 123, 34, 73, 115, 69, 109, 112, 116, 121, 34, 58, 102, 97, 108, 115, 101, 44, 34, 88, 34, 58, 50, 56, 50, 44, 34, 89, 34, 58, 55, 52, 53, 125, 93]); console.log('crcValue', crcValue);` and here i try to convert between signed and unsigned 32-bit integers and get the same value back 1070418610: `var crcValue = CRC32.buf([0, 128, 0, 1, 35, 91, 123, 34, 73, 115, 69, 109, 112, 116, 121, 34, 58, 102, 97, 108, 115, 101, 44, 34, 88, 34, 58, 50, 56, 50, 44, 34, 89, 34, 58, 55, 52, 53, 125, 93]) >>> 0; console.log('crcValue', crcValue);` but if i make a small array it works like this returnes -1950775789: `var crcValue = CRC32.buf([0, 1, 2, 3]); console.log('crcValue', crcValue);` and this returns 2344191507: `var crcValue = CRC32.buf([0, 1, 2, 3]) >>> 0; console.log('crcValue', crcValue);` What is wrong with the big array?
SheetJSDev commented 2022-09-17 04:30:46 +00:00 (Migrated from github.com)

The behavior is correct.

There are two types of 32-bit integers that JS specifies:

  1. "signed 32-bit integers": the integer range is -(2**31) ... 2**31 - 1 (-2147483648 ... 2147483647). Operators like | and & always coerce to signed 32-bit integers. For example, 4000000000|0 is -294967296

  2. "unsigned 32-bit integers": the integer range is 0 ... 2**32 - 1 (0 ... 4294967295). The operator >>> always returns an unsigned 32-bit integer. For example, (-1)>>>0 is 4294967295 .

Integers in the range 0 ... 2**31-1 (0 ... 2147483647) have the same bit representation as signed 32-bit integer and unsigned 32-bit integer, so | 0 won't negate them and >>> 0 won't change the result.
1070418610 is in that range and thus won't be changed.

The behavior is correct. There are two types of 32-bit integers that JS specifies: 1) "signed 32-bit integers": the integer range is `-(2**31) ... 2**31 - 1` (`-2147483648 ... 2147483647`). Operators like `|` and `&` always coerce to signed 32-bit integers. For example, `4000000000|0` is `-294967296` 2) "unsigned 32-bit integers": the integer range is `0 ... 2**32 - 1` (`0 ... 4294967295`). The operator `>>>` always returns an unsigned 32-bit integer. For example, `(-1)>>>0` is `4294967295` . Integers in the range `0 ... 2**31-1` (`0 ... 2147483647`) have the same bit representation as signed 32-bit integer and unsigned 32-bit integer, so `| 0` won't negate them and `>>> 0` won't change the result. `1070418610` is in that range and thus won't be changed.
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sheetjs/js-crc32#22
No description provided.