Skip to content

Instantly share code, notes, and snippets.

@Hugoberry
Last active February 27, 2017 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hugoberry/a35af475c658545d8b97027d1b771c26 to your computer and use it in GitHub Desktop.
Save Hugoberry/a35af475c658545d8b97027d1b771c26 to your computer and use it in GitHub Desktop.
Precomputin the CRC32 lookup table
calculateCRClookup = (divident)=>
let
// move divident byte into MSB of 32Bit CRC
curByte = Number.BitwiseShiftLeft(divident,24)
in
List.Accumulate({0..7},
curByte,
(_curByte,_)=>
let
// if MSB set, shift left and XOR with polynomial = 0x4C11DB7
then_clause = Number.BitwiseXor(Number.BitwiseShiftLeft(_curByte,1),0x4C11DB7),
// else shift one bit left
else_clause = Number.BitwiseShiftLeft(_curByte,1)
in //if MSB set
if Number.BitwiseAnd(_curByte,0x80000000)<>0 then then_clause else else_clause ),
// precompute CRC32 lookup table
CRC_table = List.Generate(()=>[count=0,crc=0], each [count]<256, // iterate over all possible input byte values 0-255
each [count = [count]+1, crc = calculateCRClookup(count) ], each [crc])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment