# Is my OS 32-bit or 64-bit?

It's not too important anymore, but occasionally somebody needs to know.

Here is a simple golf: Taking no user input, tell me if the computer on which the code is run is on a 64-bit operating system, or a 32-bit operating system!

If the code is run on a 32-bit operating system, print "32", if the code is run on a 64 bit operating system, output "64". Important: Print any other non-empty string of alphanumeric characters if it's neither 32 or 64 bit.

Please note that a 32 bit program running on a computer with a 64 bit operating system should output "64". You can assume that users will use 64 bit software whenever possible.

To be eligible for entry, your code must be able to run on Windows 4.10 or newer Microsoft supported Windows systems, and at least one flavor of Linux of your choosing (so long as that flavor is gratis). Compatibility mods can be installed, so long as the program still returns the right value.

The usual rules apply.

Note: If your answer is only meant to print out 32 or 64, but not the alt case, I'll accept it, but it is not a competing answer.

I'll try and post some results of running these codes on different OSes later!

• So "Please note that a 32 bit program running on a computer with a 64 bit operating system should output "64". You can assume that users will use 64 bit software whenever possible." means that if the interprettor/compiler etc is is available in both 32 bit and 64 bit, then a 32 bit OS, will always run the 32 bit version of the interpreter/etc, and the 64 bit OS will always run the 64 bit interpreter/etc. So worrying about the difference between the program being 32 or 64, and the OS being 32 or 64, is basically only a problem for languages with only 32 bit implementations. Right? – Lyndon White Jun 20 '17 at 4:33
• Many solutions here would print "32" on a 64-bit OS if a 32-bit compiler was used to compile program. Is this OK? – Martin Rosenau Jun 20 '17 at 8:54
• What in the world is "Windows 4.10"? Does that mean Windows 98? Or does it mean Windows NT 4? What do you consider to be "newer" than that? This seems an exceptionally poorly thought-out challenge. – Cody Gray Jun 20 '17 at 9:47
• There is no "official windows spec", and nobody refers to Windows 98 as being "Windows 4.10". You are literally the first. So maybe instead of trying to sound cool or official by using version numbers, you should just use the actual product name. By the way, Windows 9x was never available in a 64-bit build, so is it actually legitimate for me to submit an entry that runs only on Windows 98 and just returns "32"? Seems very unfair/unsporting/uninteresting, but would technically be allowed by your rules. – Cody Gray Jun 21 '17 at 2:25
• You still haven't answered the question about the Windows version. Does must be able to run on Windows 4.10 or newer mean on Windows 4.10 and all newer versions or on any single Windows version, 4.10 or newer? – Dennis Jun 21 '17 at 3:08

# C, 36 bytes

Note that the following program will need to be compiled on the system to be tested.

main(){printf("%u",8*sizeof(int*));}


Simple. Assumes that the machine's word length is the same as the pointer size, which seems to be a valid assumption.

sizeof(int*) (or any other pointer type, for that matter), should be 4 on a 32-bit system, and 8 on a 64-bit one (and 2 on a 16-bit system). This should also the be the most portable of all the solutions given here till now - and work back to any system with a ISO C89 compiler.

The gotcha here is that this program must be compiled for every system which it needs to be run on, otherwise it'll output the bitness of the operating system it was compiled on.

• Like all pointer-size solutions, this will print the wrong result when the program is a 32-bit binary running on a 64-bit OS. – DarkDust Jun 20 '17 at 11:39
• @DarkDust, I did add a note about it saying how it needs to be compiled for each system it'll be run on. I won't bother improving my answer before l because it looks like DexterCD got it working with the runtime – Tamoghna Chowdhury Jun 20 '17 at 12:08
• Does it explicitly need to have the .c extension? – Restioson Jun 22 '17 at 15:59
• @Restioson I'm not sure, but I think it'll work with GCC without the extension. I'll keep the penalty in interest of portability. – Tamoghna Chowdhury Jun 22 '17 at 17:24
• @Foo, I realize your edit is in good intention, but you have misunderstood why I've allowed a byte count penalty - this question requires the program run on multiple systems - mine needs to be compiled on each system it runs on. Hence I accept a penalty for a non-conforming answer, and I've rejected your edit. – Tamoghna Chowdhury Jun 25 '17 at 9:20

# J, 8 bytes

2^5+IF64


Alternatively, from the metal up (as opposed to using built-ins), 23 bytes:

2^5+1<224-~a.i.{.3!:1''


Based on the approach in the relevant RosettaCode task, as well as the underlying J documentation.

A version which would work on the theoretical, non-existent 16- or 128-bit J interpreters, provided they use the same code base, 11 bytes:

>:2^.|{.i:_j1


This works in both the real-life 32- and 64-bit J interpreters, and is a function of the implementation, not the specification.

• What does this theoretically print on a 128 bit os? – tuskiomi Jun 28 '17 at 17:51
• @tuskiomi The first, IF64 has boolean semantics. I doubt a theoretical 128-bit J interpreter would set IF64 to 2; they'd likely introduce a new global IF128. The bare-metal one has a better chance of generalizing to greater bit widths, but of course the current documentation doesn't say what the code will be for 128 bit OSes. – Dan Bron Jun 28 '17 at 17:55
• Ah. Maybe a different output for 16 bit then? – tuskiomi Jun 28 '17 at 18:02
• @tuskiomi This particular puzzle makes it hard to answer theoretical questions confidently. The results of the expression are interpreter-implementation specific (per the rules in your question). We'd have to have the documentation for the 16-bit interpreter, which was never built, or the 128-bit one, which never may be. – Dan Bron Jun 28 '17 at 18:06

puts [expr 8*$tcl_platform(pointerSize)]  # demo I think I can golf it using some bit-shift magic • I don't see a good way to make that shorter, other than assuming you're running interactively (which could let you drop the puts [...] wrapper. – Donal Fellows Jun 19 '17 at 23:00 • You can probably do $tcl_platform(pointerSize)<<3, but this is bigger. Disclaimer: I don't know TCL. It works in Python anyways :) – Restioson Jun 22 '17 at 15:58
• @Restioson: Yes, it is bigger. I was thinking about something like puts [expr -1>>31==1?32:-1>>63==1?64:0] , but it didn't work. – sergiol Jun 22 '17 at 22:00
• @sergiol sorry, I don't know TCL :p – Restioson Jun 23 '17 at 9:25
• @Restioson: no problem – sergiol Jun 23 '17 at 9:26

# PHP, 25 Bytes

<?=PHP_INT_SIZE==4?32:64;

• That detects if PHP is 32 or 64 bit, not the OS. – Petah Jun 20 '17 at 12:55
• this will print 64 if there is an 8-bit operating system – tuskiomi Jun 21 '17 at 13:38
• @tuskiomi Have you tried running PHP on an arduino lately (or any other 8 bit operating system (Yes I know an arduino isn't an operating system))? I see it as: since PHP only runs on 32 and 64 bit CPUs then it doesn't matter that it prints 64 on a 8-bit operating system. +1 from me. – Bja Jun 21 '17 at 15:28
• @Bja codegolf.meta.stackexchange.com/a/11355/60951 if only 32 or 64 bit systems are supported, it will never print the 3rd case. – tuskiomi Jun 21 '17 at 15:31
• @Bja that's a valid competitive answer. – tuskiomi Jun 21 '17 at 15:48

# Javascript (ES6), 464140 38 Bytes

-5 Bytes thanks to @GustavoRodrigues

-2 Bytes thanks to @GustavoRodrigues!

_=>/64/.test(navigator.userAgent)+1<<5


I was only able to test it on my Windows x64 machine, so any feedback will be much appreciated :)

Edit: removed the x from x64 to fix an issue pointed out by @ThomasAyoub, I don't know if this is going to cause any unexpected behaviour.

var c=
_=>/64/.test(navigator.userAgent)+1<<5

console.log(c());

• From other answers I don't think you need to return a string, if so: _=>navigator.userAgent.includesx64+1<<5 – Gustavo Rodrigues Jun 20 '17 at 13:05
• @GustavoRodrigues Clever approach! Thanks :) – Hankrecords Jun 20 '17 at 13:10
• Doesn't work on my laptop, it prints 32 instead of 64 – Thomas Ayoub Jun 20 '17 at 13:21
• @ThomasAyoub Could you please post your laptop's full navigator.userAgent string? – Hankrecords Jun 20 '17 at 13:45
• How about using regular expressions? _=>/64/.test(navigator.userAgent)+1<<5 – Gustavo Rodrigues Jun 20 '17 at 18:17

# Java 7, 77 71 bytes

A java pointer-size solution

void a(){System.out.print(Integer.toHexString(hashCode()).length()*8);}


## x86 machine code, 8 bytes

Bytecode:

b0 40 33 c9 41 d2 e8 c3

which decodes to:

mov al, 64
xor ecx, ecx
inc ecx
shr al, cl
ret

The "inc ecx/shr al,cl" in 32-bit becomes "shr r8b,cl" in 64-bit, and returns either 32 or 64 in the AL register.

# Batch, 54 53 51 bytes

Not the shortest.

@if defined ProgramFiles(x86) (echo 64)else echo 32


This checks for a 64-bit system only variable. If the variable exists, then return 64. In any other case, it returns 32.

To make this work on Linux(assuming Ubuntu):

• Install wine using sudo apt-get install wine
• Run wine cmd in the terminal
• Call the script: scriptname.bat
• I get Can't recognize 'x86' as an internal or external command, or batch script. before wine cmd prints 64. – Ruslan Jun 26 '17 at 15:11
• Weird.. did you use the same script I posted? – stevefestl Jun 26 '17 at 22:36
• @Ruslan I've edited the script, please check does this works on 32 bit systems – stevefestl Jun 26 '17 at 22:39
• No, still the same error message, and both current and previous versions output 64 on a 32-bit system. – Ruslan Jun 27 '17 at 5:28
• @Ruslan Did you get the Can't recognize error message on Wine or cmd? – stevefestl Jun 27 '17 at 8:52

# APL (Dyalog), 20 bytes

Runs on Windows NT/CE, Linux, AIX, macOS and Solaris. Assumes exposure of root properties.

32×1+'6'∊⊃APLVersion


Try it online!

APLVersion (Target Environment, Version Number, Version Type, Program Type)

⊃ pick the first element (OSName[-64])

'6'∊ is six a member thereof?

1+ add one to that Boolean

32× multiply 32 by that

If root properties are not exposed, use ⎕WG'APLVersion' instead of APLVersion.

## SAS, 28

%put%eval(&syssizeoflong*8);


Runs on any OS that supports SAS - there are many...

# Go, 94 bytes

package main;import"fmt";const wordsize=32<<(^uint(0)>>32&1);func main(){fmt.Println(wordsize)}


# JavaScript, 26 34 bytes

console.log((
_=>/\d\d/.exec(navigator.oscpu)[0]
)())

Tested with Firefox. Not all browsers support the navigator.oscpu property.

Edit: rewrote my answer to comply with the specification. Thanks to Restioson for pointing out my error.

• I thought that it had to print 64, 32, and a non empty string respectively? – Restioson Jun 23 '17 at 9:26

# Python 2.7, 49 bytes

from platform import*;print architecture()[0][:2]


# VB.NET (Mono), 99 65 Bytes

Full subroutine and module which take no input and outputs if the OS is 64 bit or not to the console.

Module M
Sub Main
Console.Write(8*IntPtr.Size)
End Sub
End Module


Try it Online. Returns 64 as TIO is run on a 64 Bit Unix 4.11.3.202 server

-34 Bytes thanks to @Alexander

# VBA, 41 Bytes (Non-Competing)

Anonymous VBE immediate window function that takes no input and outputs OS Version to the VBE immediate window

This only works on Windows as VBA cannot be run on Linux

?Val(Mid(Application.OperatingSystem,10))


## TXR Lisp, 16 bytes

(*(sizeof val)8)


Same for long or cptr instead of val; but those are one character longer. val is the FFI type denoting a raw Lisp value. That corresponds to a pointer, whose size usually determines the "bitness" of a system.

## Perl 6, 17 bytes

say \$*KERNEL.bits


# Powershell, 47 bytes

(32,64)[[Environment]::Is64BitOperatingSystem]


Basically, it's an array that's indexed into by the truthy value returned by the environment.

This avoids the pointer methods, so will correctly identify the bitness of the OS rather than the bitness of the process.

Appears to work on Windows and Linux.

• Wait, Powershell on Linux?? Sign me up! But really, did you just paste this into the shell or what? – Cullub Jun 21 '17 at 12:30
• Yes - see github.com/PowerShell/PowerShell/blob/master/README.md (packages for Ubuntu, Debian, Redhat, etc, all from the Microsoft PS team); and yes - just pasted into the shell. – Chris J Jun 21 '17 at 12:42
• this will print 32 for an 8-bit os, no? – tuskiomi Jun 21 '17 at 13:40
• @tuskiomi - an 8-bit OS isn't one of the target systems according to the rules (at least a supported Windows and a Linux distribution of choice) – Chris J Jun 21 '17 at 19:26
• @ChrisJ agreed, but it does need an alternate case for non 32/64 bit systems in order to be competitive. Still valid though. – tuskiomi Jun 21 '17 at 19:28