[Visual Studio] — Differences between x86, x64 and AnyCPU

Hanz
3 min readApr 25, 2020

I believe most programmers are aware of these options, many still lack a whole clear picture of these options. I would like to document and share some points to clear away confusion.

First, we should talk about the OS, earlier, Windows OS was dominating the market with 32-bit OS until AMD invented a first 64-bit processor ever. So, the OS continued evolving over the years, from 16-bit OS to 32-bit (x86) and years ago to 64-bit (x64).

64-bit OS vs 32-bit OS, What’s the difference?

To understand this, let’s understand the capabilities of both 32-bit and 64-bit CPUs.

  • 32-bit CPU: Address pointer size is 32 bits which can access 2³² (4,294,967,296) discrete addresses. This allows a program to make a data structure in memory up to 4 GB in size.
  • 64 bit CPU: Address pointer size is 64 bits which can access 2⁶⁴ (18,446,744,073,709,551,616) discrete addresses. This allows a program to make a data structure in memory up to 16 Exabytes in size.
  • Key Points: Processes on a 64-bit CPU can work with a larger set of data compared to 32-bit CPU besides the constraint by physical memory. A 64-bit integer makes arithmetic or logical operations using 64-bit types such as C#’s long faster than one implemented as two 32 bit operations.
  • Summary: Applications that use large amounts of memory, like software process images and videos, 3D rendering utilities, and video games will make better use of a 64-bit architecture and OS, especially if the machine has 8 or even 16 GB of RAM that can be divided among the applications that need it.

The behavior of 32-bit, 64-bit and Any CPU EXEs

In Visual Studio IDE, there are three options for compilation: x86, x64 and AnyCPU, what happens when you run different compiled process executables on a 32-bit and 64-bit machine?

On a 32-bit machine:

  • Any CPU: Runs as a 32-bit process, can load Any CPU and x86 assemblies(dlls, libs) , will get BadImageFormatException if it tries to load an x64 assembly.
  • Any CPU-32-bit preferred (default): Same as Any CPU.
  • x86: Same as Any CPU.
  • x64: BadImageFormatException always.

On a 64-bit machine:

  • Any CPU: Runs as a 64-bit process, can load Any CPU and x64 assemblies, will get BadImageFormatException if it tries to load an x86 assembly.
  • Any CPU-32-bit preferred (default): Runs as a 32-bit process, can load Any CPU and x86 assemblies, will get BadImageFormatException if it tries to load an x64 assembly.
  • x86: Runs as a 32-bit process, can load Any CPU and x86 assemblies, will get BadImageFormatException if it tries to load an x64 assembly.
  • x64: Same as Any CPU.

So, if we don’t know the target machine is 32-bit, 64-bit or both, we can compile our executables as 32-bit, and the assemblies as Any CPU.

--

--