Function of type unsigned int returns negative number

为君一笑 提交于 2019-12-06 09:47:04

Here's what I think is happening:

The value of tzInfo.Bias is actually -10. (0xFFFFFFF6) On most systems, casting a signed integer to an unsigned integer of the same size does nothing to the representation.

So the function still returns 0xFFFFFFF6.

But when you print it out, you're printing it back as a signed integer. So it prints-10. If you printed it as an unsigned integer, you'll probably get 4294967286.

What you're probably trying to do is to get the absolute value of the time difference. So you want to convert this -10 into a 10. In which you should return abs(tzInfo.Bias / 60).

You are trying to print an unsigned int as a signed int. Change %d to %u

_stprintf( ch, _T("A: %u\n"), getTimeZoneBias());
                       ^

The problem is that integers aren't positive or negative by themselves for most computers. It's in the way they are interpreted.

So a large integer might be indistinguishable from a small (absolute value) negative one.

One error is in your _T call. It should be:

_T("A: %u\n")

The function does return a non-negative integer. However, by using the wrong printf specifier, you're causing it to get popped off the stack as an integer. In other words, the bits are interpreted wrong. I believe this is also undefined behavior.

As other people have pointed out, when you cast to an unsigned int, you are actually telling the compiler to use the pattern of bits in the int and use it as an unsigned int. If your computer uses two's complement, as most do, then your number will be interpreted as UINT_MAX-10 instead of 10 as you expected. When you use the %d format specifier, the compiler goes back to using the same bit pattern as an int instead of an unsigned int. This is why you are still getting -10.

If you want the absolute value of an integer, you should try to get it mathematically instead of using a cast.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!