Size of an Integer

Today, an Integer is the same as Int32. When Xojo moves to 64-bit, an Integer will be Int64, yes? So how can I tell the size of an Integer, for example, if I want to create a library that return the correct result on either 32- or 64-bit apps?

I ask because I’m introducing a function that will return the maximum value of an Integer. Right now, that’s &h7FFFFFFF, but eventually it will be &h7FFFFFFFFFFFFFFF.

You could always use #if RBVersion <= xxxx and just update that with every release.

Alternatively, coerce Integer -1 to UInt64 -1 and see what you end up with.

I believe Xojo does not have a sizeof(), but it’s documented that on 32 systems Integer is 32 bit signed and in the future 64 bit systems it will be 64 bit signed, as a workaround you could set a constant based on #if Target64bit

Yes, I could certainly update it manually in the future. I would just rather not have to remember, hence the test.

I guess calculating the max Int64 then seeing if an Integer can hold it is my best bet.

“The Integer type is an alias to the platform’s native signed integer type.
On 32-bit builds, this ends up being SInt32 and on 64-bit builds this ends up being SInt64.”.

http://documentation.xojo.com/index.php/Integer_datatypes

So I believe you can safely decide at compile time. :wink:

[quote=33212:@Kem Tekinay]I guess calculating the max Int64 then seeing if an Integer can hold it is my best bet.
[/quote]

[code]dim x as Int64
dim y as integer

x = &h7FFFFFFFFFFFFFFF
y = x
if x = y then it’s 64 bits.[/code]

[quote=33215:@Brad Hutchings][code]dim x as Int64
dim y as integer

x = &h7FFFFFFFFFFFFFFF
y = x
if x = y then it’s 64 bits.[/code][/quote]

Or just use Target32Bit/Target64Bit…

Holy smoke, I didn’t know Target64Bit was there. When was that introduced?

Thanks to all.

This is a case where there are really two answers. I’d have used Target64Bit, but that was introduced after the earliest supported version of Real Studio (this is for MacOSLib), so I ended up with a version of Brad’s code:

Protected Function NSIntegerMax() As Integer
  static r as Integer
  static needsCalc as boolean = true
  if needsCalc then
    dim n64 as Int64 = &h7FFFFFFFFFFFFFFF
    r = n64
    if n64 <> r then
      r = &h7FFFFFFF
    end if
    
    needsCalc = false
  end if
  
  return r
  
  
  // This is a method and not a constant because it will be a different value under 32-bit and 64-bit.
  // Would use Target64Bit here but that is not supported in 2011r1. Otherwise, the code would look like this:
  
  '#if Target64Bit
  'return &h7FFFFFFFFFFFFFFF
  '#else
  'return &h7FFFFFFFF
  '#endif
  
End Function

You could use something like:

#if RBVersion >= 2011.02
  #if Target64Bit
    return &h7FFFFFFFFFFFFFFF
  #else
    return &h7FFFFFFFF
  #endif
#else
  return &h7FFFFFFFF
#endif

I’m not sure on the version information, but I’d expect this to work.

Well that’s true. I was so fixated on a mathematical solution, I missed the obvious one. Thanks.

2011 r2 or so

Yes, I changed my code to something very close to what Joe recommended. I just used RBVersion >= 2013.01 to be on the safe side.