hey anyone know what the largest value that can be stored in an integer variable is? likewise is there a double / long type of deal that isnt of float type? thanks

Dani AI

Generated

, two quick clarifications that build on @Siersan and . The size of int is implementation-defined, but it is at least 16 bits. On the common 32-bit int implementations, the max for signed int is 2,147,483,647 and for unsigned int it is 4,294,967,295. Doubles are floating-point and only keep integer precision up to 2^53-1, so they are not a safe substitute for big integers. A 20-hex-digit value is 80 bits, so it will not fit in int, long, or even 64-bit types; you need either manual big-integer arithmetic or a big-int library. You can confirm actual limits on your compiler with std::numeric_limits and the fixed-width types like std::uint32_t/std::uint64_t. See integer types and std::numeric_limits.

If you want to stay library-free (as you suggested) and keep it simple for an intro assignment, add hex strings digit-by-digit from right to left with a carry. Here is a compact pattern you can adapt:

#include <string>
#include <algorithm>

std::string add_hex(std::string a, std::string b) {
    auto hv = [](char c){ return (c>='0'&&c<='9')? c-'0' :
                                 (c>='a'&&c<='f')? 10+(c-'a') :
                                 (c>='A'&&c<='F')? 10+(c-'A') : 0; };
    std::string out; int i=(int)a.size()-1, j=(int)b.size()-1, carry=0;
    while (i>=0 || j>=0 || carry) {
        int s = (i>=0? hv(a[i--]) : 0) + (j>=0? hv(b[j--]) : 0) + carry;
        out.push_back("0123456789ABCDEF"[s & 0xF]); carry = s >> 4;
    }
    std::reverse(out.begin(), out.end()); return out;
}

Notes:

  • std::uint64_t handles up to 16 hex digits exactly; your 20-digit case still needs the string approach or chunking into 32-bit blocks. On Linux/macOS long is typically 64-bit, but on Windows it is 32-bit; do not rely on long for size portability. See data models overview: 64-bit data models.

Recommended Answers

All 9 Replies

Member Avatar for Member #27895

hey anyone know what the largest value that can be stored in an integer variable is?

That is up to your implementation, but you can assume that the size of an int is the natural word size of your system. A 32 bit system will most likely have 32 bit integers, so the largest value is 4,294,967,296. You can only assume the range of -32,767 to 32,767 for int if you want your code to be portable though. If you want at least a 32 bit data type then long int is the way to go.

likewise is there a double / long type of deal that isnt of float type?

I don't understand what you are asking.

There is a file called LIMITS.H that has all the values you are looking for.

well if you have a float value that is too large for a normal float you can us a double to give it twice the normal memory usage...

i was wondering if there was an int type of variable to do the same.

i am basically working with addition of two hexadecimal numbers each of which can be up to 20 characters long, when i add two of these i am storing it in an int variable, and i am thinking the reason i am having problems with the output is becuase the answer it returns is too large to be stored in the int...

Member Avatar for Member #27895

A 20 character long hexadecimal value would exceed any of C++'s types. Try an arbitrary precision library such as .

well the project is for a intro level c++ class , so it seems like the prof wouldnt require us to do soemthin that required libraries other than the ones in code warrior to start with. im thinkin im gonna have to store the value as a character array and only convert it to actual numbers when i need to manipulate them, that way i dont have to deal w/ the whole thing as one huge number, jus go one slot at a time thru the array. thanks alot for speedy replies by the way

hey anyone know what the largest value that can be stored in an integer variable is? likewise is there a double / long type of deal that isnt of float type? thanks

#include <iostream>
#include <limits>

int main(void)
{
   std::cout << std::numeric_limits<int>::max()    << std::endl;
   std::cout << std::numeric_limits<double>::max() << std::endl;
   return 0 ;
}

/* my output
2147483647
1.79769e+308
*/

im thinkin im gonna have to store the value as a character array and only convert it to actual numbers when i need to manipulate them

Exactly. I've never found the need to do something like this with binary numbers, but before co-processors I coded this kind of thing on the Z80 to give me 128 digit precision in BCD (Binary Coded Decimals).

the largest value is 4,294,967,296.

The largest int ist 2^31 = 2147483647.
One bit is used to store the sign.

well the project is for a intro level c++ class , so it seems like the prof wouldnt require us to do soemthin that required libraries other than the ones in code warrior to start with. im thinkin im gonna have to store the value as a character array and only convert it to actual numbers when i need to manipulate them, that way i dont have to deal w/ the whole thing as one huge number, jus go one slot at a time thru the array. thanks alot for speedy replies by the way

Yep, that's why we make assignments like that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.