2

There is the ULARGE_INTEGER union for compilers that don't support 64 bit arithmetic.

What would happen in the following code if the addition on the last line overflows?

ULARGE_INTEGER u;
u.LowPart = ft->dwLowDateTime;
u.HighPart = ft->dwHighDateTime;
u.LowPart += 10000; //what if overflow?

Related question: What is the point of the ULARGE_INTEGER union?

11
  • 1
    unsigned integers are guaranteed to wrap around. Commented Aug 7, 2017 at 5:34
  • @HenriMenke which means the carry would not be added to the HighPart? Commented Aug 7, 2017 at 5:44
  • 1
    Have you tried that? In my opinion only LowPart will overflow (wrap), it won't affect HighPart. Commented Aug 7, 2017 at 5:45
  • 1
    @H Walters What would that prove? Commented Aug 7, 2017 at 7:16
  • 2
    @Roland I've added a couple of duplicates. In sure there are many more. This isn't really related to the struct. You are simply performing unsigned wrap around on a single variable. The compiler just sees one variable. Commented Aug 7, 2017 at 7:18

1 Answer 1

8

ULARGE_INTEGER is composed of two unsigned values. Unsigned values are guaranteed to wrap round, so in some sense they can't "overflow".

If wrap round does occur, u.LowPart will end up being less than 10,000. What you probably want is:

u.LowPart += 10000;
if (u.LowPart < 10000) u.HighPart++;

... but what compiler still doesn't support 64-bit integers these days? They have been required by the C++ standard since 2011, and the C standard since 1999. So what you really want is:

u.QuadPart += 10000;  // Forget about legacy compilers that doen't support 64 bits.
Sign up to request clarification or add additional context in comments.

12 Comments

Understood. I'm just trying to understand how ULARGE_INTEGER is to be used if you only have 32 bit arithmetic. So you have to check for overflow and increment the HighPart as you did in your code fragment?
@Roland : Yup. Similarly if you are trying to emulate 1024 bit arithmetic with 64 bit values.
@MatteoItalia I got it. uli.QuadPart += 10000; will only work if your compiler supports 64 bit arithmetic, otherwise you have to check for overflow as in Martin Bonner's code.
I still work with a C cross compiler on a 16 bit microprocessor, and I'm fine.
Without long long (64) , and I'm fine. ANSI X3.159-1989 standard [ANSI]
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.