Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does java.util.zip.CRC32.getValue() return a long, not an int?

Tags:

See the title. The returned value is 32 bits, right? Why not return an int?

like image 324
Steveo Avatar asked Jun 12 '13 22:06

Steveo


People also ask

Can a CRC32 be negative?

The CRC32 checksum sometimes returns negative values. You can use an unsigned shift (x >>> 0) to force JavaScript to treat the arithmetic as unsigned.

What is CRC32 in Java?

All Implemented Interfaces: Checksum. public class CRC32 extends Object implements Checksum. A class that can be used to compute the CRC-32 of a data stream. Passing a null argument to a method in this class will cause a NullPointerException to be thrown.

What is CRC32 used for?

CRC32 is an error-detecting function that uses a CRC32 algorithm to detect changes between source and target data. The CRC32 function converts a variable-length string into an 8-character string that is a text representation of the hexadecimal value of a 32 bit-binary sequence.


2 Answers

Because if it returned an int, half of the CRC's would be negative. The expectation is that a 32-bit CRC is unsigned, i.e. 0..4294967295, which cannot be represented in an int.

like image 60
Mark Adler Avatar answered Sep 22 '22 12:09

Mark Adler


java.util.zip.CRC32 implements the Checksum interface, which requires a long return type for getValue(), therefore requiring a long for a 32-bit checksum; the upper 32 bits of the output are almost definitely 0.

like image 36
David Futcher Avatar answered Sep 25 '22 12:09

David Futcher