g | x | w | all
Bytes Lang Time Link
040Tcl170619T212713Zsergiol
043PowerShell201221T050109Zmazzy
056Powershell201220T051055Zwasif
017Perl 6190320T050213Zbb94
016TXR Lisp170622T182812ZKaz
051Batch170620T082953Zstevefes
008x86 machine code170622T153023ZRuslan
008x86 machine code171111T022207Zpeter fe
065VB.NET Mono170621T012549ZTaylor R
013Julia170620T042639ZFrames C
050C#170620T061454Zkakkarot
008J170621T114444ZDan Bron
036C170620T104453ZTamoghna
022C170620T024427Zuser7091
nan170620T221116Zhildred
013x86 Assembly polyglot170621T055333ZObsequio
049Python 2.7170624T115040ZKoishore
017Bash170623T184112Znotagull
nan170620T131612Zuser2428
026Shell170620T014412ZZombo
094Go170622T182926ZAvdept
012x86 machine code170620T193947ZRuslan
059Python 3170620T092410ZtOmAtE
010Ruby170622T085406Zaxiac
018PHP170621T155628ZBja
047Powershell170621T092717ZCallie J
029PHP170620T033506ZPetah
nan170621T064246ZTwometer
038Javascript ES6170620T122154ZHankreco
052Windows CMD170620T043803ZKen Y-N
012Swift 4 REPL/Playground170621T005153ZAlexande
016julia170619T202155Zrahnema1
1752PowerShell170620T185009ZAndy C.
018Perl170620T093233ZGrimmy
nan170620T183425ZBrad Gil
183C170620T145407ZMrPaulch
024JavaScript Node.js170619T204317ZDowngoat
025PHP170620T114538ZMr. Alie
029C#170620T081026ZJames B
028SAS170620T094619Zuser3490
020APL Dyalog170620T073222ZAdá
016R170620T070431ZSven Hoh
016PowerShell170620T015427ZChirishm
045Java 8170619T201752ZDaniel
022Ruby170619T223238ZValue In
023C170619T210636ZGovind P
050Java170619T213432ZJeff I
041Python 2170619T201554ZDaniel
017Mathematica170619T202115ZZaMoC

Tcl, 40 bytes

puts [expr 8*$tcl_platform(pointerSize)]

Try it online!

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

PowerShell, 51 44 43 bytes

-7 bytes thanks to Wasif Hasan

32+32*[Environment]::Is64BitOperatingSystem

Try it online!

Powershell, 56 bytes

$null=$env:Processor_Architecture-match"\d+";$matches[0]

Perl 6, 17 bytes

say $*KERNEL.bits

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.

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

x86 machine code, 8 bytes

31 C0 B0 40 48 24 60 C3

Ungolfed:

31 c0    xor eax,eax
b0 40    mov al, 0x40
48       dec al — in 32-bit mode; "REX.W" in 64-bit mode (ignored)
24 60    and al, 0x60
c3       ret

If compiled as a 64-bit executable, it returns 64 in eax, and if compiled as 32-bit, then returns 32 — regardless of the OS.

This answer relies on the rules saying:

You can assume that users will use 64 bit software whenever possible.

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.

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

Julia 14 13 bytes

n->8sizeof(1)

Explain:

Other fun answer is ()->"$(Int)"[4:5], but I can't get the count down on that one.


-1 thanks to @Roman Gräf

C#, 60 50 bytes

_=>System.Environment.Is64BitOperatingSystem?64:32

Thanks @TheLethalCoder

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.

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.

C, 22 bytes

f(){return(int**)0+8;}

This is a pointer-size based answer that assumes a native binary. The 0 is cast to int** (address 0x0). Then we add 8 to 0, which, in C advances, the pointer by sizeof(int*)*8. 4 bytes * 8 bits = 32, 8 bytes * 8 bits = 64. So we get (int**)0x20 and 0x40 which are then implicitly cast as integers by returning them from an implicitly int-returning function.

C, stand-alone, 34 bytes

main(){printf("%d\n",(int**)0+8);} 

C, fun with Unicode, 30 code-points, 34 bytes(UTF-8)

main(){puts((int**)U" ㈳㐶"+1);}

Boot Loaders

Did you know that GRUB and IPXE both have Turing complete programming languages accessible at run-time? The Syslinux family of boot loaders don't but they can do this.

IPXE, 36 bytes

#!ipxe
cpuid --ext 29 && echo 64 || echo 32

the first line is needed if the script is run remotely, but not if typed directly at the command line.

GRUB, 42 bytes

if cpuid -l ; then
echo 64
else
echo 32
fi

Syslinux, 186 bytes

This takes three files the first is syslinux.cfg (or isolinux.cfg, etc.).

label a
  kernel ifcpu64.c32
  append s -- t

label s
  kernel menu.c32
  append s.cfg

label t
  kernel menu.c32
  append t.cfg

default a
prompt 0
timeout 0

and t.cfg

menu title 32

ans s.cfg

menu title 64

For this one the hard part is that syslinux does not have any simple text display capabilities, so the menu is abused.

x86 Assembly (polyglot), 13 bytes

Bytecode:

31 c0 b4 80 48 70 05 04 40 83 e0 60 c3

Defines a function which returns 32 if interpreted as 32-bit, 64 if 64-bit, and 32767 if 16-bit.

I wanted to make a polyglot which ran on Windows and Linux, but this is a lot harder than I thought. As it is I'm not sure there's even any way to print a value on non-16-bit Windows without linking.

Explanation

This code uses two tells to determine the architecture it is running on. The first is the instruction 0x48—on 16 and 32 bits, this is dec %eax, but on 64 bits, it is an instruction-size prefix. The second tell is the same instruction, however, when we execute it on the value 0x8000, the most significant bit is flipped only if the register size is 16 bits, setting the overflow flag and letting us use jo.

In 16 bits, this code is interpreted as the following:

   0:   31 c0                   xor    %ax,%ax    /* 0x0000 */
   2:   b4 80                   mov    $0x80,%ah  /* 0x8000 */
   4:   48                      dec    %ax        /* 0x7fff */
   5:   70 05                   jo c              /* taken  */
   7:   04 40                   add    $0x40,%al
   9:   83 e0 60                and    $0x60,%ax
   c:   c3                      ret               /* 0x7fff */

In 32 bits, this code is interpreted as the following:

   0:   31 c0                   xor    %eax,%eax   /* 0x00000000 */
   2:   b4 80                   mov    $0x80,%ah   /* 0x00008000 */
   4:   48                      dec    %eax        /* 0x00007fff */
   5:   70 05                   jo c               /* not taken  */
   7:   04 40                   add    $0x40,%al   /* 0x00007f3f */
   9:   83 e0 60                and    $0x60,%eax  /* 0x00000020 */
   c:   c3                      ret

In 64 bits, this code is interpreted as the following:

   0:   31 c0                   xor    %eax,%eax   /* 0x00000000 */
   2:   b4 80                   mov    $0x80,%ah   /* 0x00008000 */
   4:   48 70 05                rex.W jo c         /* not taken  */
   7:   04 40                   add    $0x40,%al   /* 0x00008040 */
   9:   83 e0 60                and    $0x60,%eax  /* 0x00000040 */
   c:   c3                      ret

Python 2.7, 49 bytes

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

Bash, 25 17 bytes

getconf LONG_BIT

Thanks to Dennis for golfing help.

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.

Shell, 26 bytes

uname -m|awk \$0=/_/?64:32

Go, 94 bytes

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

x86 machine code, 12 bytes

8c c8 83 f8 23 b0 20 75 02 00 c0 c3

Ungolfed:

getKernelBitness:
    mov eax,cs
    cmp eax,0x23 ; 32 bit process on 64 bit kernel has this selector in CS
    mov al,32
    jne kernelIs32Bit
    add al,al    ; return value in eax
kernelIs32Bit:
    ret

This function works in Linux when used in ELF32, following i386 SysV ABI, as well as in Windows/Wine when used in PE32, following stdcall calling convention.

Python 3, 77 84 71 59 bytes

-13 bytes, thanks to @JonathanAllan!
Down to 59 by @Clearer

from platform import*;print({'4':64,'6':32}[machine()[-1]])

Try it online!

My fist time code-golfing :)
Should output the correct version even when running 32Bit-Python on 64bit-OS.
Assuming platform.machine() gives i*86 or x86 for 32Bit-OS. I don't have one available to check this. Output is 0 when OS is not in 64/32Bit
Edit: Added print statement, so it got 7 bytes longer

Ruby, 10 bytes

p 0.size*8

While Ruby can use integers of any length, internally it stores the values that fit in a machine word as Fixnum. The method Fixnum#size always return the length in bytes of a machine word.

The Fixnum class was removed in Ruby 2.4.0, its functionality was included in class Integer. The code stands.

PHP, 18 Bytes

<?=PHP_INT_SIZE*8;

This correctly handles all of the cases of 32, 64 and other bit CPUs provided that PHP_INT_SIZE is correct, it will show the precise size of the CPU no matter what CPU PHP is running on!

If PHP is running on

32-bit OS PHP_INT_SIZE == 4,

64-bit OS PHP_INT_SIZE == 8,

16-bit OS PHP_INT_SIZE == 2 (theoretically)

8-bit OS PHP_INT_SIZE == 1 (again theoretically)

128-bit OS PHP_INT_SIZE == 16 (Not yet achieved but possible)

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.

PHP, 29 bytes

<?=@php_uname(m)[-1]-4?32:64;

https://3v4l.org/Y6JXv

Java 7, 77 71 bytes

A java pointer-size solution

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

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());

Windows CMD, 56 52 bytes (thanks Bob!)

if EXIST "%ProgramFiles(x86)%" (echo 64)else echo 32

Still surprisingly lengthy - longest so far!

Swift 4 REPL/Playground, 12 bytes

Int.bitWidth

Int is word sized, acting like either Int32 or Int64 depending on the system.

julia, 20 17 16 bytes

n->Sys.WORD_SIZE

*Thanks to @LyndonWhite saved 3 bytes *Thanks to @RomanGräf saved a byte

Previous answers:

()->Sys.WORD_SIZE
print(Sys.WORD_SIZE)

Try it online!

PowerShell, 17 52 bytes

try{32+32*((gci \*`))-or(arch)[-1]-eq52)}catch{32}

Returns 64 if either of the following is true:

The try-catch is intended to circumvent the error you get if gci returns nothing and you don't have an arch. I haven't found a shorter way to do it so far. gci is used over ls because on Linux, ls will produce a visible error.

This version should detect whether the OS is 64-bit rather than just PowerShell, and is tested to work on Windows and Linux. For Mac support, replace arch with uname -m.

Previous Windows-only version: -!(ls \*`))*32+64

Perl, 18 15 18 bytes

say length pack p8

Perl 6, 17 bytes

say $*KERNEL.bits

Try it


There is a related $?BITS which contains the number of bits that a native int has in the runtime.

say $?BITS

Try it

C, Win32 API, 103 183 bytes

#include <windows.h>
typedef int(WINAPI*F)(HANDLE,int*);b;main(){F f=GetProcAddress(GetModuleHandle("kernel32"),"IsWow64Process");return(f!=0&&f(GetCurrentProcess(),&b)&&!b)?32:64;}

Actually there are more than 2 cases here. Let's examine them

For the next two cases we need to have the knowledge that our binary will be a 32 bit executable. And this description of what will be out into the out parameter of IsWow64Process

A pointer to a value that is set to TRUE if the process is running under WOW64. If the process is running under 32-bit Windows, the value is set to FALSE. If the process is a 64-bit application running under 64-bit Windows, the value is also set to FALSE.

That leaves us with two additional cases:

We don't wory about the part where a 64-bit application on a 64-bit Windows yields FALSE. As we know that our application is 32-bit

Oh and there is one additional case that is not covered by this challenge and should be rare anyways:

This should cover most Windows NT Operating Systems. Have only tested on Win10 64-Bit, Win 7 64-Bit, Win 8.1 32-Bit and WinXP SP1 32-Bit


Original answer:

#include<windows.h>
main(){return GetProcAddress(GetModuleHandle("Kernel32"),"IsWow64Process")?64:32;}

To be sure we need to distinguish only 2 cases

The actual value provided by IsWow64Process is irrelevant for this challange, since we want the binary to be 32bit in any case.

Unlike most of the answers, this doesn't rely on the binary itself being compiled on the machine that it's executed on.

If I knew a shorter function that is present only on 64bit and not 32bit machines, I could shorten the answer.

JavaScript (Node.js), 24 bytes

_=>process.arch.slice(1)

This is a function and returns '32', '64', or if neither 'rm'.

PHP, 25 Bytes

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

C# (29 bytes)

Console.Write(IntPtr.Size*8);

SAS, 28

%put%eval(&syssizeoflong*8);

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

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.

R, 16 bytes

.Machine[[18]]*8

Returns the pointer size.

PowerShell, 16 bytes

8*[IntPtr]::Size

Gets the pointer size in bytes, multiplies by 8 to get bits.

Java 8, 45 bytes

()->System.getProperty("sun.arch.data.model")

Ruby, 22 bytes

p [?a].pack(?p).size*8

["any string"].pack("p") returns a string whose bytes correspond to the pointer that pointed towards "any string", and is 8 characters if the OS is 64-bit, or 4 characters if the OS is 32-bit.

C, 33 31 29 23 bytes

f(){return sizeof&f*8;}

Thanks to commenters @ceilingcat and @Dennis for golfing tips!

Java, 50 bytes

int b(){return com.sun.jna.Native.POINTER_SIZE*8;}

Python 2, 52, 48, 42 41 bytes

from struct import*;print calcsize("P")*8

Thanks to totallyhuman!

Mathematica, 17 bytes

$SystemWordLength