• Post Reply Bookmark Topic Watch Topic
  • New Topic

Check Sum -- 16 bit sum with out carry check word  RSS feed

 
Patrick Walter
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a requirement to create a checksum of a set of 16bit words by summing these words without carry checkword. Can any one provide guidance and/or code.

Not sure the best way to perform the "without carry" requirement.

thanks!!!
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

I'm not sure what you mean by "without carry" - if you don't want one, don;t calculate one.

What datatype are the values in - if it's an int (which is a 32 bit type) then you'll have no problem with numbers being negative, and you can simply add them up in either an int or a long.

If the values are of type short (which is 16 bit) then the values could be negative. You'd need to check the most significant bit, and if it's set, treat the number especially before adding it to the sum.
 
Patrick Walter
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.

I'm not sure what you mean by "without carry" - if you don't want one, don;t calculate one.

I am using short (16 bit) values. Acually bytes. Requirement is to create a 16 bit check sum with "no carryover".

What do you mean by "check the most significant bit, and if it's set, treat the number especially before adding it to the sum. "
How do I "treat" the number.

example code would help....

thanks!!!
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was talking about 16 bit numbers where the most significant bit (MSB) is set , i.e., negative numbers as far as the Java short type is concerned. Since their actual value is between 32768 and 65536 (and not between -32768 and -1 as Java would think) you'd need to handle them especially.

But since you're adding bytes, this can't happen. The only thing to watch out for is if the sum becomes larger than 32767 because a short type will then become negative. This may be impossible unless you have a lot of numbers you're adding up, though.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!