What is C++ equipvalent of Java Long?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

Thread: What is C++ equipvalent of Java Long?

  1. #1
    Join Date
    Oct 2009
    Posts
    3

    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++?

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,013

    Re: What is C++ equipvalent of Java Long?

    Quote Originally Posted by ictoan View Post
    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++?
    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

  3. #3
    Join Date
    Apr 1999
    Posts
    27,431

    Re: What is C++ equipvalent of Java Long?

    Quote Originally Posted by ictoan View Post
    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

  4. #4
    Join Date
    Apr 1999
    Posts
    27,431

    Re: What is C++ equipvalent of Java Long?

    Quote Originally Posted by D_Drmmr View Post
    The C++ standards guarantees a long to be at least 32-bit.
    Are you sure about that?

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,294

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Oct 2009
    Posts
    3

    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?

  7. #7
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,297

    Re: What is C++ equipvalent of Java Long?

    Quote Originally Posted by Paul McKenzie View Post
    (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.

  8. #8
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,297

    Re: What is C++ equipvalent of Java Long?

    Quote Originally Posted by ictoan View Post
    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.

  9. #9
    Join Date
    May 2009
    Posts
    2,413

    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

  10. #10
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,013

    Re: What is C++ equipvalent of Java Long?

    Quote Originally Posted by Paul McKenzie View Post
    Are you sure about that?
    99%, since I don't have a copy of the standard handy.
    Quote Originally Posted by Paul McKenzie View Post
    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

  11. #11
    Join Date
    May 2009
    Posts
    2,413

    Re: What is C++ equipvalent of Java Long?

    Quote Originally Posted by nuzzle View Post
    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
    }

  12. #12
    Join Date
    Apr 1999
    Posts
    27,431

    Re: What is C++ equipvalent of Java Long?

    Quote Originally Posted by nuzzle View Post
    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

  13. #13
    Join Date
    Nov 2007
    Location
    Birmingham, England
    Posts
    157

    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 08:41 PM.
    Signature
    Please use: [ code ][/ code ] tags and reasonably correct tabbing. They really help us read your code
    End Signature

  14. #14
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,725

    Re: What is C++ equipvalent of Java Long?

    Quote Originally Posted by monarch_dodra View Post
    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

  15. #15
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,297

    Re: What is C++ equipvalent of Java Long?

    Quote Originally Posted by JohnW@Wessex View Post
    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?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center