0

I have doubt about the multiplication of unsigned long long int in the G++ 5.4.0 compiler. Why the 1st snippet overflows even though the variable n is unsigned long long int type? Why the 'LL' behind 8 works?

Please give me some keyword that I can dig more about this problem. Thanks!

//WRONG RESULT: 1549413176
int main(){
  unsigned long long int n = 1804289383;
  std::cout<<n * 8 <<endl;
  return 0;
}

//Correct :14434315064
int main(){
  unsigned long long int n = 1804289383;
  std::cout<<n * 8LL <<endl;
  return 0;
}

compiler: g++ 5.4.0

Shirley Feng
  • 143
  • 1
  • 10
  • 1
    Unable to reproduce with gcc 6.2 – Sam Varshavchik Nov 10 '16 at 01:57
  • And this seems to be dependent on the compilers because the 1st snippet works fine in my VS2015. Please correct me if my observation is wrong – Shirley Feng Nov 10 '16 at 01:57
  • Cannot reproduce on gcc 5.3: http://melpon.org/wandbox/permlink/OfuG6PbOpe3Wwmo5 Could you try to put a testcase in an online compiler like wandbox, coliru or ideone? – krzaq Nov 10 '16 at 01:58
  • @krzaq Thank you for share the wandbox website to me. It's an awesome website!! Actually, I guess this is probably an issue in g++ 5.4.0. But unfortunately, there's no g++ 5.4.0 on wandbox. But the problem can be reproduced in the following online coding website which is said to use g++ 5.4.0 as their compiler for C++ language. https://leetcode.com/problems/arranging-coins/ – Shirley Feng Nov 10 '16 at 02:12
  • https://leetcode.com/faq/ @krzaq – Shirley Feng Nov 10 '16 at 02:14
  • It could also be that 8 is an int and the result is being casted to an int. – Nick Pavini Nov 10 '16 at 03:49
  • Unable to reproduce on GCC 4.8.5. I agree with Nick- you probably accidentally casted the result to an int (which gives the stated wrong result). – David Nov 10 '16 at 03:54

0 Answers0