-
November 12th, 2009, 10:57 AM
#1
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++?
-
November 12th, 2009, 11:08 AM
#2
Re: What is C++ equipvalent of Java Long?
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.
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
-
November 12th, 2009, 11:09 AM
#3
Re: What is C++ equipvalent of Java Long?
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
-
November 12th, 2009, 11:10 AM
#4
Re: What is C++ equipvalent of Java Long?
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
-
November 12th, 2009, 11:13 AM
#5
Re: What is C++ equipvalent of Java Long?
Originally Posted by Paul McKenzie
Are you sure about that?
Yes, by virtue of the minimum range required.
-
November 12th, 2009, 11:22 AM
#6
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?
-
November 12th, 2009, 11:30 AM
#7
Re: What is C++ equipvalent of Java Long?
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.
-
November 12th, 2009, 11:33 AM
#8
Re: What is C++ equipvalent of Java Long?
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.
-
November 12th, 2009, 11:52 AM
#9
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
-
November 12th, 2009, 01:01 PM
#10
Re: What is C++ equipvalent of Java Long?
Originally Posted by Paul McKenzie
Are you sure about that?
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
-
November 12th, 2009, 01:45 PM
#11
Re: What is C++ equipvalent of Java Long?
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
}
-
November 12th, 2009, 02:48 PM
#12
Re: What is C++ equipvalent of Java Long?
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
-
November 12th, 2009, 02:50 PM
#13
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.
Last edited by couling; November 12th, 2009 at 09:41 PM.
Signature
Please use: [ code ][/ code ] tags and reasonably correct tabbing. They really help us read your code
End Signature
-
November 13th, 2009, 05:08 AM
#14
Re: What is C++ equipvalent of Java Long?
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.
"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
-
November 13th, 2009, 07:18 AM
#15
Re: What is C++ equipvalent of Java Long?
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?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|