I'm adapting an algorithm for calculating a CRC 16-bit CCITT (XModem) value from an ASCII input. I've found some code here. I'm using the function the poster has in his question, but I've noticed at the bottom he does this:

calcrc = crc And &HFFFF

What's the purpose of applying such a bit mask that is all 1s to a value? Doesn't that just return the value again?

  • $\begingroup$ It could be because the code is mechanically generated from a template. It could be there also because some situation require changing it, and having it in place make it easy to make sure the masking is done at the right place. I have done that on occasion. $\endgroup$
    – babou
    Feb 25 '15 at 18:00

The reason is crc that it's calculating is defined to be a 16-bit value, the And &HFFFF will truncate the most significant bit outside the 16-bit range if there is any overflow during the calculation.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.