This is probably a really silly question to experienced C++ developers, but what is the purpose of casting a -1 to uint32? I am translating a program from C++ to C# and there are many occasions when I see something like this:
static const uint32 AllTypes = static_cast<uint32>(-1);
What exactly does this do? How can the same be accomplished in C#?
On systems using two's complement, casting -1
to unsigned gives the highest value an unsigned number can represent.
In C# you can use unchecked((UInt32)-1)
or better: UInt32.MaxValue
. This is well defined behavior, and works on all CPU architectures.
According to the thread rve linked , casting -1
to unsigned results in all bits being set on all architectures in C++.
如何在C#中完成同样的事情
uint AllTypes = uint.MaxValue;
I guess it's used to have all bits to 1. Useful when we use tagged data. Probably each elementary type it's given a bit, and 'complex' types (arrays, for instance) get their own.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.