[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

checking if machine is 64-bit

Suraj Kurapati

7/28/2007 5:43:00 PM

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.
--
Posted via http://www.ruby-....

9 Answers

Michael Fellinger

7/28/2007 7:03:00 PM

0

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

Daniel Berger

7/28/2007 7:50:00 PM

0

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


Suraj Kurapati

7/28/2007 10:32:00 PM

0

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
--
Posted via http://www.ruby-....

Suraj Kurapati

7/28/2007 10:40:00 PM

0

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.
--
Posted via http://www.ruby-....

Xeno Campanoli

7/28/2007 10:51:00 PM

0

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?

--
The only sustainable organizing methods focus not on scale,
but on good design of the functional unit,
not on winning battles, but on preservation.

Lionel Bouton

7/28/2007 10:56:00 PM

0

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.

Nobuyoshi Nakada

7/28/2007 11:38:00 PM

0

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.

--
Nobu Nakada

Lloyd Linklater

7/30/2007 12:53:00 PM

0

Suraj Kurapati wrote:
> (-1.size) is 4 on a 32-bit machine and 8 on a 64-bit machine.

Very slick! Nice find!

--
Posted via http://www.ruby-....

M. Edward (Ed) Borasky

7/30/2007 1:20:00 PM

0

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