View Single Post
  #1   Spotlight this post!  
Unread 05-01-2008, 22:23
dmlawrence dmlawrence is offline
MIT '14
FRC #1751 (Warriors)
Team Role: Alumni
 
Join Date: Jan 2007
Rookie Year: 2006
Location: Long Island
Posts: 63
dmlawrence is an unknown quantity at this point
Question Signed and unsigned variables

What is the proper method to cast from an unsigned to a signed variable?

For example, suppose that u is an unsigned int and i is a signed int.

When u is 0, i should be -32768.
When u is 32768, i should be 0.
When u is 65535, i should be 32767.

Which of the following is correct:
i = (unsigned int) u or
i = (unsigned int) (u - 32768)?

Thanks,
David