Go Back   EQEmulator Home > EQEmulator Forums > Development > Development::Development

Development::Development Forum for development topics and for those interested in EQEMu development. (Not a support forum)

Reply
 
Thread Tools Display Modes
  #1  
Old 06-30-2009, 06:51 PM
Kobaz
Hill Giant
 
Join Date: Nov 2008
Location: Gold Coast, Oz
Posts: 119
Default Need a C++ guru

When comparing an int with an unsigned int, are both promoted to a long by the compiler?
Reply With Quote
  #2  
Old 06-30-2009, 07:24 PM
Shendare
Dragon
 
Join Date: Apr 2009
Location: California
Posts: 814
Default

According to the assembly generated, my Visual C++ 2008 appears to treat both as unsigned ints.

Different compilers may implement such things in their own way, however.
Reply With Quote
  #3  
Old 06-30-2009, 07:32 PM
KLS
Administrator
 
Join Date: Sep 2006
Posts: 1,348
Default

Depends, MSC++ 32 bit compiler treats long and int essentially the same, however you can't rely on this being the case on other platforms or software configurations.
Reply With Quote
  #4  
Old 06-30-2009, 07:50 PM
Kobaz
Hill Giant
 
Join Date: Nov 2008
Location: Gold Coast, Oz
Posts: 119
Default

So in the following code snippet:

Code:
int a = -1;

unsigned int b = 3000000000; // 3 billion

if ( a > b) { "true" >> cout } else { "false" >> cout };
what comes out in Visual C++?
Reply With Quote
  #5  
Old 06-30-2009, 08:03 PM
Shendare
Dragon
 
Join Date: Apr 2009
Location: California
Posts: 814
Default

My compiler outputs "true" in the presented situation.

Microsoft Visual C++ 2008 Standard on WinXP Pro 32-bit.
Reply With Quote
  #6  
Old 06-30-2009, 08:19 PM
Kobaz
Hill Giant
 
Join Date: Nov 2008
Location: Gold Coast, Oz
Posts: 119
Default

What about:

Code:
int a = -1;

unsigned int b = 3000000000; // 3 billion

if ( long(a) > long(b)) { "true" >> cout } else { "false" >> cout };
?
Reply With Quote
  #7  
Old 07-01-2009, 11:00 AM
Yeormom
Discordant
 
Join Date: Apr 2004
Location: 127.0.0.1
Posts: 402
Default

Quote:
Originally Posted by Kobaz View Post
When comparing an int with an unsigned int, are both promoted to a long by the compiler?
You should get in the habbit of casting the variables to the degree of precision you are actually trying to get. By doing so, you never need to smoke test that procedure as you know exactly what will happen every time. It's good programming practice that unfortunately rarely makes it to the hobbyist world.
__________________
Yeorwned
Bane of Life [Custom Classic/PvP]
Reply With Quote
  #8  
Old 07-01-2009, 03:40 PM
Kobaz
Hill Giant
 
Join Date: Nov 2008
Location: Gold Coast, Oz
Posts: 119
Default

Ok, that makes sense. Since (as was pointed out above) on a 32 bit system a long is the same precision as an int, what should I be casting to in order to get the scenario I started with, and how? And is casting 32 bit signed to 64 bit signed expensive?
Reply With Quote
  #9  
Old 07-01-2009, 04:37 PM
Shendare
Dragon
 
Join Date: Apr 2009
Location: California
Posts: 814
Default

It isn't generally expensive at all. Most compilers and processors can work with 64-bit numbers inherently.

However, different compilers do use different terms for explicit 64-bit terminology. In MS C++ the internal type name is '__int64', as all of their integer types are actually aliases for __intXX precision-defining monikers (char is __int8, short is __int16, WORD would be 'unsigned __int16', etc.)

LONGLONG (all caps) is a common typedef defined in Windows.h for MS C++ as well, which maps to __int64.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

   

All times are GMT -4. The time now is 11:38 PM.


 

Everquest is a registered trademark of Daybreak Game Company LLC.
EQEmulator is not associated or affiliated in any way with Daybreak Game Company LLC.
Except where otherwise noted, this site is licensed under a Creative Commons License.
       
Powered by vBulletin®, Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Template by Bluepearl Design and vBulletin Templates - Ver3.3