uint64

Is Shifting more than 32 bits of a uint64_t integer on an x86 machine Undefined Behavior?

送分小仙女□ 提交于 2019-12-17 19:39:00
问题 Learning the hard way, I tried to left shift a long long and uint64_t to more than 32 bits on an x86 machine resulted 0 . I vaguely remember to have read somewhere than on a 32 bit machine shift operators only work on the first 32 bits but cannot recollect the source. I would like to know is if Shifting more than 32 bits of a uint64_t integer on an x86 machine is an Undefined Behavior? 回答1: The standard says (6.5.7 in n1570): 3 The integer promotions are performed on each of the operands. The

Decimal.ToUInt64 "Value was either too large or too small for a UInt64

此生再无相见时 提交于 2019-12-13 09:44:22
问题 I am doing: - Decimal production = 0; Decimal expense = 5000; Decimal.ToUInt64(production - expense); But it throws exception with the following error message. "Value was either too large or too small for a UInt64" Can someone give me a workaround for this. Thanks! Edit In any case, I want the result as a positive number. 回答1: Problem: -5000m is a negative number, which is outside the range of UInt64 (an unsigned type). Solution: use Int64 instead of UInt64 if you want to cope with negative

c++ string (int) + string (int) [duplicate]

一曲冷凌霜 提交于 2019-12-13 02:25:37
问题 This question already has answers here : How to implement big int in C++ (13 answers) Closed 6 years ago . I have 2 strings, both contain only numbers. Those numbers are bigger than max of uint64_t . How can I still add these 2 numbers and then convert the result to string? 回答1: Well, you can either use a bigger datatype (for example a library that deals with large integers), or you can quickly knock up your own. I would suggest that if this is a one off, you do long addition exactly like you

access violation casting to void* and back

痞子三分冷 提交于 2019-12-12 05:39:43
问题 I get an access violation reading location when I try the following. What am I doing wrong? uint64_t hInt = 2901924954136; void* hPoint = reinterpret_cast<void*>(hInt); uint64_t hIntBack = *static_cast<uint64_t*>(hPoint); //get access violation here 回答1: I am guessing you meant to store the address of hInt in hPoint , not the value of hInt . uint64_t hInt = 2901924954136; void* hPoint = reinterpret_cast<void*>(&hInt); // ^ addressof operator 回答2: What you do is the following: cast an integer

Converting (u)int64_t to NSNumbers

血红的双手。 提交于 2019-12-10 03:26:14
问题 So essentially my question is this, I am creating an NSMutableDictionary using uint64_t objects as the key. Is there any better way to create them than doing this? uint64_t bob=7; NSNumber *bobsNumber; #if __LP64__ || TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64 bobsNumber=[NSNumber numberWithUnsignedLong:bob]; #else bobsNumber=[NSNumber numberWithUnsignedLongLong:bob]; #endif This would work as long as you didn't include it in a binary file/sockets/NSData

Convert uint64_t to std::string

你说的曾经没有我的故事 提交于 2019-12-09 08:59:01
问题 How can I transfer uint64_t value to std::string? I need to construct the std::string containing this value For example something like this: void genString(uint64_t val) { std::string str; //.....some code for str str+=(unsigned int)val;//???? } Thank you 回答1: use either boost::lexical_cast or std::ostringstream e.g.: str += boost::lexical_cast<std::string>(val); or std::ostringstream o; o << val; str += o.str(); 回答2: In C++ 11 you may just use: std::to_string() it's defined in header http:/

UInt64 and “The operation overflows at compile time in checked mode” - CS0220

依然范特西╮ 提交于 2019-12-08 15:02:24
问题 This feels like a stupid question, but I can't seem to see the answer. I have an UInt64, which is supposed to have a max value of UInt64.MaxValue 18446744073709551615 However, when I try to assign a modest-sized number, I get this overflow error of "The operation overflows at compile time in checked mode". If I wrap it in an "unchecked" block then it compiles, and runs as if this variable is zero: UInt64 value1 = 1073741824 * 8; // Compile error CS0220 UInt64 value2 = 8589934592; // Actual

std::atoll with VC++

▼魔方 西西 提交于 2019-12-06 21:44:33
问题 I have been using std::atoll from cstdlib to convert a string to an int64_t with gcc. That function does not seem to be available on the Windows toolchain (using Visual Studio Express 2010). What is the best alternative? I am also interested in converting strings to uint64_t . Integer definitions taken from cstdint . 回答1: MSVC have _atoi64 and similar functions, see here For unsigned 64 bit types, see _strtoui64 回答2: use stringstreams ( <sstream> ) std::string numStr = "12344444423223"; std:

Multiplying __int64's

有些话、适合烂在心里 提交于 2019-12-05 13:22:41
Can someone explain to me (in detail) how to multiply two __int64 objs and check if the result will fit in __int64. Note: Do not use any compiler or processor dependent routines. not assuming a and b are positive: __int64 a,b; //... __int64 tmp_result = abs(a) * abs(b) ; if ( ( a && b ) && ( ( tmp_result < abs(a) || tmp_result < abs(b) ) || ( tmp_result / abs(a) != abs(b)) || ( a == TYPE_MIN && b != 1) || ( b == TYPE_MIN && a != 1) ) ) std::cout << "overflow"; __int64 result = a * b; EDIT: Adding corner cases to code. EDIT: In my opinion just ( a && a * b / a != b) is enough. 来源: https:/

Why does an uint64_t needs more memory than 2 uint32_t's when used in a class? And how to prevent this?

余生颓废 提交于 2019-12-05 09:33:52
I have made the following code as an example. #include <iostream> struct class1 { uint8_t a; uint8_t b; uint16_t c; uint32_t d; uint32_t e; uint32_t f; uint32_t g; }; struct class2 { uint8_t a; uint8_t b; uint16_t c; uint32_t d; uint32_t e; uint64_t f; }; int main(){ std::cout << sizeof(class1) << std::endl; std::cout << sizeof(class2) << std::endl; std::cout << sizeof(uint64_t) << std::endl; std::cout << sizeof(uint32_t) << std::endl; } prints 20 24 8 4 So it's fairly simple to see that one uint64_t is as large as two uint32_t's, Why would class 2 have 4 extra bytes, if they are the same