• short and long?
14 replies, posted
• So I was watching a couple tutorial videos for C++ programming and the guy was going over signed, unsigned, short, and long INT types. He mentioned to go ahead and always use long, and not short, but to keep in mind that long has a larger file size than short. My question is this though, in a game design sense, when might it be better to use short compared to long? A few examples would be nice.
• On most platforms a char is 1 byte, short is 2 bytes, int is 4, and the size of long depends on the OS. Whatever tutorial you're using, it's ass, longs are pretty much never (explicitly) used over ints. In game design you don't really need to care much about the capacity of your integers, unless you're storing a very large number or trying to make your networking protocol as efficient as possible.
• int is the most general one that is used for all integer needs. short and long should be used only when there are special requirements for the size.
• As Dajoh said, the person who made your tutorial doesn't seem to really know what he's doing. Longs will almost [b]never[/b] be used. There are only very extreme scenarios where you would ever have to actually use a long, due to the very nature of such extreme numbers. As Dajoh (again) said, a short is 2 bytes, or 16 bits. This means that the maximum size of an unsigned short is 2^16, or a bit over 65,000. This is fairly small, so you won't see shorts be used terribly often - in fact, one could almost argue you'd see chars, with a max size of 1 byte, or 8 bytes, which is unsigned 256, more than shorts, because generally if you're dealing with numbers that small, your numbers are probably less than 255, so characters work just fine. Longs vary in size; for 32-bit Windows, C++'s longs are 4 bytes, or 32 bits long, which means they have an unsigned capacity of 2^32, or a good bit over 4 billion. In 32-bit Windows, Java's longs are a massive 8 bytes, or 64 bits, though since Java requires its longs to be signed, you have a range of &#8722;9,223,372,036,854,775,808 to +9,223,372,036,854,775,807. So you should almost never have to use longs over shorts. And then, as mentioned previously, simple integers are the most common form, and they should do you fine. Only use chars if you know, for certain, your numbers won't be going above 255, and I honestly can't really think of an instance where you'd really use a short. Just stick with integers, unless those extraordinary circumstances for longs calls upon you.
• [QUOTE=Gmod4ever;36067874]...[/QUOTE] It should be worth mentioning that longs are always 32 bits on Windows (because of backward compatibility), but on Linux they're 32 or 64 bits depending on if you have a 32 or 64 bit OS. And as Jookia said, you should use [b]cstdint[/b] for when you absolutely have to have the right amount of bits.
• Also, longs and ints don't really impact "file size" of a program.
• I lost a mark on a lab exercise because some statistics data I collected overflowed the regular ints I was using. Int should be fine for pretty much anything, just consider the limits of them if you start getting strange numerical errors. I agreed to help with a game some uni friends were making a while ago, and one of the programmers kept using UInt8 or something (in C++) if he knew the value wouldn't exceed 255. That really peeved me.
• [QUOTE=geel9;36071869]Also, longs and ints don't really impact "file size" of a program.[/QUOTE] -snip- apparently it does OP the tutorial you saw sounds rather misinformed, check out this table: [url]http://www.cplusplus.com/doc/tutorial/variables/[/url] Only use larger data types if you actually need them, though int is commonly used even for things that would require 1 byte Usually not too much of a problem on computer platforms, but you'd need to be careful on an embedded system