views:

1667

answers:

9

How do I find out if my processor is 32 bit or 64 bit (in your language of choice)? I want to know this for both Intel and AMD processors.

+16  A: 

Windows, C/C++:

#include <windows.h>

SYSTEM_INFO sysInfo, *lpInfo;
lpInfo = &sysInfo;
::GetSystemInfo(lpInfo);
switch (lpInfo->wProcessorArchitecture) {
case PROCESSOR_ARCHITECTURE_AMD64:
case PROCESSOR_ARCHITECTURE_IA64:
    // 64 bit
    break;
case PROCESSOR_ARCHITECTURE_INTEL:
    // 32 bit
    break;
case PROCESSOR_ARCHITECTURE_UNKNOWN:
default:
    // something else
    break;
}
plinth
Can you specify which language this is (yes, *I* know but others may not) and any other requirements (e.g., header files)?
paxdiablo
Or, for that matter, what OS? Will this work on my Mac Mini, my Linux boxes, and my Windows XP and Vista systems?
David Thornley
Bah - can't edit... but so *need* to add the 'default:' to that switch... :-)
saw-lau
@saw-lau - done
Dominic Rodger
@Dominic - thank you so much! I can sleep at night now. :-)
saw-lau
@saw-lau - haha!
Dominic Rodger
A: 

I was thinking, on a 64-bit processor, pointers are 64-bit. So, instead of checking processor features, it maybe possible to use pointers to 'test' it programmatically. It could be as simple as creating a structure with two contiguous pointers and then checking their 'stride'.

sybreon
No, won't help with 32 bit code running on 64 bit enabled processor.
mghie
+7  A: 

C#, OS agnostic

sizeof(IntPtr) == 4 ? "32-bit" : "64-bit"

This is somewhat crude but basically tells you whether the CLR is running as 32-bit or 64-bit, which is more likely what you would need to know. The CLR can run as 32-bit on a 64-bit processor, for example.

For more information, see here: http://stackoverflow.com/questions/336633/how-to-detect-windows-64-bit-platform-with-net

Matt Howells
+4  A: 

The tricky bit here is you might have a 64 bit CPU but a 32 bit OS. If you care about that case it is going to require an asm stub to interrogate the CPU. If not, you can ask the OS easily.

Joshua
A: 

VBScript, Windows:

Const PROCESSOR_ARCHITECTURE_X86 = 0
Const PROCESSOR_ARCHITECTURE_IA64 = 6
Const PROCESSOR_ARCHITECTURE_X64 = 9

strComputer = "."

Set oWMIService = GetObject("winmgmts:" & _
    "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colProcessors = oWMIService.ExecQuery("SELECT * FROM Win32_Processor")

For Each oProcessor In colProcessors
  Select Case oProcessor.Architecture
    Case PROCESSOR_ARCHITECTURE_X86
      ' 32-bit
    Case PROCESSOR_ARCHITECTURE_X64, PROCESSOR_ARCHITECTURE_IA64
      ' 64-bit
    Case Else
      ' other
  End Select
Next

Another possible solution for Windows Script Host, this time in JScript and using the PROCESSOR_ARCHITECTURE environment variable:

var oShell = WScript.CreateObject("WScript.Shell");
var oEnv = oShell.Environment("System");
switch (oEnv("PROCESSOR_ARCHITECTURE").toLowerCase())
{
  case "x86":
    // 32-bit
  case "amd64":
    // 64-bit
  default:
    // other
}
Helen
+1  A: 

In .NET you can differentiate x86 from x64 by looking at the Size property of the IntPtr structure. The IntPtr.Size property is returned in bytes, 8 bits per byte so it is equal to 4 on a 32-bit CPU and 8 on a 64-bit CPU. Since we talk about 32-bit and 64-bit processors rather than 4-byte or 8-byte processors, I like to do the comparison in bits which makes it more clear what is going on.

C#

if( IntPtr.Size * 8 == 64 )
{
    //x64 code
}

PowerShell

if( [IntPtr]::Size * 8 -eq 64 )
{
    #x64 code 
}
Brian Reiter
A: 

C# Code:

int size = Marshal.SizeOf(typeof(IntPtr));
if (size == 8)
{
 Text = "64 bit";
}
else if (size == 4)
{
 Text = "32 bit";
}
NinethSense
A: 

In Python :

In [10]: import platform
In [11]: platform.architecture()
Out[11]: ('32bit', 'ELF')

As usual, pretty neat. But I'm pretty sure these functions return the platform where the exec has been built, not the the platforms it running on. There is still a small chance that some geek is running a 32 bits version on a 64 bits computer.

You can have some more infos like :

In [13]: platform.system()
Out[13]: 'Linux'

In [19]: platform.uname()
Out[19]: 
('Linux',
 'asus-u6',
 '2.6.28-11-generic',
 '#42-Ubuntu SMP Fri Apr 17 01:57:59 UTC 2009',
 'i686',
 '')

ETC.

This looks more like live data :-)

e-satis
I am that geek. 64bit python exists, but its difficult to get prebuilt modules for that architecture, and almost as hard to get working ones compiled locally.
TokenMacGuy
A: 

In linux you can determine the "bitness" by reading

/proc/cpuinfo

eg.

cat /proc/cpuinfo | grep flags

if it contains the

lm

flag it's a x86 64 bit CPU (even if you have 32 bit linux installed)

Not sure if this works for non x86 CPUs as well such as PPC or ARM.

Ben Schwehn