long-long

-9'223'372'036'854'775'808LL is unsigned

时间秒杀一切 提交于 2021-02-04 05:56:50
问题 Since C++20 two's complement representation is the only representation allowed by the standard, with the guaranteed range from -2 N-1 to +2 N-1 -1. So for a 64-bit signed integer type the range goes from -9'223'372'036'854'775'808 to 9'223'372'036'854'775'807 . However, this code does not compile on Visual Studio (and neither on gcc) int main() { long long x{-9'223'372'036'854'775'808LL}; // error C4146: unary minus operator applied to unsigned type, result still unsigned // error C2397:

-9'223'372'036'854'775'808LL is unsigned

╄→尐↘猪︶ㄣ 提交于 2021-02-04 05:56:19
问题 Since C++20 two's complement representation is the only representation allowed by the standard, with the guaranteed range from -2 N-1 to +2 N-1 -1. So for a 64-bit signed integer type the range goes from -9'223'372'036'854'775'808 to 9'223'372'036'854'775'807 . However, this code does not compile on Visual Studio (and neither on gcc) int main() { long long x{-9'223'372'036'854'775'808LL}; // error C4146: unary minus operator applied to unsigned type, result still unsigned // error C2397:

Why is “int” not working correctly with “j” but long long is working fine?

孤者浪人 提交于 2020-05-30 11:31:08
问题 This is my code with int j : void solve(){ unsigned long long n; cin>>n; unsigned long long sum = 0; int j = 1; for(int i=3;i<n+1;i+=2){ sum += ((4*i)-4)*(j); j++; } cout<<sum<<"\n"; } Input: 499993 Output: 6229295798864 but it is giving wrong output, and here is my code with long long j which is working fine: void solve(){ int n; cin>>n; unsigned long long sum = 0; long long j = 1; for(int i=3;i<n+1;i+=2){ sum += ((4*i)-4)*(j); j++; } cout<<sum<<"\n"; } Input: 499993 Output:

Why is “int” not working correctly with “j” but long long is working fine?

情到浓时终转凉″ 提交于 2020-05-30 11:29:57
问题 This is my code with int j : void solve(){ unsigned long long n; cin>>n; unsigned long long sum = 0; int j = 1; for(int i=3;i<n+1;i+=2){ sum += ((4*i)-4)*(j); j++; } cout<<sum<<"\n"; } Input: 499993 Output: 6229295798864 but it is giving wrong output, and here is my code with long long j which is working fine: void solve(){ int n; cin>>n; unsigned long long sum = 0; long long j = 1; for(int i=3;i<n+1;i+=2){ sum += ((4*i)-4)*(j); j++; } cout<<sum<<"\n"; } Input: 499993 Output:

How to store 64 digits integer in C?

一曲冷凌霜 提交于 2020-05-23 07:23:33
问题 I am dealing with 2 64 digits integers and need to multiple these two numbers. When I tried to store it into a long long int variable, I got the following compiling error: 1.c: In function ‘main’: 1.c:5:6: warning: integer constant is too large for its type. a = 1234567890123456789012345678901234567890123456789012345678901234; Can someone tell me how to store the integer in C? [edit] OP later implies a 64 decimal digit number. 回答1:

Should I use long long or int64_t for portable code?

喜夏-厌秋 提交于 2019-12-29 04:28:04
问题 I have an open-source codebase that is written in both C and C++. I'm looking for an integer type that is guaranteed to be at least 64 bits wide, which can be reliably compiled on most OS X (Intel, 64-bit) and Linux boxes with open-source C and C++ compilers, without too much extra work on the end user's part. Windows and 32-bit client support are not important at this time. I did some testing on OS X, and the latest GCC that ships with the developer tools does not support C+11 mode (and

Power for big numbers modulo m in C

孤人 提交于 2019-12-24 10:45:57
问题 I am using the following function to compute powers of big numbers modulo m, where m is any integer,i.e. (a^b)%m long long power(long long x, long long y, long long p) { long long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p while (y > 0) { // If y is odd, multiply x with result if (y & 1) res = (res*x) % p; // y must be even now y = y>>1; // y = y/2 x = (x*x) % p; } return res; } But, for some numbers even this function is not working. For example,

Troubling converting string to long long in C

纵然是瞬间 提交于 2019-12-19 17:36:34
问题 I'm having trouble getting the atoll function to properly set a long long value in c. Here is my example: #include <stdio.h> int main(void) { char s[30] = { "115" }; long long t = atoll(s); printf("Value is: %lld\n", t); return 0; } This prints: Value is: 0 This works though: printf("Value is: %lld\n", atoll(s)); What is going on here? 回答1: First, let's answer your question: #include <stdio.h> #include <stdlib.h> // THIS IS WHAT YOU ARE MISSING int main(void) { char s[30] = { "115" }; long

long long vs int multiplication

泪湿孤枕 提交于 2019-12-17 19:28:18
问题 Given the following snippet: #include <stdio.h> typedef signed long long int64; typedef signed int int32; typedef signed char int8; int main() { printf("%i\n", sizeof(int8)); printf("%i\n", sizeof(int32)); printf("%i\n", sizeof(int64)); int8 a = 100; int8 b = 100; int32 c = a * b; printf("%i\n", c); int32 d = 1000000000; int32 e = 1000000000; int64 f = d * e; printf("%I64d\n", f); } The output with MinGW GCC 3.4.5 is (-O0): 1 4 8 10000 -1486618624 The first multiplication is casted to an

Selecting 'long' versus 'long long' as 64-bit type under LP64/ILP64/LLP64 data models?

99封情书 提交于 2019-12-12 21:29:52
问题 I'm trying to understand the requirements for selecting a built-in 64-bit data type using either long or long long . I'm having trouble understanding the type equivalence and the alignment requirements of long versus long long . What is the best practice or criteria when selecting long versus long long as a 64-bit type under LP64/ILP64/LLP64 data models? Here are some related questions. They completely cover other topics, like sizeof(long) <= sizeof(long long) , but they don't quite have a