1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| #include <bits/stdc++.h> #define DBG(x) cerr << #x << " = " << x << endl
using namespace std; typedef long long LL;
unsigned short checksum(const vector<unsigned short> &v) { unsigned int sum = 0; for (int i = 0; i < v.size(); i++) { sum += v[i]; } while (sum > 0xFFFF) { sum = (sum & 0xFFFF) + (sum >> 16); } return ~sum; }
int main(int argc, char **argv) { vector<unsigned short> vec{ 0x0a01, 0xbf44, 0x0a02, 0x0002, 0x0011 , 0x0023,
0xc183, 0x0035, 0x0000, 0x0023,
0x06b0, 0x0100, 0x0001, 0x0000, 0x0000, 0x0000, 0x0562, 0x6169, 0x6475, 0x0363, 0x6f6d, 0x0000, 0x0100, 0x0100,
}; unsigned short sum = checksum(vec);
printf("checksum = 0x%X\n", sum); assert(sum == 0x22E4);
vec[8] = sum;
sum = checksum(vec); assert(sum == 0); printf("0x%X\n", sum);
return 0; }
|