简体   繁体   English

unsigned int类型的函数返回负数

[英]Function of type unsigned int returns negative number

Wow I thought I knew my C++ but this is weird 哇我以为我知道我的C ++,但这很奇怪

This function returns an unsigned int so I thought that means I will never get a negative number returned right? 这个函数返回一个unsigned int所以我认为这意味着我永远不会得到一个负数返回吗?

The function determines how many hours ahead or behind of UTC you are. 该函数确定您前进或后退的时间。 So for me I'm in Australia, Sydney so I am +10 GMT which means I am UTC = LocalTime + (-10). 所以对我来说,我在澳大利亚,悉尼所以我是+10 GMT,这意味着我是UTC = LocalTime +( - 10)。 Therefore the GetTimeZoneInformation correctly determines I am -10. 因此,GetTimeZoneInformation正确地确定我是-10。

BUT my function returns an unsigned int so shouldn't it return 10 not -10? 但我的函数返回一个unsigned int所以不应该返回10而不是-10?

unsigned int getTimeZoneBias()
{
    TIME_ZONE_INFORMATION tzInfo;
    DWORD res  = GetTimeZoneInformation( &tzInfo );

    if ( res == TIME_ZONE_ID_INVALID )
    {
        return (INT_MAX/2); 
    }

    return (unsigned int(tzInfo.Bias / 60));  // convert from minutes to hours         
}

TCHAR ch[200];
_stprintf( ch, _T("A: %d\n"), getTimeZoneBias()); // this prints out A: -10
debugLog += _T("Bias: ") + tstring(ch) + _T("\r\n");

You are trying to print an unsigned int as a signed int . 您正在尝试将unsigned int打印为signed int Change %d to %u %d更改为%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. 因此,大的整数可能与小的(绝对值)负整数无法区分。

Here's what I think is happening: 这就是我的想法:

The value of tzInfo.Bias is actually -10. tzInfo.Bias的值实际上是-10。 ( 0xFFFFFFF6 ) On most systems, casting a signed integer to an unsigned integer of the same size does nothing to the representation. 0xFFFFFFF6 )在大多数系统上,将有符号整数转换为相同大小的无符号整数对表示没有任何作用。

So the function still returns 0xFFFFFFF6 . 所以函数仍然返回0xFFFFFFF6

But when you print it out, you're printing it back as a signed integer. 但是当你打印出来时,你将它打印为有符号整数。 So it prints -10 . 所以它打印-10 If you printed it as an unsigned integer, you'll probably get 4294967286 . 如果你把它打印成无符号整数,你可能会得到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) . 所以你想把这个-10转换成10.你应该返回abs(tzInfo.Bias / 60)

One error is in your _T call. 你的_T电话中有一个错误。 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. 但是,通过使用错误的printf说明符,您将导致它作为整数从堆栈弹出。 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 . 正如其他人所指出的那样,当你转换为unsigned int ,实际上是在告诉编译器使用int的位模式并将其用作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. 如果您的计算机使用二进制补码 ,那么您的数字将被解释为UINT_MAX-10而不是您预期的10 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 . 当您使用%d格式说明符时,编译器将返回使用与int相同的位模式而不是unsigned int This is why you are still getting -10 . 这就是为什么你仍然得到-10

If you want the absolute value of an integer, you should try to get it mathematically instead of using a cast. 如果你想要一个整数的绝对值,你应该尝试以数学方式得到它而不是使用强制转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM