| Bytes | Lang | Time | Link |
|---|---|---|---|
| 040 | Tcl | 170619T212713Z | sergiol |
| 043 | PowerShell | 201221T050109Z | mazzy |
| 056 | Powershell | 201220T051055Z | wasif |
| 017 | Perl 6 | 190320T050213Z | bb94 |
| 016 | TXR Lisp | 170622T182812Z | Kaz |
| 051 | Batch | 170620T082953Z | stevefes |
| 008 | x86 machine code | 170622T153023Z | Ruslan |
| 008 | x86 machine code | 171111T022207Z | peter fe |
| 065 | VB.NET Mono | 170621T012549Z | Taylor R |
| 013 | Julia | 170620T042639Z | Frames C |
| 050 | C# | 170620T061454Z | kakkarot |
| 008 | J | 170621T114444Z | Dan Bron |
| 036 | C | 170620T104453Z | Tamoghna |
| 022 | C | 170620T024427Z | user7091 |
| nan | 170620T221116Z | hildred | |
| 013 | x86 Assembly polyglot | 170621T055333Z | Obsequio |
| 049 | Python 2.7 | 170624T115040Z | Koishore |
| 017 | Bash | 170623T184112Z | notagull |
| nan | 170620T131612Z | user2428 | |
| 026 | Shell | 170620T014412Z | Zombo |
| 094 | Go | 170622T182926Z | Avdept |
| 012 | x86 machine code | 170620T193947Z | Ruslan |
| 059 | Python 3 | 170620T092410Z | tOmAtE |
| 010 | Ruby | 170622T085406Z | axiac |
| 018 | PHP | 170621T155628Z | Bja |
| 047 | Powershell | 170621T092717Z | Callie J |
| 029 | PHP | 170620T033506Z | Petah |
| nan | 170621T064246Z | Twometer | |
| 038 | Javascript ES6 | 170620T122154Z | Hankreco |
| 052 | Windows CMD | 170620T043803Z | Ken Y-N |
| 012 | Swift 4 REPL/Playground | 170621T005153Z | Alexande |
| 016 | julia | 170619T202155Z | rahnema1 |
| 1752 | PowerShell | 170620T185009Z | Andy C. |
| 018 | Perl | 170620T093233Z | Grimmy |
| nan | 170620T183425Z | Brad Gil | |
| 183 | C | 170620T145407Z | MrPaulch |
| 024 | JavaScript Node.js | 170619T204317Z | Downgoat |
| 025 | PHP | 170620T114538Z | Mr. Alie |
| 029 | C# | 170620T081026Z | James B |
| 028 | SAS | 170620T094619Z | user3490 |
| 020 | APL Dyalog | 170620T073222Z | Adá |
| 016 | R | 170620T070431Z | Sven Hoh |
| 016 | PowerShell | 170620T015427Z | Chirishm |
| 045 | Java 8 | 170619T201752Z | Daniel |
| 022 | Ruby | 170619T223238Z | Value In |
| 023 | C | 170619T210636Z | Govind P |
| 050 | Java | 170619T213432Z | Jeff I |
| 041 | Python 2 | 170619T201554Z | Daniel |
| 017 | Mathematica | 170619T202115Z | ZaMoC |
Tcl, 40 bytes
puts [expr 8*$tcl_platform(pointerSize)]
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
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):
- Install
wineusingsudo apt-get install wine - Run
wine cmdin the terminal - Call the script:
scriptname.bat
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:
- Anon function, taking anything (including
nothing) returning an integer 32, or 64 - Integer literals are of type
Intwhich depending if 32 bit or 64 bit, is either anInt32or anInt64(the1could be any digit) - placing a number before a function call does a juxtaposition multiplication
- This is basically a minified version of the code for
Sys.WORD_SIZE, used in rahnema1's answer
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]])
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.
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)
PowerShell, 17 52 bytes
try{32+32*((gci \*`))-or(arch)[-1]-eq52)}catch{32}
Returns 64 if either of the following is true:
- You have a directory on your current drive with a filename ending in a close paren, the idea being to detect
Program Files (x86). archreturns a string ending in4(ASCII 52), likex86_64, as opposed to e.g.i686.
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
There is a related $?BITS which contains the number of bits that a native int has in the runtime.
say $?BITS
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
- The easiest:
IsWow64Processdoes not exist: We are on a 32 bit OS
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:
IsWow64Processexists, and yields TRUE -> We are on a 64 bit machineIsWow64Processexists, and yields FALSE -> We are on a 32 bit machine
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:
- IsWow64Process exists, but it fails: We default to 32-bit machine.
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
IsWow64Processdoes not exist in kernel32.dll => We are on a 32 bit machine.IsWow64Processdoes exist => We are on a 64 bit machine.
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
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