-4

How to write a function in java to detect whether your operating system is running on a 32 bit processor or 64 bit processor ? One answer i found in the internet was this Shift 1 into an int until the int flips to 0 and count how many places you were able to shift over. But i am not able to understand the solution.

user3246489
  • 1,111
  • 3
  • 12
  • 20
  • The solution you describe with an `int` will not work in Java, because in Java (unlike in C or C++) an `int` is always 32 bits, no matter if the JVM and operating you are running on is 32-bit or 64-bit. – Jesper Feb 09 '14 at 21:18

2 Answers2

1

System.getProperty ("os.arch");

On my PC returns amd64.

Btw,That is really a popular gotcha.

Article by Oracle: Beware when detecting the 32 or 64 bit OS

Please note, the os.arch property will only give you the architecture of the JRE, not of the underlying os.

If you install a 32 bit jre on a 64 bit system, System.getProperty("os.arch") will return x86

In order to actually determine the underlying architecture, you will need to write some native code. See this post for more info (and a link to sample native code).

If you're using windows then this will be the exact solution for you:

boolean is64bit = false;

if (System.getProperty("os.name").contains("Windows")) {

is64bit = (System.getenv("ProgramFiles(x86)") != null);

} else {

is64bit = (System.getProperty("os.arch").indexOf("64") != -1);

}`

The processor architecture can be detected by using the code below:

    String arch = System.getenv("PROCESSOR_ARCHITECTURE");
String wow64Arch = System.getenv("PROCESSOR_ARCHITEW6432");

String realArch = arch.endsWith("64")
                  || wow64Arch != null && wow64Arch.endsWith("64")
                      ? "64" : "32";
System.out.println(realArch);
Pankaj
  • 592
  • 7
  • 19
  • 1
    This won't work if you run it within a x32 bit JVM. See http://stackoverflow.com/questions/4748673/how-can-i-check-the-bitness-of-my-os-using-java-j2se-not-os-arch for more details – MadProgrammer Feb 09 '14 at 21:17
0

In native languages, such as C and C++:

  • Your code is executed directly on the CPU.

  • You have a direct access to memory, which is the primary reason for which the language also provides the ability to determine the size of different types (using the sizeof operator).

In non-native languages, such as Java and C#:

  • Your code is executed on a VM (which is essentially a CPU-emulation program).

  • You do not have a direct access to memory, hence the language does not really need to provide the ability to determine the size of different types.

So whereas in C and C++ you could use sizeof(void*) * CHAR_BIT in order to determine the width of your processor's memory-bus, you cannot do the same in Java and C#.

The solution you mention, probably assumes that the size of int in Java is 32 bits on a 32-bit VM, and 64-bits on a 64-bit VM.

Even if this assumption is true, it will indicate the type of the VM that you are running on, and not the type of the CPU that you are running on.

Now, assuming that the size of int in Java is indeed 32 bits on a 32-bit VM and 64-bits on a 64-bit VM, here is the logic behind the solution:

int n = 1; // n == 0x0000000000000001 (iteration #0)
n <<= 1;   // n == 0x0000000000000002 (iteration #1)
n <<= 1;   // n == 0x0000000000000004 (iteration #2)
n <<= 1;   // n == 0x0000000000000008 (iteration #3)
n <<= 1;   // n == 0x0000000000000010 (iteration #4)
n <<= 1;   // n == 0x0000000000000020 (iteration #5)
n <<= 1;   // n == 0x0000000000000040 (iteration #6)
n <<= 1;   // n == 0x0000000000000080 (iteration #7)
n <<= 1;   // n == 0x0000000000000100 (iteration #8)
n <<= 1;   // n == 0x0000000000000200 (iteration #9)
n <<= 1;   // n == 0x0000000000000400 (iteration #10)
n <<= 1;   // n == 0x0000000000000800 (iteration #11)
...
n <<= 1;   // n == 0x1000000000000000 (iteration #60)
n <<= 1;   // n == 0x2000000000000000 (iteration #61)
n <<= 1;   // n == 0x4000000000000000 (iteration #62)
n <<= 1;   // n == 0x8000000000000000 (iteration #63)
n <<= 1;   // n == 0x0000000000000000 (iteration #64)

On a 32-bit system, n will run between 0x00000001 and 0x80000000 before it becomes 0x00000000 (after 32 iterations).

But if the assumption above is correct, then you might as well just do it in a single bit-shift operation:

int n = 0x80000000;
if (n<<1 == 0)
    ; // 32-bit system
else
    ; // 64-bit system

As a side note, I have to say that I doubt this assumption to begin with. On a 32-bit VM, the size of int is 32 bits and the size of long is 64 bits. So what would be the size of long on a 64-bit VM then???

barak manos
  • 29,648
  • 10
  • 62
  • 114