clear

buf(1)= hex2dec('A5');
buf(2)= hex2dec('3F');
buf(3)= hex2dec('01');
buf(4)= hex2dec('12');
buf(5)= hex2dec('00');
buf(6)= hex2dec('02');

remain = 0;
for i=1:6
    remain = bitxor(remain, bitshift(buf(i), 8));
    for j=1:8
        if bitand(remain,hex2dec('8000'))
            remain = bitxor(hex2dec('1021'), bitshift(remain,uint16(1)));
        else
            remain = bitshift(remain,1);
        end
        remain = bitand(remain, hex2dec('FFFF'));
    end
end
remain
dec2hex(remain)

            