64 bit integers: C++, example

64 bit integers: C++, example

You may need to handle very large numbers in the C language. An unsigned number of 32 bits cannot exceed a particular value. In order to handle larger integers, a separate data type for handling 64 bit integers can be used in the C programming language. This article will show you some examples of 64 bit integers in the C programming language.

Introduction

The long long data type can handle large integers by allowing the compiler to store the number in two registers instead of one. To print a long long data type, the formatting for display is different from other data types. The long long data type makes handling 64 bit integers easy.

In C language, an unsigned number over 32 bits cannot exceed the value of 4,294,967,295.

You may find you are required to handle larger numbers and for this you need these numbers to be coded in 64-bit.

However, this is not handled in the same way as an ordinary integer. They must be defined differently.

Unsigned 64-bit integer

Type: unsigned long long. Formatting for display: % llu. Suffix to define a constant: ULL.

Example

//Assign the value in a 4294967296     
unsigned long long a= 4294967296ULL;     
//Show the value     
printf ( "% llu", a);

Signed 64-bit integer

Type: long long. Formatting for display: % lld. Suffix to define a constant: LL.

Example

//Assign the value in a 4294967296     
long long a= 4294967296LL;     
// Show the value     
printf ( "% lld", a);

Using an unusual suffix to define a constant value.

Unsigned long long a = 4294967296.

Your compiler will prompt you that this number is too large for the "long" type. This concept is directly related to the architecture of 32-bit processors. A 32-bit processor is limited and your default compiler will try to fit the numbers in one registry. But with a suffix such as LL and ULL, your compiler will store your number on 2 registers, i.e. in 64-bit, therefore allowing for considerably larger numbers.

Any more programming questions? Check out our forum!