• Post Reply Bookmark Topic Watch Topic
  • New Topic

unsigned 8 bit integers,, and checksums  RSS feed

 
Mike Southgate
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) how would I represent unsigned 8 bit integers in Java? Can I do this using a type or should I use byte, write out the value in unsigned 8 bit binary, and then reverse out the 2's complement to get the value? As an example 16 decimal = 0001 0000 and that works fine as it's not a 2's complement negative. But 254 = 1111 1110, subtract 1 = 1111 1101, now filp the bits = 1000 0010. Now we need to represent this in 2's complement form, so add 1 = 1000 0011 and flip the bits = 1111 1100 = -125 ? Did I do this right?

2) I need to calculate a checksum that is defined as the 2's complement of the sum of a series of bytes. The checksum is 8 bits. Now, it's pretty clear that 8 bits won't handle a very large series of bytes before it overflows to 9 bits etc. Anyone have experience with exactly how this is done. Do you just ignore the bits above 8 or do they wrap around? If the high-order bit of the sum ends up being 1, that would indicate a negative number. Would you flip that bit too during the two's complement conversion?

Thanks in advance

ms
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!