C++ 操作 LARGE_INTEGERS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1395361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:50:46  来源:igfitidea点击:

manipulating LARGE_INTEGERS

c++int64

提问by Mick

I am converting some code from C to C++ in MS dev studio under win32. In the old code I was doing some high speed timings using QueryPerformanceCounter() and did a few manipulations on the __int64 values obtained, in particular a minus and a divide. But now under C++ I am forced to use LARGE_INTEGER because that's what QueryPerformanceCounter() returns. But now on the lines where I try and do some simple maths on the values I get an error:

我正在 win32 下的 MS dev studio 中将一些代码从 C 转换为 C++。在旧代码中,我使用 QueryPerformanceCounter() 进行了一些高速计时,并对获得的 __int64 值进行了一些操作,特别是减号和除法。但现在在 C++ 下,我被迫使用 LARGE_INTEGER,因为这是 QueryPerformanceCounter() 返回的内容。但是现在在我尝试对值进行一些简单的数学运算时,我得到了一个错误:

error C2676: binary '-' : 'LARGE_INTEGER' does not define this operator or a conversion to a type acceptable to the predefined operator

错误 C2676:二进制“-”:“LARGE_INTEGER”未定义此运算符或转换为预定义运算符可接受的类型

I tried to cast the variables to __int64 but then get:

我试图将变量强制转换为 __int64 但随后得到:

error C2440: 'type cast' : cannot convert from 'LARGE_INTEGER' to '__int64'

错误 C2440:“类型转换”:无法从“LARGE_INTEGER”转换为“__int64”

How do I resolve this?

我该如何解决?

Thanks,

谢谢,

回答by Jorge Ferreira

LARGE_INTEGER is a union of a 64-bit integer and a pair of 32-bit integers. If you want to perform 64-bit arithmetic on one you need to select the 64-bit int from inside the union.

LARGE_INTEGER 是一个 64 位整数和一对 32 位整数的联合。如果要对一个执行 64 位算术运算,则需要从联合内部选择 64 位 int。

LARGE_INTEGER a = { 0 };
LARGE_INTEGER b = { 0 };

__int64 c = a.QuadPart - b.QuadPart;

回答by Isaac

Here it is:

这里是:

LARGE_INTEGER x,y;
///
//Some codes...
///

__int64 diff = x.QuadPart - y.QuadPart

Because QuadPartis defined as a LONGLONG, that same as __int64.

因为QuadPart被定义为LONGLONG,与__int64相同。

回答by Cat Plus Plus

LARGE_INTEGERis a union, documented here. You probably want a QuadPartmember.

LARGE_INTEGER是一个联合,记录在此处。你可能想要一个QuadPart会员。

回答by jcopenha

LARGE_INTEGERis a union, you can still use .QuadPart if you want to work on the 64-bit value.

LARGE_INTEGER是一个联合,如果您想处理 64 位值,您仍然可以使用 .QuadPart。

回答by Mehdi

As the Documentationsays in the Remarkssection :

正如文档备注部分所说:

The LARGE_INTEGERstructure is actually a union. If your compiler has built-in support for 64-bit integers, use the QuadPart member to store the 64-bit integer. Otherwise, use the LowPart and HighPart members to store the 64-bit integer.

LARGE_INTEGER结构实际上是一个联合。如果您的编译器内置支持 64 位整数,请使用 QuadPart 成员来存储 64 位整数。否则,使用 LowPart 和 HighPart 成员来存储 64 位整数。

So if your compiler supports 64 bit integers use quadPart like this :

因此,如果您的编译器支持 64 位整数,请像这样使用 quadPart:

LARGE_INTEGER a, b;
__int64 diff = a.QuadPart - b.QuadPart

回答by M.Boelter

In addition to the answers, if you are looking to construct a LARGE_INTEGER with a value other than zero, you can assign the low and high parts separately. LowPart is first as defined in the union, and the only highPart is signed.

除了答案之外,如果您希望构造一个非零值的 LARGE_INTEGER,您可以分别分配低部分和高部分。LowPart 是联合中定义的第一个,并且唯一的 highPart 是有符号的。

LARGE_INTEGER li = {0x01234567, -1};