Skip to main content
gamedev.net
🔒 Locked

Variable type preferrence

Started by jdinger2 2 replies 350+ views
Original Post
jdinger2
jdinger2
Is there any reason to use an int or long instead of a byte (UDT - unsigned char) when I know that the value contained won''t be greater than 100? Is there a specific speed increase with either? Normally for variables that will only be needed to hold small values I just use a byte: typedef unsigned char byte; byte byNumberOfSurfs; I just figured using the variable type that would take the smallest amount of memory was the way to go.
MSVC++ 6DirectX 7
Fuzztrek
Fuzztrek
Hmm.. not sure waht you want, but here's something that might help (it helped me ): (ARGGGGGG.. stupid forum doesn;t support HTML)
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>TYPE </td>
<td>16 BIT </td>
<td>32 BIT </td>
<td>RANGE </td>
</tr>
<tr>
<td>unsigned short int</td>
<td>2 bytes</td>
<td>2 bytes</td>
<td>0 to 65 535</td>
</tr>
<tr>
<td>unsigned long int</td>
<td>2 bytes</td>
<td>2 bytes</td>
<td>-32 768 to 32 767</td>
</tr>
<tr>
<td>unsigned int</td>
<td>4 bytes</td>
<td>4 bytes</td>
<td>0 to 4 294 967 295</td>
</tr>
<tr>
<td>short int</td>
<td>4 bytes</td>
<td>4 bytes</td>
<td>-2 147 483 648 to 2 147 483 647</td>
</tr>
<tr>
<td>long int</td>
<td>2 bytes</td>
<td>4 bytes</td>
<td>(16) -31 768 to 32 767; (32) -2 147 483 648 to 2 147 483 647</td>
</tr>
<tr>
<td>int</td>
<td>2 bytes</td>
<td>4 bytes</td>
<td>(16) 0 to 65 535; (32) 0 to 4 294 967 295</td>
</tr>
<tr>
<td colspan="4" height="15"> </td>
</tr>
<tr>
<td>char</td>
<td>1 byte</td>
<td>1 byte</td>
<td>0 to 256</td>
</tr>
<tr>
<td>wchar_t</td>
<td>2 bytes</td>
<td>2 bytes</td>
<td>0 to 65 535</td>
</tr>
<tr>
<td>bool</td>
<td>1 byte</td>
<td>1 byte</td>
<td>0 to 1</td>
</tr>
<tr>
<td>float</td>
<td>4 bytes</td>
<td>4 bytes</td>
<td>1.2e-38 to 3.4e38</td>
</tr>
<tr>
<td>double</td>
<td>8 bytes</td>
<td>8 bytes</td>
<td>2.2e-308 to 1.8e308</td>
</tr>
<tr>
<td>long double</td>
<td>10 bytes</td>
<td>10 bytes</td>
<td>3.4e-4932 to 1.1e+4932</td>
</tr>
</table>

Whew! Heheh. Usually I use USINT (0 to 65 535):

typedef unsigned short int USINT;      


For most of my integer variables (mostly counters, and such that do not need to be negative). I would typedef char aswell, and have it something like INT255, but my compiler complains and I would have to cast them (for DirectX functions at least). As far as I know you cannot create your own variables in c/c++ (so I do not think you would be able to have a variable with a range of 0 to 100).

Hope this helps!!

Fuzztrek

¬_¬

[edited by - Fuzztrek on October 28, 2002 10:49:34 PM]

[edited by - Fuzztrek on October 28, 2002 10:49:55 PM]
jdinger2
jdinger2
Thanks for the reply, but I''m not trying to get a variable that ranges from 0 to 100 (I was just using that as an example range). I understand the values each type can hold and how signed/unsigned-ness affect it. What I''m looking for is to know if either is faster to use. I remember reading that longs compute faster because they''re 64-bit variables.
MSVC++ 6DirectX 7
BeerNutts
BeerNutts
Using Bytes or Shorts is slower on 32-bit and higher machines.
My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.