Forum: Ruby checking if machine is 64-bit

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bc368ef524130e8d0deb386de961e24a?d=identicon&s=25 Suraj Kurapati (snk)
on 2007-07-28 19:42
Hello,

I am checking if the machine is 64-bit by doing this:

  require 'rbconfig'
  is_machine_64_bit = ( Config::CONFIG["arch"] =~ /64/ )

Is there a better way?

Thanks.
86e33dee4a89a8879a26487051c216a8?d=identicon&s=25 Michael Fellinger (Guest)
on 2007-07-28 21:04
(Received via mailing list)
On 7/29/07, Suraj Kurapati <snk@gna.org> wrote:
> Hello,
>
> I am checking if the machine is 64-bit by doing this:
>
>   require 'rbconfig'
>   is_machine_64_bit = ( Config::CONFIG["arch"] =~ /64/ )

Config::CONFIG['arch']
# "x86_64-linux"

> Is there a better way?

Not that i know of.

^ manveru
Aee77dba395ece0a04c688b05b07cd63?d=identicon&s=25 Daniel Berger (Guest)
on 2007-07-28 21:52
(Received via mailing list)
On Jul 28, 11:42 am, Suraj Kurapati <s...@gna.org> wrote:
> Hello,
>
> I am checking if the machine is 64-bit by doing this:
>
>   require 'rbconfig'
>   is_machine_64_bit = ( Config::CONFIG["arch"] =~ /64/ )
>
> Is there a better way?

if (2**33).kind_of?(Bignum)
   # 32 bit
else
   # 64 bit
end

Might need some tweaking once the 128 bit operating systems show up...

Regards,

Dan
Bc368ef524130e8d0deb386de961e24a?d=identicon&s=25 Suraj Kurapati (snk)
on 2007-07-29 00:31
Daniel Berger wrote:
> if (2**33).kind_of?(Bignum)
>    # 32 bit
> else
>    # 64 bit
> end

Excellent idea.  However, note that it takes 34 bits to represent 2**33,
and 33 bits to represent 2**32. Thus, we can get away with only checking
whether 2**32 is a Fixnum:

  is_machine_64_bit = (2 ** 32).is_a? Fixnum
Bc368ef524130e8d0deb386de961e24a?d=identicon&s=25 Suraj Kurapati (snk)
on 2007-07-29 00:40
Suraj Kurapati wrote:
>   is_machine_64_bit = (2 ** 32).is_a? Fixnum

Hmm, after reading the ri manpage for Fixnum, I found a better way:

  (-1.size) is 4 on a 32-bit machine and 8 on a 64-bit machine.
2be64f56da442ac119e422785e21c3a8?d=identicon&s=25 Xeno Campanoli (Guest)
on 2007-07-29 00:52
(Received via mailing list)
Suraj Kurapati wrote:
> Suraj Kurapati wrote:
>>   is_machine_64_bit = (2 ** 32).is_a? Fixnum
>
> Hmm, after reading the ri manpage for Fixnum, I found a better way:
>
>   (-1.size) is 4 on a 32-bit machine and 8 on a 64-bit machine.
Aren't you gonna get Kernel information for 32 bit if it's a 32 bit
Kernel on a 64 bit machine?
Bef7ff8a0537495a1876ffebdc9f8e51?d=identicon&s=25 Lionel Bouton (Guest)
on 2007-07-29 00:58
(Received via mailing list)
Suraj Kurapati wrote:
> Suraj Kurapati wrote:
>
>>   is_machine_64_bit = (2 ** 32).is_a? Fixnum
>>
>
> Hmm, after reading the ri manpage for Fixnum, I found a better way:
>
>   (-1.size) is 4 on a 32-bit machine and 8 on a 64-bit machine.
>

I don't know if it matters to you, but IIRC there are architectures with
different sizes for integers and memory addresses (64 bit integers with
32 bit addresses if memory serves).

Lionel.
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2007-07-29 01:31
(Received via mailing list)
On 7/28/07, Lionel Bouton <lionel-subscription@bouton.name> wrote:
> >
>
> I don't know if it matters to you, but IIRC there are architectures with
> different sizes for integers and memory addresses (64 bit integers with
> 32 bit addresses if memory serves).


MRI needs sizeof(long) == sizeof(void *)  (or it won't compile). MRI
Fixnums
are longs.

Lionel.
F1d6cc2b735bfd82c8773172da2aeab9?d=identicon&s=25 Nobuyoshi Nakada (Guest)
on 2007-07-29 01:39
(Received via mailing list)
Hi,

At Sun, 29 Jul 2007 02:42:46 +0900,
Suraj Kurapati wrote in [ruby-talk:262297]:
> I am checking if the machine is 64-bit by doing this:
>
>   require 'rbconfig'
>   is_machine_64_bit = ( Config::CONFIG["arch"] =~ /64/ )
>
> Is there a better way?

Depends on the purpose.

If you are trying to compile an extension library:

  is_machine_64_bit = check_sizeof("int") == 64

  is_machine_64_bit = try_static_assert("sizeof int == 64")

or, better in C source(s):

  #if SIZEOF_VOIDP == 8

Otherwise, if you want to know the current running platform is
64bit:

  is_machine_64_bit = [0].pack("i!").size == 8

Note that all of the above check for the size of int.
84dc575c33a123789521d53cad0f62ae?d=identicon&s=25 Lloyd Linklater (lloyd)
on 2007-07-30 14:53
Suraj Kurapati wrote:
>   (-1.size) is 4 on a 32-bit machine and 8 on a 64-bit machine.

Very slick!  Nice find!
3bb23e7770680ea44a2d79e6d10daaed?d=identicon&s=25 M. Edward (Ed) Borasky (Guest)
on 2007-07-30 15:21
(Received via mailing list)
Lloyd Linklater wrote:
> Suraj Kurapati wrote:
>>   (-1.size) is 4 on a 32-bit machine and 8 on a 64-bit machine.
>
> Very slick!  Nice find!
>

Is that portable to jRuby??
This topic is locked and can not be replied to.