View Single Post
  #2   Spotlight this post!  
Unread 05-01-2008, 22:34
mluckham's Avatar
mluckham mluckham is offline
Registered User
FRC #0758 (Sky Robotics)
Team Role: Mentor
 
Join Date: Mar 2006
Rookie Year: 2006
Location: Ontario, Canada
Posts: 116
mluckham will become famous soon enoughmluckham will become famous soon enough
Re: Signed and unsigned variables

Code:
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)?

As a matter of form, neither is correct, although the second is closest to your requirements. Assuming you have defined 'u' as unsigned int, and 'i' as int, you use:

i = (int) (u - 32768);

Last edited by mluckham : 05-01-2008 at 22:54.