-2

I am working on an assignment and it is asking me to calculate a checksum by stripping off the least significant byte of a ones complement version of an integer...

This is the part of the assignment outline I am confused by:

"The CHECKSUM field (MM) value is calculated by taking the least significant byte of the 1’s Complement value of the sum of the COUNT, ADDRESS and DATA fields of the record"

Im a little bit unclear on what this means, as I haven't really worked with ones complements or LSB's in C.

What I have so far is:

int checkSum(int count, int address, char* data)
{
int i = 0;
int dataTotal = 0;

for(i = 0; i < strlen(data); i += 2)
{
    dataTotal += (getIntFromHex(data[i]) * 16) + getIntFromHex(data[i + 1]);
}

int checksum = ~(count + address + dataTotal) & 1;
printf("Checksum: %.2X\n", checksum);

return checksum;
}

I didn't really expect this to work but I've done some research and this is what I came up with.

I need some clarification on what is meant by the least significant byte.

P.S. The reason for the for loop is simply just to get the total of the data. Not important for this but the code uses the variable so I figured I would just copy the whole thing to avoid confusion.

Kyle Jensen
  • 419
  • 9
  • 27
  • You forgot to ask us a question. Can you be specific about what you don't understand or what you need help with? Do you know what 1's complement is? Do you know what a least significant byte is? Also, I see a call to `strlen` in your code. But I'm not quite sure what strings have to do with anything. It sounds like your input is not a string. – David Schwartz Feb 24 '16 at 01:44
  • Updated my question. I just want to know how I can go about doing this algorithm, – Kyle Jensen Feb 24 '16 at 01:58
  • The spec is not very clear. To start with, the int types may be a problem since the sum may overflow maxInt.and so cause UB. Operations such as calculating checksums, CRC's are typically performed with unsigned types. – Martin James Feb 24 '16 at 02:00
  • 1
    Is the data a string? Are the COUNT and ADDRESS fields single bytes? If not, how many bytes are they and what's their byte order? Do you have any sample data with computed checksum? You haven't given us enough information to solve the problem and you haven't asked us a sufficiently specific question for us to answer your question. – David Schwartz Feb 24 '16 at 02:00

1 Answers1

1

I need some clarification on what is meant by the least significant byte.

The last significant byte means the number mod 256, a result from zero to 255.

unsigned leastSignificantByte(unsigned j)
{
    return j & 0xff;
}
David Schwartz
  • 179,497
  • 17
  • 214
  • 278