Question

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.

Was it helpful?

Solution

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;
}

OTHER TIPS

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: How to detect Windows 64-bit platform with .NET?

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.

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 
}

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

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
}

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

C# Code:

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top