Int32 vs int8 or uint8

Why are we using int32 to store the number of towers? Wouldn’t int8 be fine? It can hold a value up to 127 which is quite large. Even if we plan to have more that 127 enemies, why not use int16 then?

Also, why not make it uint? Are we expecting a negative number of towers? Or did I somewhere miss why I should be doing this?

Sorry if this is a silly question. It just feels like I am missing something. I tried it with uint8 and it seems to work fine, but now I’m not sure if there are downsides to this.

3 Likes
  1. The integer isn’t being stored as part of some type so in essence it doesn’t really matter what fixed width type you use, it’ll probably be stored in a 64-bit register regardless.
  2. Using unsigned integers just because “it can’t be negative” for the size type of the C++ standard containers is largely considered a mistake by the committee*. The amount of problems caused by them outweighs gained by them (the benefits in a pre-64-bit world are different). It’s the same reason Unreal uses int32 for TArray and and not uint32.
    This is further explained by this popular library for linear algebra
    FAQ - Eigen
    * also noted with timestamps in that link.
3 Likes
  1. Oh, I didn’t know that my types would merely be used as a suggestion. I thought int8 would be int8
  2. I did not expect the answer to go this deep. Thanks, it looks like a fun rabbit hole!

Sorry I forgot that this is stored in the class, so you could make it int8 but it’s not really going to affect anything here. If this were a multiplayer game where you would be sending this data over a network then it would more beneficial.

Also as a sidenote, it’s not a suggestion. If you had

using int8 = signed char; // assuming char == 8-bits

int8 GetTurretCount();

bool IsLessTha3()
{
    return GetTurretCount() < 3;
}

Then the value returned from GetTurretCount would be put in the eax register then from within IsLessThan3 it would just read the lower 8-bits of that.

2 Likes

I never thought I’d take this long reading a comment! This is well beyond what I knew so far.

If this were a multiplayer game where you would be sending this data over a network then it would more beneficial.

That’s one of the things I was thinkingm, but only vaguely. I’ll just stick to int32 for now.

…eax register

This is the main thing that made this comment so hard to understand for me, but now I’ve learned something fun. I also didn’t know about using. You did the same to me on discord some time ago, dropping all sorts of cool things that seem tiny on the surface but end up costing me a lot of time haha.

Thanks again!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms