constant being treated as 32-bit when variable is 64-bit

This is a discussion on constant being treated as 32-bit when variable is 64-bit within the C Programming forums, part of the General Programming Boards category; I haven't been able to figure out what is going on and why this is happening. I'm using 64-bit integers ...

  1. #1
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582

    constant being treated as 32-bit when variable is 64-bit

    I haven't been able to figure out what is going on and why this is happening. I'm using 64-bit integers and whenever I use constants, I sometimes get completely unexpected results unless the constant is outside the range of a 32-bit value. I've tried typecasting it, but that didn't work. Is there a way to use constants and have them treated as a 64-bit integer when the value is from about 2.15 to 4.3 billion? Here's some examples on what I'm referring to:

    Code:
    __int64 Example; // declare a 64-bit integer
    
    ...
    
    Example = 13741152; // 13.7 million - works fine
    Example = -5120000000; // negative 5.1 billion - works fine
    Example = 68014747325468582; // 68 quadrillion - works fine
    Example = -4096000000; // negative 4.1 billion - problem
    Example = (__int64)-4096000000; // typecasting the constant - no good
    Example = -2048000000+-2048000000; // adding smaller numbers - no good
    The last 3 return a value of 198,967,296. Adding this to the positive counterpart of what I'm attempting to use adds up to 4,294,967,296, which is exactly how many possible values there are for a 32-bit integer, a sign that the compiler is treating the constant as a 32-bit value when the variable I'm trying to modify is a 64-bit value. Is there any way I can set the 64-bit variable to what I intend on using?
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Try adding LL and ULL to the end of your constants. These are type suffixes that force the compiler to use signed or unsigned long longs respectively.

    It's like F for floats.

  3. #3
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Adding the double L at the end worked.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    It is better that you use long long instead of the Microsoft proprietary __int64. And just a heads up, VS2010 is released. I urge you to upgrade.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Or use stdint.h... I'm guessing because of the std part that it a standardized header.

    VS2010 is buggy and slow. I don't use 2010 or 2008, but I prefer '08. IMO, dump them both, get Code Blocks are use GCC, it's better.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    Quote Originally Posted by Elysia View Post
    It is better that you use long long instead of the Microsoft proprietary __int64. And just a heads up, VS2010 is released. I urge you to upgrade.
    User Name is not wrong there. I would in fact discourage using VS2010 also. The Express version isn't really as free as it used to be, it's really just a trial version.

    The compiler regularly hangs indefinitely, and crashes with internal compiler errors on a 32-bit system if you use the /MP switch with more than a couple of projects. It also frequently complains that precompiled headers are corrupted and is notoriously bad at responding to updated dependent files. These small issues mean that even if it builds faster (and I'm not saying it does) then you have to build the main project I work on at work about 5 times before it succeeds, so it takes far longer overall.
    The IDE performs hideously slowly if you rotate your monitor 90 degrees. In VS2008 the speed was identical no matter how your monitor was oriented.
    The footprint for the install and all prerequisites is quite large, and the debugging experience is marginally better at best.

    The main redeeming quality is its reasonable support for C++0x features. At this point though, being an early adopter just doesn't seem to pay off well.
    Last edited by iMalc; 08-12-2010 at 02:18 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    You are not wrong, iMalc. But the fact that 08 doesn't support C++0x makes it rather useless if you ask me.
    But now that I look closely, I see that C++ isn't used at all. So 08 is fine, or GCC if you will.

    Quote Originally Posted by User Name: View Post
    Or use stdint.h... I'm guessing because of the std part that it a standardized header.

    VS2010 is buggy and slow. I don't use 2010 or 2008, but I prefer '08. IMO, dump them both, get Code Blocks are use GCC, it's better.
    Or dump the useless GCC which doesn't support wchar and get Visual Studio instead.
    Better is subjective. Recommending it is fine, but saying it's better is crossing the line.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    >>Or dump the useless GCC which doesn't support wchar and get Visual Studio instead.
    Are you sure that GCC doesn't support wchar?
    Anyway for C development, GCC supports C99...

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Pretty sure it doesn't work right. Never have gotten it to work.
    Yes, GCC's advantage of VS is C99, if you're a C programmer. So if you're doing C, GCC might be the compiler to recommend unless you want to stay in the stone age. If you use GCC, you'll just stay in the bronze age!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    You can't say more than it doesn't work for you?

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Sigh. No. Because frankly I don't like it. I have never gotten it to work and it annoys me.
    And I cannot--will not--be bothered to experiment and google and whatnot to make it work.
    Happy now?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Then it's better to stick with what works for you.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Exactly. And that is another good point - to me, Visual Studio is better. To ohers GCC is better. So there is no universal best - nothing is better than the other. It's subjective.
    So, User Name, be careful about what you say.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to save a 64 bit number with 2 32 bit ints?
    By homer_3 in forum C Programming
    Replies: 26
    Last Post: 04-07-2010, 12:47 PM
  2. c# OpenSubKey problem on 64 bit windows vs 32 bit.
    By Striph in forum C# Programming
    Replies: 3
    Last Post: 03-22-2010, 06:07 AM
  3. Replies: 2
    Last Post: 01-13-2010, 03:58 PM
  4. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 06:18 AM
  5. 16 bit or 32 bit
    By Juganoo in forum C Programming
    Replies: 9
    Last Post: 12-19-2002, 06:24 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21