# Checking if machine is 64-bit

**URL:** <https://rubytalk.org/t/checking-if-machine-is-64-bit/39485>\
**Category:** ruby-talk\
**Created:** [28 July 2007 17:42 UTC](https://rubytalk.org/t/checking-if-machine-is-64-bit/39485 "2007-07-28T17:42:46Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Suraj\_Kurapati1](https://avatars.discourse-cdn.com/v4/letter/s/f19dbf/32.png) [@Suraj\_Kurapati1](https://rubytalk.org/u/Suraj_Kurapati1)\
**Post date:** [28 July 2007 17:42 UTC](https://rubytalk.org/t/checking-if-machine-is-64-bit/39485/1 "2007-07-28T17:42:46Z")

</div>

Hello,

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

&nbsp;&nbsp;require 'rbconfig'  
&nbsp;&nbsp;is\_machine\_64\_bit = ( Config::CONFIG["arch"] =~ /64/ )

Is there a better way?

Thanks.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Michael\_Fellinger1](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@Michael\_Fellinger1](https://rubytalk.org/u/Michael_Fellinger1)\
**Post date:** [28 July 2007 19:03 UTC](https://rubytalk.org/t/checking-if-machine-is-64-bit/39485/2 "2007-07-28T19:03:28Z")

</div>

> Hello,
> 
> I am checking if the machine is 64-bit by doing this:
> 
> &nbsp;&nbsp;require 'rbconfig'  
> &nbsp;&nbsp;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

> **···**
>
> On 7/29/07, Suraj Kurapati \<snk@gna.org\> wrote:

---

<div class="post-metadata">

**Author:** ![Daniel\_Berger2](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/daniel_berger2/32/2243_2.png) [@Daniel\_Berger2](https://rubytalk.org/u/Daniel_Berger2)\
**Post date:** [28 July 2007 19:50 UTC](https://rubytalk.org/t/checking-if-machine-is-64-bit/39485/3 "2007-07-28T19:50:01Z")

</div>

if (2\*\*33).kind\_of?(Bignum)  
&nbsp;&nbsp;&nbsp;# 32 bit  
else  
&nbsp;&nbsp;&nbsp;# 64 bit  
end

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

Regards,

Dan

> **···**
>
> 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:
> > 
> > &nbsp;&nbsp;require 'rbconfig'  
> > &nbsp;&nbsp;is\_machine\_64\_bit = ( Config::CONFIG["arch"] =~ /64/ )
> > 
> > Is there a better way?

---

<div class="post-metadata">

**Author:** ![Nobuyoshi\_Nakada1](https://avatars.discourse-cdn.com/v4/letter/n/9f8e36/32.png) [@Nobuyoshi\_Nakada1](https://rubytalk.org/u/Nobuyoshi_Nakada1)\
**Post date:** [28 July 2007 23:38 UTC](https://rubytalk.org/t/checking-if-machine-is-64-bit/39485/4 "2007-07-28T23:38:11Z")

</div>

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:
> 
> &nbsp;&nbsp;require 'rbconfig'  
> &nbsp;&nbsp;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:

&nbsp;&nbsp;is\_machine\_64\_bit = check\_sizeof("int") == 64

&nbsp;&nbsp;is\_machine\_64\_bit = try\_static\_assert("sizeof int == 64")

or, better in C source(s):

&nbsp;&nbsp;#if SIZEOF\_VOIDP == 8

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

&nbsp;&nbsp;is\_machine\_64\_bit = [0].pack("i!").size == 8

Note that all of the above check for the size of int.

> **···**
>
> --  
> Nobu Nakada

---

<div class="post-metadata">

**Author:** ![Suraj\_Kurapati1](https://avatars.discourse-cdn.com/v4/letter/s/f19dbf/32.png) [@Suraj\_Kurapati1](https://rubytalk.org/u/Suraj_Kurapati1)\
**Post date:** [28 July 2007 22:31 UTC](https://rubytalk.org/t/checking-if-machine-is-64-bit/39485/5 "2007-07-28T22:31:35Z")

</div>

Daniel Berger wrote:

> if (2\*\*33).kind\_of?(Bignum)  
> &nbsp;&nbsp;&nbsp;# 32 bit  
> else  
> &nbsp;&nbsp;&nbsp;# 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:

&nbsp;&nbsp;is\_machine\_64\_bit = (2 \*\* 32).is\_a? Fixnum

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Suraj\_Kurapati1](https://avatars.discourse-cdn.com/v4/letter/s/f19dbf/32.png) [@Suraj\_Kurapati1](https://rubytalk.org/u/Suraj_Kurapati1)\
**Post date:** [28 July 2007 22:40 UTC](https://rubytalk.org/t/checking-if-machine-is-64-bit/39485/6 "2007-07-28T22:40:11Z")

</div>

Suraj Kurapati wrote:

> &nbsp;&nbsp;is\_machine\_64\_bit = (2 \*\* 32).is\_a? Fixnum

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

&nbsp;&nbsp;(-1.size) is 4 on a 32-bit machine and 8 on a 64-bit machine.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Xeno\_Campanoli](https://avatars.discourse-cdn.com/v4/letter/x/e56c9b/32.png) [@Xeno\_Campanoli](https://rubytalk.org/u/Xeno_Campanoli)\
**Post date:** [28 July 2007 22:51 UTC](https://rubytalk.org/t/checking-if-machine-is-64-bit/39485/7 "2007-07-28T22:51:19Z")

</div>

Suraj Kurapati wrote:

> Suraj Kurapati wrote:
> 
> > &nbsp;&nbsp;is\_machine\_64\_bit = (2 \*\* 32).is\_a? Fixnum
> 
> Hmm, after reading the ri manpage for Fixnum, I found a better way:
> 
> &nbsp;&nbsp;(-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.

---

<div class="post-metadata">

**Author:** ![Lionel\_Bouton](https://avatars.discourse-cdn.com/v4/letter/l/a88e4f/32.png) [@Lionel\_Bouton](https://rubytalk.org/u/Lionel_Bouton)\
**Post date:** [28 July 2007 22:56 UTC](https://rubytalk.org/t/checking-if-machine-is-64-bit/39485/8 "2007-07-28T22:56:17Z")

</div>

Suraj Kurapati wrote:

> Suraj Kurapati wrote:  
> &nbsp;&nbsp;
> 
> > &nbsp;&nbsp;is\_machine\_64\_bit = (2 \*\* 32).is\_a? Fixnum  
> > &nbsp;&nbsp;&nbsp;&nbsp;  
> > Hmm, after reading the ri manpage for Fixnum, I found a better way:
> 
> &nbsp;&nbsp;(-1.size) is 4 on a 32-bit machine and 8 on a 64-bit machine.  
> &nbsp;&nbsp;  
> 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.

---

<div class="post-metadata">

**Author:** ![Lloyd\_Linklater](https://avatars.discourse-cdn.com/v4/letter/l/919ad9/32.png) [@Lloyd\_Linklater](https://rubytalk.org/u/Lloyd_Linklater)\
**Post date:** [30 July 2007 12:53 UTC](https://rubytalk.org/t/checking-if-machine-is-64-bit/39485/9 "2007-07-30T12:53:06Z")

</div>

Suraj Kurapati wrote:

> &nbsp;&nbsp;(-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-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [28 July 2007 23:30 UTC](https://rubytalk.org/t/checking-if-machine-is-64-bit/39485/10 "2007-07-28T23:30:05Z")

</div>

> 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).

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

Lionel.

> **···**
>
> On 7/28/07, Lionel Bouton \<lionel-subscription@bouton.name\> wrote:
> 
> >

---

<div class="post-metadata">

**Author:** ![M\_Edward\_Ed\_Borasky1](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@M\_Edward\_Ed\_Borasky1](https://rubytalk.org/u/M_Edward_Ed_Borasky1)\
**Post date:** [30 July 2007 13:20 UTC](https://rubytalk.org/t/checking-if-machine-is-64-bit/39485/11 "2007-07-30T13:20:14Z")

</div>

Lloyd Linklater wrote:

> Suraj Kurapati wrote:
> 
> > &nbsp;&nbsp;(-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??
