54
\$\begingroup\$

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!

\$\endgroup\$
23
  • 5
    \$\begingroup\$ 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? \$\endgroup\$ Commented Jun 20, 2017 at 4:33
  • 2
    \$\begingroup\$ Many solutions here would print "32" on a 64-bit OS if a 32-bit compiler was used to compile program. Is this OK? \$\endgroup\$ Commented Jun 20, 2017 at 8:54
  • 16
    \$\begingroup\$ 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. \$\endgroup\$
    – Cody Gray
    Commented Jun 20, 2017 at 9:47
  • 14
    \$\begingroup\$ 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. \$\endgroup\$
    – Cody Gray
    Commented Jun 21, 2017 at 2:25
  • 3
    \$\begingroup\$ 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? \$\endgroup\$
    – Dennis
    Commented Jun 21, 2017 at 3:08

49 Answers 49

1
2
3
\$\begingroup\$

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.

\$\endgroup\$
9
  • \$\begingroup\$ 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. \$\endgroup\$
    – DarkDust
    Commented Jun 20, 2017 at 11:39
  • 1
    \$\begingroup\$ @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 \$\endgroup\$ Commented Jun 20, 2017 at 12:08
  • \$\begingroup\$ Does it explicitly need to have the .c extension? \$\endgroup\$
    – Restioson
    Commented Jun 22, 2017 at 15:59
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Jun 22, 2017 at 17:24
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Jun 25, 2017 at 9:20
3
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\begingroup\$ What does this theoretically print on a 128 bit os? \$\endgroup\$
    – tuskiomi
    Commented Jun 28, 2017 at 17:51
  • \$\begingroup\$ @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. \$\endgroup\$
    – Dan Bron
    Commented Jun 28, 2017 at 17:55
  • \$\begingroup\$ Ah. Maybe a different output for 16 bit then? \$\endgroup\$
    – tuskiomi
    Commented Jun 28, 2017 at 18:02
  • \$\begingroup\$ @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. \$\endgroup\$
    – Dan Bron
    Commented Jun 28, 2017 at 18:06
2
\$\begingroup\$

tcl, 40

puts [expr 8*$tcl_platform(pointerSize)]

demo

I think I can golf it using some bit-shift magic

\$\endgroup\$
5
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Jun 19, 2017 at 23:00
  • \$\begingroup\$ You can probably do $tcl_platform(pointerSize)<<3, but this is bigger. Disclaimer: I don't know TCL. It works in Python anyways :) \$\endgroup\$
    – Restioson
    Commented Jun 22, 2017 at 15:58
  • \$\begingroup\$ @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. \$\endgroup\$
    – sergiol
    Commented Jun 22, 2017 at 22:00
  • \$\begingroup\$ @sergiol sorry, I don't know TCL :p \$\endgroup\$
    – Restioson
    Commented Jun 23, 2017 at 9:25
  • \$\begingroup\$ @Restioson: no problem \$\endgroup\$
    – sergiol
    Commented Jun 23, 2017 at 9:26
2
\$\begingroup\$

PHP, 25 Bytes

<?=PHP_INT_SIZE==4?32:64;
\$\endgroup\$
8
  • 1
    \$\begingroup\$ That detects if PHP is 32 or 64 bit, not the OS. \$\endgroup\$
    – Petah
    Commented Jun 20, 2017 at 12:55
  • 1
    \$\begingroup\$ this will print 64 if there is an 8-bit operating system \$\endgroup\$
    – tuskiomi
    Commented Jun 21, 2017 at 13:38
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – Bja
    Commented Jun 21, 2017 at 15:28
  • \$\begingroup\$ @Bja codegolf.meta.stackexchange.com/a/11355/60951 if only 32 or 64 bit systems are supported, it will never print the 3rd case. \$\endgroup\$
    – tuskiomi
    Commented Jun 21, 2017 at 15:31
  • 1
    \$\begingroup\$ @Bja that's a valid competitive answer. \$\endgroup\$
    – tuskiomi
    Commented Jun 21, 2017 at 15:48
2
\$\begingroup\$

Javascript (ES6), 46 41 40 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());

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

Java 7, 77 71 bytes

A java pointer-size solution

void a(){System.out.print(Integer.toHexString(hashCode()).length()*8);}
\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
8
  • \$\begingroup\$ I get Can't recognize 'x86' as an internal or external command, or batch script. before wine cmd prints 64. \$\endgroup\$
    – Ruslan
    Commented Jun 26, 2017 at 15:11
  • \$\begingroup\$ Weird.. did you use the same script I posted? \$\endgroup\$
    – stevefestl
    Commented Jun 26, 2017 at 22:36
  • \$\begingroup\$ @Ruslan I've edited the script, please check does this works on 32 bit systems \$\endgroup\$
    – stevefestl
    Commented Jun 26, 2017 at 22:39
  • \$\begingroup\$ No, still the same error message, and both current and previous versions output 64 on a 32-bit system. \$\endgroup\$
    – Ruslan
    Commented Jun 27, 2017 at 5:28
  • \$\begingroup\$ @Ruslan Did you get the Can't recognize error message on Wine or cmd? \$\endgroup\$
    – stevefestl
    Commented Jun 27, 2017 at 8:52
2
\$\begingroup\$

PowerShell, 51 44 43 bytes

-7 bytes thanks to Wasif Hasan

32+32*[Environment]::Is64BitOperatingSystem

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Here you go, omit the System. and save 7 bytes! \$\endgroup\$
    – Wasif
    Commented Dec 21, 2020 at 10:00
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

SAS, 28

%put%eval(&syssizeoflong*8);

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

\$\endgroup\$
1
\$\begingroup\$

Go, 94 bytes

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

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.

\$\endgroup\$
1
  • \$\begingroup\$ I thought that it had to print 64, 32, and a non empty string respectively? \$\endgroup\$
    – Restioson
    Commented Jun 23, 2017 at 9:26
1
\$\begingroup\$

Python 2.7, 49 bytes

from platform import*;print architecture()[0][:2]
\$\endgroup\$
1
\$\begingroup\$

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))
\$\endgroup\$
2
  • 1
    \$\begingroup\$ This seems more preferable: IntPtr.Size*8 msdn.microsoft.com/en-us/library/… \$\endgroup\$
    – Alexander
    Commented Jun 21, 2017 at 5:27
  • \$\begingroup\$ @Alexander thanks! I had looked for an Intger Bitwidth myself but I never would have thought to use IntPtr \$\endgroup\$ Commented Jun 21, 2017 at 13:56
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

Perl 6, 17 bytes

say $*KERNEL.bits
\$\endgroup\$
1
\$\begingroup\$

Powershell, 56 bytes

$null=$env:Processor_Architecture-match"\d+";$matches[0]
\$\endgroup\$
2
  • \$\begingroup\$ Your code works with windows only :) You can save some bytes ls env:p*e|% v*|sls '\d+'|% m*|% v* \$\endgroup\$
    – mazzy
    Commented Dec 21, 2020 at 4:17
  • \$\begingroup\$ But @mazzy if something other exists such as pae or anything between p and e, let's try to be fool proof \$\endgroup\$
    – Wasif
    Commented Dec 21, 2020 at 9:56
0
\$\begingroup\$

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.

\$\endgroup\$
7
  • \$\begingroup\$ Wait, Powershell on Linux?? Sign me up! But really, did you just paste this into the shell or what? \$\endgroup\$
    – Cullub
    Commented Jun 21, 2017 at 12:30
  • 2
    \$\begingroup\$ 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. \$\endgroup\$
    – Callie J
    Commented Jun 21, 2017 at 12:42
  • \$\begingroup\$ this will print 32 for an 8-bit os, no? \$\endgroup\$
    – tuskiomi
    Commented Jun 21, 2017 at 13:40
  • \$\begingroup\$ @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) \$\endgroup\$
    – Callie J
    Commented Jun 21, 2017 at 19:26
  • \$\begingroup\$ @ChrisJ agreed, but it does need an alternate case for non 32/64 bit systems in order to be competitive. Still valid though. \$\endgroup\$
    – tuskiomi
    Commented Jun 21, 2017 at 19:28
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.