Last active
September 26, 2022 15:10
-
-
Save israellot/7800b45144b621e60cfed1203c8d8cc2 to your computer and use it in GitHub Desktop.
Junior Checksum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Compute a 32-bit big-endian checksum on arr | |
public static uint Checksum(ReadOnlySpan<byte> arr) | |
{ | |
if (arr.Length == 0) return 0; | |
uint sum0 = 0, sum1 = 0, sum2 = 0, sum3 = 0; | |
for(var i = 0; i<arr.Length; i++) | |
{ | |
switch (i % 4) | |
{ | |
case 0: sum0 += arr[i]; break; | |
case 1: sum1 += arr[i]; break; | |
case 2: sum2 += arr[i]; break; | |
case 3: sum3 += arr[i]; break; | |
} | |
} | |
var sum = sum3+ (sum2 << 8) + (sum1 << 16) + (sum0 << 24); | |
return sum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment