Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I represent -9223372036854775808 in C/C++?

-9223372036854775808 is the most negative int64_t. the problem is that if I use -9223372036854775808L or -9223372036854775808LL then I get the warning "integer literal is too large to be represented in a signed integer type, interpreting as unsigned". The issue is that the - is being treated separately and positive 9223372036854775808 is indeed too big for int64_t. If I try -9223372036854775808UL then I get the warning "unary minus operator applied to unsigned type, result still unsigned". How do I properly encode this number?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The issue you’re facing is due to the fact that the literal -9223372036854775808 is being treated as a positive number because it’s outside the range of a signed 64-bit integer. To properly represent this number in your code, you can use the LL suffix to indicate a long long integer, and also use the unary minus operator to make it negative. Here’s an example:

#include <stdint.h>
#include<iostream>
using namespace std;
int main() {
    int64_t myInt = -9223372036854775807LL - 1LL;
    cout<<myInt; // prints -9223372036854775808
    return 0;
}

By subtracting 1 from -9223372036854775807LL, you effectively obtain the minimum representable value of int64_t. The use of LL ensures that the literal is treated as a long long integer. This approach avoids the issues with interpretation as an unsigned type or exceeding the range of signed integers.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading