Formatted with Google Java Formatter

This commit is contained in:
github-actions
2020-10-24 10:23:28 +00:00
parent a23bac99e8
commit 5d59a2e828
219 changed files with 13758 additions and 14582 deletions

View File

@ -2,30 +2,26 @@ package Others;
import java.util.BitSet;
/**
* Generates a crc32 checksum for a given string or byte array
*/
/** Generates a crc32 checksum for a given string or byte array */
public class CRC32 {
public static void main(String[] args) {
System.out.println(Integer.toHexString(crc32("Hello World")));
}
public static void main(String[] args) {
System.out.println(Integer.toHexString(crc32("Hello World")));
}
public static int crc32(String str) {
return crc32(str.getBytes());
}
public static int crc32(String str) {
return crc32(str.getBytes());
}
public static int crc32(byte[] data) {
BitSet bitSet = BitSet.valueOf(data);
int crc32 = 0xFFFFFFFF; // initial value
for (int i = 0; i < data.length * 8; i++) {
if (((crc32 >>> 31) & 1) != (bitSet.get(i) ? 1 : 0))
crc32 = (crc32 << 1) ^ 0x04C11DB7; // xor with polynomial
else
crc32 = (crc32 << 1);
}
crc32 = Integer.reverse(crc32); // result reflect
return crc32 ^ 0xFFFFFFFF; // final xor value
public static int crc32(byte[] data) {
BitSet bitSet = BitSet.valueOf(data);
int crc32 = 0xFFFFFFFF; // initial value
for (int i = 0; i < data.length * 8; i++) {
if (((crc32 >>> 31) & 1) != (bitSet.get(i) ? 1 : 0))
crc32 = (crc32 << 1) ^ 0x04C11DB7; // xor with polynomial
else crc32 = (crc32 << 1);
}
crc32 = Integer.reverse(crc32); // result reflect
return crc32 ^ 0xFFFFFFFF; // final xor value
}
}