Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function of type unsigned int returns negative number

Wow I thought I knew my C++ but this is weird

This function returns an unsigned int so I thought that means I will never get a negative number returned right?

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). Therefore the GetTimeZoneInformation correctly determines I am -10.

BUT my function returns an unsigned int so shouldn't it return 10 not -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");
like image 506
user593747 Avatar asked Mar 02 '26 10:03

user593747


2 Answers

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.

like image 170
cnicutar Avatar answered Mar 04 '26 23:03

cnicutar


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).

like image 40
Mysticial Avatar answered Mar 04 '26 23:03

Mysticial



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!