What is C++ equipvalent of Java Long?
Hello!
I need to convert the following Java code to C++ and have trouble figuring out how to represent Java Long in C++.
Here's the Java code:
String function(Number n){
long l = n.longValue();
return Long.toString(l, 16);
}
This is what I'm thinking of doing in C++
string function(long l){
stringstream ss;
ss << l;
return ss.str();
}
Would that work?
In C++, there are different types of longs...
http://home.att.net/~jackklein/c/inttypes.html#long
Is long returned by n.longValue() the same as long (signed) in C++?
Re: What is C++ equipvalent of Java Long?
Quote:
Originally Posted by
ictoan
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.
Re: What is C++ equipvalent of Java Long?
Quote:
Originally Posted by
ictoan
Hello!
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.
Regards,
Paul McKenzie
Re: What is C++ equipvalent of Java Long?
Quote:
Originally Posted by
D_Drmmr
The C++ standards guarantees a long to be at least 32-bit.
Are you sure about that?
Regards,
Paul McKenzie
Re: What is C++ equipvalent of Java Long?
Quote:
Originally Posted by Paul McKenzie
Are you sure about that?
Yes, by virtue of the minimum range required.
Re: What is C++ equipvalent of Java Long?
Hi guys,
Thanks for the quick reply.
This is what Java doc says...
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?
Re: What is C++ equipvalent of Java Long?
Quote:
Originally Posted by
Paul McKenzie
(except for char, which must be 1 byte).
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.
Re: What is C++ equipvalent of Java Long?
Quote:
Originally Posted by
ictoan
Hi guys,
Thanks for the quick reply.
This is what Java doc says...
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.
Re: What is C++ equipvalent of Java Long?
[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,
http://msdn.microsoft.com/en-us/libr...tz(VS.80).aspx
and use,
#define MY_LONG __int64 // VC++ specific
Re: What is C++ equipvalent of Java Long?
Quote:
Originally Posted by
Paul McKenzie
Are you sure about that?
99%, since I don't have a copy of the standard handy.
Quote:
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.
Re: What is C++ equipvalent of Java Long?
Quote:
Originally Posted by
nuzzle
and use,
#define MY_LONG __int64 // VC++ specific
A somewhat more general approach would be to use,
#define MY_LONG long long // biggest chance of being a Java long
Then you put in this check somewhere at the beginning of your program,
if (sizeof(MY_LONG) < 8) {
// long isn't long enougth
}
Re: What is C++ equipvalent of Java Long?
Quote:
Originally Posted by
nuzzle
A somewhat more general approach would be to use,
#define MY_LONG long long // biggest chance of being a Java long
Then you put in this check somewhere at the beginning of your program,
if (sizeof(MY_LONG) < 8) {
// long isn't long enougth
}
Better yet, do a static assert:
Code:
char dummy[ sizeof(MY_LONG) == 8 ];
Then the code will not compile if the long isn't exactly 8 bytes.
Regards,
Paul McKenzie
Re: What is C++ equipvalent of Java Long?
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:
Code:
__int8 1 byte
__int16 2 bytes
__int32 4 bytes
__int64 8 bytes
Most compilers support these.
Re: What is C++ equipvalent of Java Long?
Quote:
Originally Posted by
monarch_dodra
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.
Re: What is C++ equipvalent of Java Long?
Quote:
Originally Posted by
JohnW@Wessex
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.
Isn't that the definition of a byte though? The smallest element a processor can manipulate?
Since your processor cannot manipulate anything smaller than 16 bits => (byte size = 16) => char is 16 bits?
Re: What is C++ equipvalent of Java Long?
I had a thread about a month ago about which is more important, what a compiler does, or the standards. It was about half and half, if you're like me, you care more about the compiler than standards. I always add these few lines of code at the beginning of any code that I write
Code:
ASSERT(sizeof(char) == 1, "Replace chars with 8 bit primitive");
ASSERT(sizeof(short) == 2, "Replace shorts with 16-bit primitive");
ASSERT(sizeof(int) == 4, "Replace int with 32-bit primitive");
ASSERT(sizeof(long) == 4, "Replace long with 32-bit primitive");
ASSERT(sizeof(long long) == 8, "Replace long long with 64-bit primitive");
ASSERT(sizeof(float) == 4, "Replace float with 32-bit primitive");
ASSERT(sizeof(double) == 8, "Replace double with 64-bit primitive");
ASSERT(sizeof(long double) == 12, "Replace long double with 96-bit primitive");
That way if I port my code to a platform or compiler that has a difference, I can do a quick bulk replace of the primitive that doesn't match up to what I expect :)
I have a huge header file full of those things for large projects, especially since I use cross-platform libraries that may be built differently, specifically wxWidgets.
Re: What is C++ equipvalent of Java Long?
Quote:
Originally Posted by
monarch_dodra
Isn't that the definition of a byte though? The smallest element a processor can manipulate?
Byte is one of many computer terms with a modified meaning.
In the early days of computing, thats correct. And it was common to find such things as 7 bit bytes. In modern times anyone refering to a byte as anything other than an octect is asking to be misunderstood. :D
Re: What is C++ equipvalent of Java Long?
Quote:
Originally Posted by
ictoan
Hello!
Is long returned by n.longValue() the same as long (signed) in C++?
Documents on value returned after use of Number class's longValue is required
On 32bit systems, long long is defined as a size of 8 bytes
Re: What is C++ equipvalent of Java Long?
Quote:
Originally Posted by
ninja9578
I had a thread about a month ago about which is more important, what a compiler does, or the standards. It was about half and half, if you're like me, you care more about the compiler than standards. I always add these few lines of code at the beginning of any code that I write
Code:
ASSERT(sizeof(char) == 1, "Replace chars with 8 bit primitive");
ASSERT(sizeof(short) == 2, "Replace shorts with 16-bit primitive");
ASSERT(sizeof(int) == 4, "Replace int with 32-bit primitive");
ASSERT(sizeof(long) == 4, "Replace long with 32-bit primitive");
ASSERT(sizeof(long long) == 8, "Replace long long with 64-bit primitive");
ASSERT(sizeof(float) == 4, "Replace float with 32-bit primitive");
ASSERT(sizeof(double) == 8, "Replace double with 64-bit primitive");
ASSERT(sizeof(long double) == 12, "Replace long double with 96-bit primitive");
That way if I port my code to a platform or compiler that has a difference, I can do a quick bulk replace of the primitive that doesn't match up to what I expect :)
I have a huge header file full of those things for large projects, especially since I use cross-platform libraries that may be built differently, specifically wxWidgets.
It can also be nice to have your own number classes, and if implemented with SIMD and memory aligned they can be much faster than the built in types. Then you know exactly what they are doing, and know exactly how they will round off without worrying what order you declared them, or have very large values etc.