19
Jul
13

What is the size of a signed data type in Visual C++?

So, I was compiling some code the other day with warnings-as-errors enabled, like you do, and I hit a curious one:

 warning C4309: 'initializing' : truncation of constant value

This particular warning is generated when the code attempts to initialize (or assign, or convert) a variable (or parameter) to a value that is larger than the variable’s type can handle. The thing that makes this curious is that the line in question looks something like this:

short const tmp = 0xFFFF;

I’ll save you the bitmath and the Googling for the size of a short in Visual C++ and just tell you that, according to the documentation, a short is 2 bytes (16 bits) and so is 0xFFFF, which sets all 16 bits to 1. The above statement will generate the warning for any values of 0x8FFF and higher. Even more interesting, setting an unsigned short to 0xFFFF does not generate the warning. Does this mean that in VC++, signed shorts are actually 1 bit shorter than unsigned shorts??

Probably not. More likely, this just means that the compiler is being a bit aggressive with this warning. You see, Visual Studio (and g++, for that matter) has no objection to this code:

short const tmp = -0x0001

This is theoretically the exact same value as the first line, because VC++ uses two’s complement (which is not enforced by the C++ standard, by the way, but is used by most compilers) for the binary representation of signed numbers. My (mostly unsubstantiated) guess, though, is that the VC++ compiler throws this warning on the first statement but not the second because 65535, the unsigned value of 0xFFFF, would have to be be truncated to fit into a signed short (where the maximum allowable value is 32767), so the compiler would prefer it if you just used your negative sign to indicate your negativity, rather than the most significant bit, like any sane programmer would.

In this case, I think I’ll just suppress the warning.

Advertisements

4 Responses to “What is the size of a signed data type in Visual C++?”


  1. 1 Ben
    July 19, 2013 at 12:55 pm

    Do you mean that it warns for anything about 0x7FFF (0x8000 and above)? That makes sense to me. All shorts are 16 bits (2 bytes) but the range of signed shorts are -0x8000 to 0x7FFF, which mean there are only 15 bits for positive numbers in a signed short. If you want to use a signed variable but want to initialize it to 0xFFFF like it is unsigned, instead of disabling the warning, you could just put in an explicit cast (unsigned short)0xFFFF.

    • July 19, 2013 at 1:01 pm

      That’s true. Of course, it’ll just get cast right back to signed.

      Either way, I think it’s poor form for the compiler to complain about a perfectly legitimate value. Why it interprets 0xFFFF as 35565 instead of -1 is beyond me.

  2. 3 sparkyb6
    July 19, 2013 at 12:56 pm

    Do you mean that it warns for anything about 0x7FFF (0×8000 and above)? That makes sense to me. All shorts are 16 bits (2 bytes) but the range of signed shorts are -0×8000 to 0x7FFF, which mean there are only 15 bits for positive numbers in a signed short. If you want to use a signed variable but want to initialize it to 0xFFFF like it is unsigned, instead of disabling the warning, you could just put in an explicit cast (unsigned short)0xFFFF.

  3. July 23, 2013 at 8:42 am

    You’re much better looking than Visual Studio, but I’m afraid I agree with the compiler that it’s a warning. If what you mean is -1, then I think it’s better to put -1 rather than doing the complementing yourself. I was recently porting some code over to Arduino where the type sizes are not necessarily the same, and that caused all kinds of odd errors with bitmasks. And if you ever changed that short to an int, the value would suddenly, validly become the 65535 that you wrote instead of the -1 you “secretly” wanted.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


About

My name is Maitland Lederer, and I’m a video game developer. I learn stuff you probably already knew and have opinions you've probably already heard. I figured it might be a good idea for me to start writing down the stuff I've learned so I don't have to relearn it. It's not, like, great wisdom or anything. It's just things I happened to learn, usually today.

Header photo by D Sharon Pruitt, used under a Creative Commons License.


%d bloggers like this: