Thread: unions in C
View Single Post
  #6   Spotlight this post!  
Unread 10-11-2003, 20:21
Unsung FIRST Hero
Matt Leese Matt Leese is offline
Been-In-FIRST-Too-Long
FRC #1438 (The Aztechs)
Team Role: Engineer
 
Join Date: May 2001
Rookie Year: 1998
Location: Long Beach, CA
Posts: 937
Matt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond reputeMatt Leese has a reputation beyond repute
Send a message via AIM to Matt Leese
It will matter in some cases what you use and not matter in other cases.

Let me try and explain why. A signed byte and an unsigned byte represent the numbers from 0 to 127 in exactly the same manner. The values from 128 to 255 in an unsigned byte are represented as the numbers from -128 to -1 in a signed byte. Basically, 255 is represented in the same manner that -1 is represented. Now, for 0 to 255 output values, you don't want this to occur.

Now, as long as you correctly subtract 127 and then add 127, there should be no difference in the ending output as long as simple arithmetic is used (addition, subtraction, multiplication, and division). That's a function of binary arithmetic and not dependent on a certain processor. Basically, the code should be portable in that case. If you're using bitwise operations, I'd be a bit more careful.

My suggestion is to use a a word variable for calculations and then cast it to an unsigned char for output. It will waste a bit of RAM space but it will make sure there's enough room for all variable calculations.

Matt