(unsigned) char, bitsets when using - + / *

Moderator
Posts: 373
Joined: 2006.08
Post: #1
after reading an old book about C, I decided to do some optimizing to my graphics engine. Beforehand, I had not known that char could contain an int value, and I had never heard of bitsets. For many of the variables that I was using, I only needed a small range of numbers...some only needed a possible combination of three values, so I used a bitset 2, and some needed a slightly larger, but not huge, so I used char. After googling around for awhile, I found this: http://www.eventhelix.com/RealtimeMantra...PPCode.htm article, which basically says that the C/C++ (I'm compiling in C++) compiler automatically recasts char variables to a signed int when doing mathematical operations like addition, subtraction, etc. and therefore, if speed instead of memory is your main concern, you should stick with int instead of chars. Is this true? I'm not outputting the value as a char, only as an int, so should I go ahead and use ints instead of chars? Does the same apply to bitsets? If I use an unsigned char, and then perform math with it, will it convert it into a signed char, or will it keep the unsigned status? What if I use an unsigned int instead of an unsigned char?
Thanks Smile
-wyrmmager

Worlds at War (Current Project) - http://www.awkward-games.com/forum/
Quote this message in a reply
Sage
Posts: 1,403
Joined: 2005.07
Post: #2
Yes use ints, also profile your code then optimize the bits which are slow.

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Moderator
Posts: 373
Joined: 2006.08
Post: #3
alright; thanks for the quick reply Smile I know about general optimizing rules, but I figure the code requires less work overall if I do things the faster way the first time XD
Again, thanks Smile
-wyrmmage

Worlds at War (Current Project) - http://www.awkward-games.com/forum/
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #4
On modern computers, everything is done in 32 bits (or 64 bits) regardless of if it's 32 bits or less. This means that if you have a char, it loads the 8 bits, then stores it in a 32 bit register. When you do any operations on it, it's using all 32 bits. When it gets time to store that value back into memory, it simply throws away all but 8 bits of the value. In the end, it does the same amount of work as an int. The only difference is it uses less memory, but due to alignment constraints (such as ints must be aligned on 4 byte boundaries), it may not matter anyway. In the end, it's not worth it. If anything, it may even make things worse.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #5
Always profile before optimizing. Very often, horrifically inefficient pieces of code (like bits written in Ruby) have absolutely zero effect on the performance. There's no point in "trying to do things the fast way first", that's just a guarantee of wasting your time. Do things the easy way first, then if (and only if) performance is a problem, profile, then optimize the bits that are actually causing you problems.
Quote this message in a reply
Moderator
Posts: 373
Joined: 2006.08
Post: #6
thanks for all of the help...just one last question then Smile
If you have code like:
Code:
unsigned int ui = 2;
signed int si = -1;
ui = ui + si;

what happens? Does the unsigned int get converted into a signed int for the operation?
Thanks again Smile
-wyrmmage

Worlds at War (Current Project) - http://www.awkward-games.com/forum/
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #7
C99 Spec Wrote:6.3.1.3 Signedand unsigned integers
1 When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged.
2 Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.
3 Otherwise, the newtype is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised.

(whatever that really means, the answer "in practice" is that you get 1. I think how it gets there is that -1 gets converted to unsigned, unsigned addition gets done (which overflows), and the overflow is discarded)
Quote this message in a reply
Moderator
Posts: 373
Joined: 2006.08
Post: #8
very interesting stuff Smile
Well, that answers all of my questions... thanks for all the help guys Grin

Worlds at War (Current Project) - http://www.awkward-games.com/forum/
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  converting text input to a c char NelsonMandella 6 4,094 Feb 4, 2010 10:58 AM
Last Post: NelsonMandella
  Char* to double, int, and back wyrmmage 18 6,680 Jun 24, 2007 11:09 AM
Last Post: akb825
  Return char if input is char, else - int? Achithyn 2 2,977 Aug 6, 2006 03:43 AM
Last Post: backslash
  How do unsigned integers differ from signed integers? ferum 3 3,513 Apr 11, 2006 01:52 PM
Last Post: ferum
  C/C++ Int To Char* Nick 4 11,017 Sep 12, 2005 10:02 AM
Last Post: Zekaric