Is long returned by n.longValue() the same as long (signed) in C++?
As you can read on the link you provided, there are really only two types of long in C++, signed and unsigned. The C++ standards guarantees a long to be at least 32-bit. So the range of a (signed) long is from -2147483648 to 2147483647.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
I need to convert the following Java code to C++ and have trouble figuring out how to represent Java Long in C++.
Your problem is that you're trying to equate Java to C++ and variable types.
In Java, there is a standard size for each type, not so in C++. A "long" in C++ could be any size -- it is up to the compiler as to what size any simple data type is.
The only requirement for C++ is that long is at least as big as an "int". The same rules apply to the other simple types in C++ -- there is no concrete size (except for char, which must be 1 byte). In C++, simple type sizes are determined by what type must be at least as big as another type, etc.
So you tell us -- what is a Java long? What size is it? Whatever it is, you have to make sure that your compiler has an integer type of that size.
http://java.sun.com/docs/books/tutor...datatypes.html
long: The long data type is a 64-bit signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). Use this data type when you need a range of values wider than those provided by int.
However, the standard doesn't say how many bits a byte should be. Only that it should hold at least the values 0 to 255 (ie At least 8 bits).
It also states that for chars, all possible binary values of the char be a valid value. If you read a RAW byte, you know it is a valid value. This is usually not the case for floating type numbers, for example. Certain binary values simply represent nothing in floating point, not even NAN.
http://java.sun.com/docs/books/tutor...datatypes.html
long: The long data type is a 64-bit signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive). Use this data type when you need a range of values wider than those provided by int.
So in this case.. use long long?
long long is not (yet) part of standard C++. It is standard C99 though. All of the compilers I have seen support it though, you my say is you are 99% safe using it.
However, you have no guarantees on the size of your long long. It is at least 64 bits, but could be 128.
[QUOTE=ictoan;1894891]
I need to convert the following Java code to C++ and have trouble figuring out how to represent Java Long in C++.
C++ doesn't specify the exact byte size of primitives so what you do will be implementation dependent. The easiest way to handle this is to use a define and then change it to the 64 bit signed type supported by the specific compiler. For example If you use MS Visual C++ you can study this table,
99%, since I don't have a copy of the standard handy.
Originally Posted by Paul McKenzie
The only requirement for C++ is that long is at least as big as an "int".
I thought the rule was:
1 = sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)
Moreover, a char is at least 8-bit and a long is at least 32-bit.
Indeed, the standard doesn't talk about bits, but guarantees a valid range instead. However, assuming a computer works with bits (which most of them do) that translates into these requirements.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Its always funny how simple questions can spark such debates.
Anyway the standard does not dictate sizes, there are some conventions which are very common (but not universal).
Code:
char 1 byte
short 2 bytes
int 4 bytes (or 2 bytes when compiling 16 bit code).
long 4 bytes
Since 64 bit integers have been a problem for a while its no surprise that some compilers allow you to use "long long" for this. But this is not officially a type before the most recent standard (which I believe is still in draft) and not all compilers support it.
A very common addition to the standard are the types:
However, the standard doesn't say how many bits a byte should be. Only that it should hold at least the values 0 to 255 (ie At least 8 bits).
I worked with a C compiler for TMS320C50 that had 16 bit char. The processor was unable to manipulate anything less than 16 bits at a time.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
Bookmarks