Is my OS 32-bit or 64-bit?

53

10

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!

tuskiomi

Posted 2017-06-19T20:12:03.273

Reputation: 3 113

What does "You can assume that users will use 64 bit software whenever possible" mean? What software are you referring to? – NobodyNada - Reinstate Monica – 2017-06-20T02:44:18.823

5So "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 – 2017-06-20T04:33:58.413

"must be able to run on Windows 4.10 or newer" - logical OR (4.10 OR newer)? – TessellatingHeckler – 2017-06-20T06:12:22.823

2Many 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 – 2017-06-20T08:54:01.160

15What 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 – 2017-06-20T09:47:30.670

Is there anything neither 32-bit nor 64-bit? Are you talking about ARM? – Matthew Roh – 2017-06-20T10:47:39.590

What about a 32-bit operating system running on a 64-bit CPU? – OrangeDog – 2017-06-20T11:07:24.420

1

@SIGSEGV there are loads of 8-bit embedded systems, and some old systems you might get a qualifying OS to run on.

– OrangeDog – 2017-06-20T11:10:45.287

1Please note that your code running on non-binary systems still has to print a non-empty string. – Jonny Best – 2017-06-20T20:35:45.150

@JonnyBest we need a "Try it online" for nonbinary decimal computer (MIX?)

– ceilingcat – 2017-06-20T22:29:58.400

13

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 – 2017-06-21T02:25:40.420

1

@CodyGray Re: an answer that only runs on Windows 98

– Mego – 2017-06-21T02:53:05.293

Doesn't seem to apply, @Mego. That says "a language's features". Windows 98 isn't a language, it is a target platform condoned by the official rules of the challenge (which, because stated explicitly, seem like they would override any "loopholes forbidden by default" anyway). – Cody Gray – 2017-06-21T02:57:57.900

1@CodyGray That would still clearly be in violation of the spirit of the loophole. Besides, a program that only runs on Windows 98 wouldn't be valid: To be eligible for entry, your code must be able to run on Windows 4.10 or newer, and at least one flavor of Linux of your choosing (so long as that flavor is free). – Mego – 2017-06-21T02:59:07.397

1You mean I can't write a challenge that has explicit provisions that override the default loopholes, @Mego? That isn't what "default" means. And yes, I'd have to also find a Linux distribution that was 32-bit only and free. Or, I'd pick a free Linux distro that is 64-bit only (and there are a bunch of 'em), then the challenge is just detecting whether Linux or Windows. That goes beyond the point my original comment was trying to make, which is: why specify a family of 32-bit only operating systems as an acceptable minimum target, when the challenge is to detect bitness? Seems nonsensical. – Cody Gray – 2017-06-21T03:07:19.670

3You 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 – 2017-06-21T03:08:43.573

@CodyGray The "Windows 98 Resource Kit" published by Microsoft Press in 1998 refers to windows 98 as 2 different releases, depending on the year. 4.10.1998 and 4.10.2222. I omitted the last part as I didn't think it would matter. – tuskiomi – 2017-06-21T03:12:25.457

1I'm not sure why you have an OS restriction at all, but requiring answers to work on Windows 98 would probably invalidate most of the answers. – Dennis – 2017-06-21T03:21:27.260

There's really no point in including Windows 98 in the mix. It didn't have a 64-bit version anyway and, more importantly, nobody has it anymore and testing whether an answer is valid or not becomes that much harder. The verifiability issue persists, to a lesser degree, for Windows XP (16 years old) and Vista (nobody liked it in the first place). – Dennis – 2017-06-21T03:27:40.407

@LyndonWhite correct. – tuskiomi – 2017-06-25T00:58:27.807

1@tuskiomi maybe you could add something more explict, like in my comment to the question then. You can see in lots of comments that there is lots of confusion about this. – Lyndon White – 2017-06-25T01:24:06.753

@SIGSEGV the PowerMac G4 had a 128bit processor. – Skyler – 2017-06-26T23:52:42.187

As soon as you add the "print something" requirement, you essentially rule out OS-independence apart from scripting languages, but then it seems that most of the answers don't handle the "neither 32- nor 64-bit" case because the corresponding language doesn't exist on those platforms anyway. – peter ferrie – 2017-11-11T23:48:49.977

Answers

44

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

ObsequiousNewt

Posted 2017-06-19T20:12:03.273

Reputation: 836

This is actually probably in the lead, very nice – tuskiomi – 2017-06-21T16:36:38.580

1A very neat idea, but as implemented, this will return the wrong value if assembled as a 32-bit binary and run on a 64-bit operating system. – Cody Gray – 2017-06-21T17:26:45.057

1@CodyGray from the rules: You can assume that users will use 64 bit software whenever possible. I suppose this also means that for functions we can assume that the caller code is 64 bit whenever possible. – Ruslan – 2017-06-22T19:30:42.553

Ah, I suppose that's a fair interpretation, @Ruslan. And I see you have already posted the answer I had in mind, were it not for this issue. :-) You've got my upvote.

– Cody Gray – 2017-06-23T06:22:05.783

does the byte 48 represent dec %eax in 16-bit mode? – phuclv – 2017-06-24T13:50:52.883

@LưuVĩnhPhúc Fixed, thanks. – ObsequiousNewt – 2017-06-24T15:59:41.813

Can't edit because it would be too short, but there is a typo in the second listing offset 7, the result of the addition is 0x803F – Samuel Peter – 2017-06-25T14:29:42.467

@SamuelPeter shouldn't be, we're only adding to %al not %eax (which is necessary because adding to %eax would take an extra byte) – ObsequiousNewt – 2017-06-25T23:58:24.440

Why not B8 40 00 40 C3 48 24 60 98 C3 (MOV AX, 40H/INC AX/RET,MOV AX,0C3400040H/DEC AX/AND AL,60H/CWDE/RET,MOV AX,0C3400040H/AND AL,60H/CWDE/RET – l4m2 – 2017-12-09T12:03:21.883

48

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.

Ruslan

Posted 2017-06-19T20:12:03.273

Reputation: 1 283

Actually one can reduce byte count even more if following another approach.

– Ruslan – 2017-06-22T15:41:37.720

what would this theoretically do in a non 32/64 bit environment? – tuskiomi – 2017-06-26T13:35:30.680

1@tuskiomi In any case, this code will still return either 32 or 64. The only difference of interpreting this byte sequence in 16-bit mode will be a change of mnemonics from eax to ax. So, if the selector in cs happens to be 0x23, the result will be 64, otherwise 32. – Ruslan – 2017-06-26T13:44:41.367

this looks specific to two particular operating-systems, though. You could easily have a 32-bit system with cs=0x23. See instead my 8-byte answer that targets the CPU instead. – peter ferrie – 2017-11-11T23:51:09.240

@peterferrie yeah, that's why I have another answer. But yours does outgolf it by 2 bytes.

– Ruslan – 2017-11-12T06:47:12.033

32

Mathematica, 17 bytes

$SystemWordLength

J42161217

Posted 2017-06-19T20:12:03.273

Reputation: 15 931

13Of course there's a builtin! \s (+1) – tuskiomi – 2017-06-19T20:35:45.610

Shouldn't you either add an & or specify that this is in the REPL? – LegionMammal978 – 2017-06-19T20:48:14.820

5@LegionMammal978 "If the code is ran on a 32-bit operating system, print "32", if the code is ran on a 64 bit operating system, output "64". Print any (other) non-empty string of alphanumeric characters if it's neither." Not "provide a function that, when run, does this"; just "do this". – Patrick Stevens – 2017-06-19T21:41:00.867

17

@PatrickStevens: If the question doesn't specify what form a submission should take, it's a program or function, but not a snippet, by default.

– None – 2017-06-19T22:16:47.980

3@ais523 I would argue there is no distinction in Mathematica between programs, functions, and snippets. Everything is just an expression. – ngenisis – 2017-06-21T16:05:02.787

@ngenisis: Sure there is. Can you apply it to arguments? If not, it's not a function. Does it take input in its own right from the user, standard input, etc.? If not, it's not a full program. (In fact, the fact that Mathematica allows you to export files that anyone can view using a separate viewer program makes it quite clear that full programs exist, and that they're different from arbitrary expressions, because in that situation a snippet wouldn't be able to take input at all.) – None – 2017-06-22T02:48:47.780

@ais523 I made a meta post for us to discuss this further, as this is something that's bugged me for a while. IMO, this is a full program, not a snippet, and it outputs via its return value.

– ngenisis – 2017-06-22T16:49:33.477

@PatrickStevens Functions may return a function that returns the result.

– AJMansfield – 2017-06-25T22:14:16.810

21

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 Int which depending if 32 bit or 64 bit, is either an Int32 or an Int64 (the 1 could 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

Lyndon White

Posted 2017-06-19T20:12:03.273

Reputation: 1 021

You could use a dummy argument: https://codegolf.meta.stackexchange.com/a/12696/56341

– Roman Gräf – 2017-06-20T19:28:09.027

21

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.

hildred

Posted 2017-06-19T20:12:03.273

Reputation: 1 329

I'm not sure if you can tell the os resolution with a boot loader, but i like the idea – tuskiomi – 2017-06-20T22:13:51.143

It checks the cpu and is used to select the right operating system. This is basically golfed code from my unboot project. – hildred – 2017-06-20T22:17:22.120

2So it changes the os based on the architecture. Thinking outside the box. I like. – tuskiomi – 2017-06-20T22:18:55.247

What's the bytecount? – Restioson – 2017-06-22T15:45:33.370

@Restioson, which one? The best is ipxe with 36. – hildred – 2017-06-22T15:56:14.923

@hildred could you add them as a table or something? – Restioson – 2017-06-22T16:00:16.570

@Restioson they are right after the name of the boot loader, although I don't know if there should be a penalty for multiple files for syslinux. – hildred – 2017-06-22T16:12:22.887

1@hildred I believe it would be more readable if you used ## iPXE, 36 bytes instead of ### ipxe 36 – NieDzejkob – 2017-06-22T16:45:56.617

@hildred oh haha I read that as a version number – Restioson – 2017-06-22T16:54:08.220

Should "turning complete" be "turing complete"? – styfle – 2017-06-24T18:21:36.323

@styfle, possibly. My spelling is so bad I learned to spell with the help of gcc. – hildred – 2017-06-24T19:22:12.790

19

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!

rahnema1

Posted 2017-06-19T20:12:03.273

Reputation: 5 435

1I, personally, like this one! – tuskiomi – 2017-06-19T20:30:01.730

2

By following convention: https://codegolf.meta.stackexchange.com/q/2419/62131 you can shave off 3 bytes by doing ()->Sys.WORD_SIZE

– Lyndon White – 2017-06-20T04:11:52.177

1

You could also use a dummy argument: https://codegolf.meta.stackexchange.com/a/12696/56341

– Roman Gräf – 2017-06-20T19:26:46.833

14

JavaScript (Node.js), 24 bytes

_=>process.arch.slice(1)

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

Downgoat

Posted 2017-06-19T20:12:03.273

Reputation: 27 116

+1, but I suppose a 64bit arch could be running a 32bit OS though – Octopus – 2017-06-19T22:35:49.607

1Gives me '32' on 32-bit Windows, so it does appear to work. – Ken Y-N – 2017-06-20T06:16:12.047

5You can shave off 7 bytes if you're using the REPL and os instead of process: os.arch().slice(1) – GilZ – 2017-06-20T10:21:06.413

This returns the OS architecture reported to the process, not the actual OS architecture. So this does not work on Linux. (Punch "setarch" into your favorite search engine.) – David Schwartz – 2017-06-21T23:24:55.807

@DavidSchwartz it works fine on my linux box unless I'm misunderstanding, but on PPCG we can assume an unmodified environment in terms of user changing the arch – Downgoat – 2017-06-22T00:06:07.777

@Downgoat But there seem to be specific rules here that override that general assumptions permitted. "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." (Which, honestly, I'm having a hard time making sense of.) – David Schwartz – 2017-06-22T00:09:40.673

13

C, 33 31 29 23 bytes

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

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

Govind Parmar

Posted 2017-06-19T20:12:03.273

Reputation: 828

No need for printf, you can simply return the integer. – Dennis – 2017-06-19T21:40:20.307

1@Dennis the challenge specifically says"print" in the OP – Govind Parmar – 2017-06-19T21:41:25.557

13It also says that the usual code golf rules apply, and returning from a function is part of our defaults for I/O. – Dennis – 2017-06-19T21:42:43.380

@Dennis Well in that case thanks for the tip! – Govind Parmar – 2017-06-19T21:47:18.767

11Isn't that going to depend on whether you compile as a 32 or 64 bit program? – Ken Y-N – 2017-06-20T04:35:10.410

@KenY-N, you're right; this returns 32 when compiled with the -mx32 option in gcc. x32 mode is only available on x86_64 processors. – ceilingcat – 2017-06-20T05:30:12.690

1it also won't work if CHAR_BIT != 8 – phuclv – 2017-06-20T05:48:09.383

Since this only needs to work on some systems, can you just assign the result to a variable (so it's on top of the stack) instead of returning it? – k_g – 2017-06-21T06:06:19.420

4This doesn't comply with the requirements. "Please note that a 32 bit program running on a computer with a 64 bit operating system should output "64"." This tells you how the program was compiled, not what the OS is. – David Schwartz – 2017-06-21T23:24:04.500

2@DavidSchwartz "You can assume that users will use 64 bit software whenever possible." – Klas Lindbäck – 2017-06-22T12:16:38.047

2

Sorry to say that but sizeof is evaluated at compile time. So if you compile a 32 bit exe and run it on a 64 bit machine, it will ouput 32 while it should output 64 (https://stackoverflow.com/questions/2615203/is-sizeof-in-c-evaluated-at-compilation-time-or-run-time). Nice idea though !

– Dinaiz – 2017-06-23T01:14:08.390

11

PowerShell, 16 bytes

8*[IntPtr]::Size

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

Chirishman

Posted 2017-06-19T20:12:03.273

Reputation: 389

-1 because your code must be able to run on Windows 4.10 or newer – ub3rst4r – 2017-06-20T05:06:57.280

12@ub3rst4r or newer, not and newer. I'm willing to bet a good chunk of these answers are difficult, at best, to run on a long-unsupported version of Windows. It's also unreasonable to expect people to test on 98 of all OSes - it's not even easy to install in a VM these days (trust me, I've tried. Not gonna run on KVM, and it has interesting interactions with VBox display adapters). (Nor does it even have a concept of 64-bit... that's a truly ridiculous requirement.) – Bob – 2017-06-20T06:37:08.713

1@ub3rst4r I doubt that all of the interpreted programming languages here have an implementation working on Windows 4.10! – Martin Rosenau – 2017-06-20T08:51:14.380

1This looks like it fails if it's a 32-bit powershell on a 64-bit OS. – Chris J – 2017-06-21T09:28:33.973

@ChrisJ At least on Win10 and Win2012 R2 it returns the proper result from x86 PowerShell. Haven't tested on Linux. – Booga Roo – 2017-06-21T18:29:18.013

@BoogaRoo - I don't get that on Win10 x64 with x86 PS: [environment]::is64bitoperatingsystem returns true; [environment]::is64bitprocess returns false; 8*[intptr]::size returns 32. Wonder if PS version matters (although I think 10 comes with v5 regardless). – Chris J – 2017-06-22T09:17:07.677

Does this work in Linux? – stevefestl – 2017-06-27T08:53:38.160

10

Python 2, 52, 48, 42 41 bytes

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

Thanks to totallyhuman!

Daniel

Posted 2017-06-19T20:12:03.273

Reputation: 6 425

zoom! across the line in under a minute. – tuskiomi – 2017-06-19T20:16:25.573

I'm going to hold off on testing this until i'm home, but i'm not sure this prints out "32" and "64", but other cases i'm confident. – tuskiomi – 2017-06-19T20:21:15.490

@tuskiomi, this prints "32bit" or "64bit" – Daniel – 2017-06-19T20:21:52.807

ahh, may want to wrap that in a left([arg],2) operator :-) – tuskiomi – 2017-06-19T20:23:03.303

@tuskiomi done :) – Daniel – 2017-06-19T20:23:50.247

I think __import__ will be shorter – shooqie – 2017-06-19T20:35:40.850

@shooqie, thanks! I thought about doing that but for some reason I thought it would be longer. Luckily I was wrong – Daniel – 2017-06-19T20:40:34.770

1from struct import*;print calcsize("P")*8 is shorter. – totallyhuman – 2017-06-19T20:42:38.840

12If you run this on 32-bit Python in a 64-bit OS, I think this will lie to you. Most other answers also seem sensitive to that anyways... – Nick T – 2017-06-19T22:02:56.577

As far as I can see, this gives a wrong result when running as a 32-bit binary on a 64-bit OS since it's calculating the pointer-size, which is 32 bits. – DarkDust – 2017-06-20T11:34:45.013

@NickT I confirm that it DOES lie! – Alex.S – 2017-06-21T13:32:47.390

Not sure what the protocol is, but I have an alternate solution (with the same weakness; it identifies the interpreters bittedness, not the OS's directly) that shaved off another 13 bytes.

– ShadowRanger – 2017-06-23T03:18:48.790

10

Java 8, 45 bytes

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

Daniel

Posted 2017-06-19T20:12:03.273

Reputation: 6 425

@Dopapp What (s)he means is that this is a snippet, not a program or function. In Java 8, this should work: n=>System.getProperty("os.arch") – NoOneIsHere – 2017-06-19T20:25:19.280

@NoOneIsHere, oh thank you that went way over my head – Daniel – 2017-06-19T20:26:38.900

Ok, this works now. Just mention that it uses a null parameter n. – NoOneIsHere – 2017-06-19T20:26:41.367

@NoOneIsHere, I thought lambdas were ->, so it would be n=()->...? – Daniel – 2017-06-19T20:38:21.630

@Dopapp afaik it's ()-> or something like that in Java, _=> is JavaScript – Stephen – 2017-06-19T20:42:40.007

@StephenS, yeah that's what I though. Oh well, it's just one byte more. – Daniel – 2017-06-19T20:44:50.830

@Dopapp You can do n-> to save a byte, just mention it's a null parameter. n-> should work in Java too. I'm just not a Java programmer, so I am endlessly failing to compile the program. – NoOneIsHere – 2017-06-19T20:52:50.510

@NoOneIsHere, are you sure that that is a thing in Java? I have never seen n like that in Java (granted I don’t use Java that much). I’ll check when I get back to a computer. – Daniel – 2017-06-19T20:56:36.097

@Dopapp https://stackoverflow.com/a/25055739/4807393. I know I chose a bad answer, but the code uses e -> { sum += e.size(); }

– NoOneIsHere – 2017-06-19T21:09:44.160

2"If the code is ran on a 32-bit operating system, print "32", if the code is ran on a 64 bit operating system, output "64". Print any (other) non-empty string of alphanumeric characters if it's neither.". This doesn't do that at all... – Olivier Grégoire – 2017-06-19T22:54:24.650

By the way, the code as written now (()->System.getProperty("os.arch")) is a valid syntax. It just doesn't do what is expected (printing "32", "64" or any other non-empty string). – Olivier Grégoire – 2017-06-19T23:04:27.663

Oh, and "sun.arch.data.model"returns exactly what is needed. Put it in a lambda for full correct stuff: ()->System.getProperty("sun.arch.data.model") (45 bytes). Codegolf doesn't require your answer to be fully portable: only that it works on some systems. So just a word saying it only works on Oracle JDK is enough. I don't think you'll be able to give a proper answer in less bytes using "os.arch". – Olivier Grégoire – 2017-06-19T23:21:20.090

@OlivierGrégoire, ah so the first version did work! Thanks for your diligence! – Daniel – 2017-06-19T23:26:49.793

4Can you get Java 8 for Windows 98 (v4.10) ? – TessellatingHeckler – 2017-06-20T06:11:30.027

4@TessellatingHeckler Java 8 requires "i586" (whatever that means). The entry says: "your code must be able to run on Windows 4.10 or newer", not "your code must be able to run on Windows 4.10 and newer". It runs on a Windows newer. – Olivier Grégoire – 2017-06-20T07:44:33.913

@OlivierGrégoire It doesn't do what's needed. It reports the OS architecture provided to this process, not the actual, native OS architecture. These are not always the same on Linux. (Punch "setarch" into your favorite search engine.) – David Schwartz – 2017-06-21T23:26:11.197

9

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

Brad Gilbert b2gills

Posted 2017-06-19T20:12:03.273

Reputation: 12 713

8

Windows CMD, 56 52 bytes (thanks Bob!)

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

Still surprisingly lengthy - longest so far!

Ken Y-N

Posted 2017-06-19T20:12:03.273

Reputation: 396

I think you can actually get it shorter by checking for the existence of %ProgramFiles(x86)% – Bob – 2017-06-20T04:46:59.613

@Bob that didn't work! echo %thisdoesntexist% echoes %thisdoesntexist%, and DEFINED or EXISTS is longer than the ==x86. – Ken Y-N – 2017-06-20T04:57:30.083

You're forgetting the length of the variable itself. %ProgramFiles(x86)% is shorter than %PROCESSOR_ARCHITECTURE%. Also, the second set of parentheses is unnecessary. – Bob – 2017-06-20T05:03:48.500

Ahh, oops! Indeed I am. Let me update. – Ken Y-N – 2017-06-20T05:05:14.280

1Actually, %windir%\SysWOW64 is even shorter, I think... – Bob – 2017-06-20T05:07:44.837

We probably need quotes around %windir%, though, just in case it is installed to c:\W I N D O W S, taking us back to 52. – Ken Y-N – 2017-06-20T05:13:02.093

if EXIST %windir%\SysWOW64 (echo 64)else echo 32 -- you could in theory check other files or shorten further with wildcards like %windir%\*64 but I feel that might be a bit too localised..? – Bob – 2017-06-20T05:13:39.907

I think assuming %windir% is C:\Windows is perfectly safe and allowed. There's a certain amount of assumptions allowed in codegolf, and a default OS install should be included :) I've also not heard of being able to change the Windows install directory since the days of 9x. But even if you insist on quotes, it's still shorter than %ProgramFiles(x86)% by 2 chars. – Bob – 2017-06-20T05:14:46.883

7"must be able to run on Windows 4.10 or newer, and at least one flavor of Linux of your choosing" - which flavour of Linux for this? – TessellatingHeckler – 2017-06-20T06:13:06.370

1@TessellatingHeckler if you install Wine...? – Ken Y-N – 2017-06-20T06:14:58.540

1What about using shorthand? if EXIST "c:\Progra~3" (echo 64)else echo 32 By default you have ProgramData and Program Files so if a third exists, we should be 64 – Marie – 2017-06-20T20:09:33.300

@Marie, only problem with that is if I make a directory called "ProgramsInAnotherFolder" on 32-bit Windows… – Jase – 2017-06-20T23:39:44.113

Bob's suggestion to use %windir%\SysWOW64 seems fine. We can assume %windir% doesn't have spaces or special characters, because otherwise, so many other things would break on the system. Thus, we shorten from 52 to 48 bytes. – Gras Double – 2017-06-21T03:50:44.853

3What will this print for an 8-bit OS? – tuskiomi – 2017-06-21T13:43:21.423

@TessellatingHeckler Works fine in the Ubuntu flavour, if you use the SysWOW64 detection: https://gist.github.com/BobVul/5e5022d5b62b9ef39d149a595ab338a6 -- added bonus, the 32-bit WINE on 64-bit Linux still prints 64 just as it would in Windows, and correct according to question spec.

– Bob – 2017-06-22T06:33:28.043

Instructions unclear, 'echo' is not recognized as an internal or external command, operable program or batch file. – YoYoYonnY – 2017-06-22T15:49:43.480

@Jase Doesn't the same problem manifest with the posted solution if you were on Win32 and had created a folder called "Program Files (x86)"? – tjbtech – 2017-06-24T18:56:25.137

Hey, how about "%ProgramW6432%" instead of %windir%\SysWOW64? Saves one byte. – Gras Double – 2017-06-25T04:48:02.290

"%ProgramW6432%" doesn't exist in Windows XP. But it saves one byte! – Gras Double – 2017-06-25T04:55:49.937

@tjbtech yep, definitely. I was just pointing out that this answer relies on the root drive only containing the default Windows folders. – Jase – 2017-06-25T14:16:13.747

7

C#, 60 50 bytes

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

Thanks @TheLethalCoder

kakkarot

Posted 2017-06-19T20:12:03.273

Reputation: 269

1Welcome to the site! :) – James – 2017-06-20T06:22:15.203

4Save 4 bytes if you remove 'Line' – John – 2017-06-20T10:58:52.327

1can also remove 'Environment.' and 'console.' assuming use of using static System.Environment; and using static System.Console; – John – 2017-06-20T11:00:43.563

1also, change WriteLine to Write – Thomas Ayoub – 2017-06-20T13:20:08.060

7@John Last time I checked, imports must be included and so would add to the byte count – Ceshion – 2017-06-20T18:02:25.130

This is a code snippet and you should add in using System; as well. However, if you compile to an anonymous method you can get it to 50 bytes: _=>System.Environment.Is64BitOperatingSystem?64:32 – TheLethalCoder – 2017-06-29T14:59:05.570

@TheLethalCoder Thanks! – kakkarot – 2017-06-30T05:12:10.387

7

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: IsWow64Process does 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:

  • IsWow64Process exists, and yields TRUE -> We are on a 64 bit machine
  • IsWow64Process exists, 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

  • IsWow64Process does not exist in kernel32.dll => We are on a 32 bit machine.

  • IsWow64Process does 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.

MrPaulch

Posted 2017-06-19T20:12:03.273

Reputation: 771

2Drop the .dll from the module name string. Not only is this good for golfing, but it's actually a better practice in normal use, too. Also, for golfing purposes, you can drop the space after #include. – Cody Gray – 2017-06-20T15:00:31.817

Thanks, I actually wanted to reduce the whole thing by just looking for a module that can be found on 64bit but not on 32bit. I'm still looking. Sadly (for this purpose) there is no module called WoW64. – MrPaulch – 2017-06-20T15:03:49.770

There is actually a wow64.dll and Wow64Win.dll, but I have never tried to call GetModuleHandle with those. The problem, though, is that they would only be loaded for a 32-bit process running on a 64-bit machine, not for a 64-bit process running on a 64-bit machine. – Cody Gray – 2017-06-20T15:05:53.630

I have. They return 0 even on a 64bit machine. Makes sense actually. They don't exist to be linked with directly. The system takes care of the redirection during runtime. – MrPaulch – 2017-06-20T15:07:15.217

Ooh, problem though. From the SDK documentation: "Note that this technique is not a reliable way to detect whether the operating system is a 64-bit version of Windows because the Kernel32.dll in current versions of 32-bit Windows also contains this function." – Cody Gray – 2017-06-20T15:08:43.540

I had feared that. I was however convinced by some post on SO, that it will never be found on a 32bit machine. Ok, now I will have to call the function itself. – MrPaulch – 2017-06-20T15:10:08.893

Probably an old post that you found on SO. I would have thought it wasn't on 32-bit systems, either, but my experience on this is all pretty old. Windows 8 changed a lot of things, and 10 changed even more. Calling it is going to be a lot more difficult and really ratchet that byte-length up. Perhaps better off checking for environment variables, or the existence of a "Program Files (x86)" folder. Or, maybe limit yourself to a particular, older version of Windows where this isn't there in 32-bit builds, like Windows XP? Unclear to me from the challenge whether this is OK. – Cody Gray – 2017-06-20T15:12:38.710

Let us continue this discussion in chat.

– MrPaulch – 2017-06-20T15:46:25.953

but which linux is this compatible with? – tuskiomi – 2017-06-21T13:42:52.997

None. At least not natively. Maybe with wine. Hi ever I don't know whether eine reflects the bigness of its host system or its own... – MrPaulch – 2017-06-21T13:46:39.367

6

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.

Value Ink

Posted 2017-06-19T20:12:03.273

Reputation: 10 608

Since this is using the pointer size, this will print 32-bit when the Ruby interpreter is a 32-bit binary on a 64-bit OS. So misses a rule. – DarkDust – 2017-06-20T11:36:54.590

1@DarkDust You can assume that users will use 64 bit software whenever possible. So 64-bit users will be assumed to be running 64-bit Ruby. – Value Ink – 2017-06-20T17:12:00.497

6

R, 16 bytes

.Machine[[18]]*8

Returns the pointer size.

Sven Hohenstein

Posted 2017-06-19T20:12:03.273

Reputation: 2 464

1Like 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 – 2017-06-20T11:39:11.340

1@DarkDust R is interpreted, and we can assume the user uses 64-bit software where possible, so also a 64-bit interpreter. IMHO, the rule only applies to compiled languages – NieDzejkob – 2017-06-22T16:39:47.477

@NieDzejkob That is the exact same logic I have used for my answer in PHP when someone else put the same type of comment in response to my answer, I wish others read the same from the question as we have on assuming users will use 64-bit software where possible with interpreted languages. – Bja – 2017-06-24T08:03:18.953

6

Perl, 18 15 18 bytes

say length pack p8

Grimmy

Posted 2017-06-19T20:12:03.273

Reputation: 12 521

I get 64 on my 32 bit computer, because perl was built with a 64bit IV. You'll find this is usually the case when running on 32bit version of Windows. – Brad Gilbert b2gills – 2017-06-20T18:39:55.400

@BradGilbertb2gills I reverted the code to the previous version; this one should work even in this case. Let me know if it still doesn’t work, I’ll delete my answer. – Grimmy – 2017-06-20T18:54:01.150

1That works correctly. – Brad Gilbert b2gills – 2017-06-20T19:21:59.330

Seems wrong on HP 9000/785. Gives 32. But I do not think there were any 32-bit HP/PA-RISC CPUs. – Ole Tange – 2017-06-25T12:55:44.873

6

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.

Ruslan

Posted 2017-06-19T20:12:03.273

Reputation: 1 283

While this is an interesting approach, how can it be written into a program in such a way that it can actually be executed? Every method I can think of executing binary code other than writing your own loader function uses files that are specific to 64-bit or 32-bit code. Therefore, you'll need two copies of this code in order to actually run... – Jules – 2017-06-24T22:48:02.673

@Jules You can put it as inline assembly into a high-level language program, like here. Then you just need to use your platform-native compiler with its default options, and you'll get the bitness.

– Ruslan – 2017-06-25T05:52:35.457

@l4m2 why do you yell at me? But you're right anyway, these two bytes seem to be some junk left from my experiments. Will remove. – Ruslan – 2017-12-09T12:23:28.807

5

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)

Bja

Posted 2017-06-19T20:12:03.273

Reputation: 151

2that constant has the size of the OS where PHP was built, not run – Einacio – 2017-06-22T01:44:02.653

2@Einacio Yes, but in the initial challenge the poster wrote "You can assume that users will use 64 bit software whenever possible", Therefore if the OS is 64-bit then the version of PHP running on the OS would have to be assumed to be 64-bit! (Note: I don't see this as a loophole just pure logic based off of the initial challenge.) – Bja – 2017-06-22T07:26:56.237

4

Shell, 26 bytes

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

Steven Penny

Posted 2017-06-19T20:12:03.273

Reputation: 142

You can use arch instead of uname -m. – Dennis – 2017-06-20T06:29:15.957

1@Dennis Not necessarily. On macos: uname -m -> x86_64, but arch -> i386. Because macos :( Also, this is bash-specific - fails on zsh. – viraptor – 2017-06-20T09:28:17.750

@StevenPenny zsh will try to interpret the ? as a glob / single character wildcard. – Doorknob – 2017-06-20T12:43:37.153

Does this run on Windows? – Ajasja – 2017-06-20T15:03:58.897

1@Ajasja with Cygwin – Steven Penny – 2017-06-20T17:08:36.980

@viraptor this is Code Golf, not a portability competition. As long as it works on Windows and at least one free Linux distribution, as the rules say, it is OK. Otherwise I could just as easily run it with linux32/setarch i386 command and break it. – Ruslan – 2017-06-21T10:37:41.443

@Ruslan I get it, but that's no reason not to fix simple things (rename shell -> bash) – viraptor – 2017-06-21T11:22:07.777

@viraptor this works in dash and BusyBox's sh too, for example. – Ruslan – 2017-06-21T11:45:26.273

4

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

user70915

Posted 2017-06-19T20:12:03.273

Reputation:

I've been trying to understand how this works, but I'm clueless. Can you add an explaination? – NieDzejkob – 2017-06-22T16:36:59.617

@jbcreix clever... – NieDzejkob – 2017-06-23T18:03:41.987

4

C# (29 bytes)

Console.Write(IntPtr.Size*8);

James B

Posted 2017-06-19T20:12:03.273

Reputation: 41

10Like 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 – 2017-06-20T11:39:05.663

1Need to specify that this is compiled for "AnyCPU" with the "Prefer 32-bit" checkbox unchecked. – Cody Gray – 2017-06-23T17:00:09.770

4

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).
  • arch returns a string ending in 4 (ASCII 52), like x86_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

Andy C.

Posted 2017-06-19T20:12:03.273

Reputation: 141

what linux distro does this run on? – tuskiomi – 2017-06-20T18:51:44.320

@tuskiomi Oh, my bad - I see what you mean. Didn't read the challenge properly. Should I delete the answer? – Andy C. – 2017-06-20T19:17:42.950

If you know it'll work with WINE, you should keep it. – tuskiomi – 2017-06-20T19:20:10.873

@tuskiomi At the very least Ubuntu 14.04, 16.04, RHEL7, CentOS7 and some version of SUSE: https://github.com/PowerShell/PowerShell/releases/

– Xudonax – 2017-06-24T17:16:30.257

4

Swift 4 REPL/Playground, 12 bytes

Int.bitWidth

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

Alexander - Reinstate Monica

Posted 2017-06-19T20:12:03.273

Reputation: 481

Isn't this a code-snip it, so it should print? Or does saying you are using the REPL get around that restriction? – Lyndon White – 2017-06-21T01:28:57.097

1That's up to the judges. The REPL usage isn't very common, but Swift Playgrounds are very popular (especially on iPad). Personally, I think it's fair game. People design custom programming languages for golfing that implicitly print values, and this is no different – Alexander - Reinstate Monica – 2017-06-21T05:25:56.253

4

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.

axiac

Posted 2017-06-19T20:12:03.273

Reputation: 749

4

Bash, 25 17 bytes

getconf LONG_BIT

Thanks to Dennis for golfing help.

John Nunley

Posted 2017-06-19T20:12:03.273

Reputation: 41

3

Java, 50 bytes

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

Jeff I

Posted 2017-06-19T20:12:03.273

Reputation: 79

Is that a library? If yes, you should mention it! I don't see that class available in the Oracle's JDK 8 though (I don't have my usual JDK 6 and 7 at my disposal, atm). – Olivier Grégoire – 2017-06-19T23:01:51.873

2This answer is invalid. It doesn't print. – Philipp – 2017-06-20T10:42:28.920

6Quoting Dennis: > It also says that the usual code golf rules apply, and returning from a function is part of our defaults for I/O. – Asu – 2017-06-20T13:02:11.780

Change it into a lamba function for less bytes: ()->com.sun.jna.Native.POINTER_SIZE*8 – Ferrybig – 2017-06-25T12:02:20.727

3

PHP, 29 bytes

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

https://3v4l.org/Y6JXv

Petah

Posted 2017-06-19T20:12:03.273

Reputation: 147

Ooh. I like this one. How does it work? – tuskiomi – 2017-06-20T12:09:53.570

4php_uname('m') returns x86_64 on a 64 bit OS, otherwise something like i386. 4 being the 6th character (5th 0 indexed) of the string, '4' - 4 == 0 == false. And @ just suppresses warnings for unquoted strings, and uninitialised offsets. – Petah – 2017-06-20T12:27:21.757

2This detects the architecture of PHP, not of the OS. I'm running 32-bit PHP on a 64-bit Windows, php_uname('m') returns 'i586'. – Gras Double – 2017-06-21T03:58:08.913

@GrasDouble well I guess ymmv, I works for me with Windows NT USER-PC 10.0 build 15063 (Windows 10) AMD64. – Petah – 2017-06-21T09:13:50.273

I'm also getting i586 on win8.1 x64 PHP 5.5, so it may be defaulting to PHP architecture on windows (php 5.x only had x32 builds on stable) – Einacio – 2017-06-21T13:50:49.160

1@tuskiomi this will print 64 if there is an 8-bit operating system – Bja – 2017-06-21T16:33:17.833

Won't 1<<32>1 work to detect if it is 32 bits or 64 bits? – Ismael Miguel – 2017-06-22T01:00:12.437

3

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

tOmAtE

Posted 2017-06-19T20:12:03.273

Reputation: 39

Not sure if it's acceptable because it is possible to run a 32 bit OS on a 64 bit machine (or to have some machine called a foo86 that is 64 bit :p), but if it is acceptable then you can save 9 (edit ...13!) bytes with print((a==64)*a+32*(a==86)). – Jonathan Allan – 2017-06-20T14:03:43.820

from platform import*;print({'4':64,'6':32}[machine()[-1]]) would work too. – Clearer – 2017-06-21T10:40:17.167

@Clearer This would miss following constraint:
Important: Print any other non-empty string of alphanumeric characters if it's neither 32 or 64 bit..
– tOmAtE – 2017-06-22T07:41:24.680

@tOmAtE if it's neither 32 nor 64 bit, it will throw an exception, which prints a non-empty string. – Clearer – 2017-06-22T08:04:55.787

To add to what @tOmAtE said, you could use get or: {'4':64,'6':32}.get(machine()[-1],"/") (/ just for nonemptiness). It's a bit lengthy though – Restioson – 2017-06-22T15:53:50.443

2Try architecture(), saves the dict: from platform import*;print(architecture()[0][:2]) -> 50 bytes – bugmenot123 – 2017-06-23T13:19:23.270

3

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.

Tamoghna Chowdhury

Posted 2017-06-19T20:12:03.273

Reputation: 373

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 – 2017-06-20T11:39:23.913

1@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 – 2017-06-20T12:08:13.253

Does it explicitly need to have the .c extension? – Restioson – 2017-06-22T15:59:43.493

@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 – 2017-06-22T17:24:51.890

@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 – 2017-06-25T09:20:01.230

@TamoghnaChowdhury it is unnecessary to indicate a compilation penalty as it can generally be assumed that your code will be compiled and executed on the end computer – Taylor Scott – 2017-06-25T14:37:09.663

@TaylorScott, the compilation penalty has been imposed because the question states that the program will be run, not compiled and then run. It is reasonable to assume that one binary may be used without recompilation on different systems. – Tamoghna Chowdhury – 2017-06-25T16:24:49.910

@TamoghnaChowdhury, i understand your point but it is moot, compilation does not, by default, ever count towards score, but rather only non-standard compilation flags (like -ansi or -p) add to the count. this is because compilers are not always standardized and not all languages have an explicit compiler – Taylor Scott – 2017-06-25T17:14:31.413

@TaylorScott Accepted. Edited out the compilation penalty. – Tamoghna Chowdhury – 2017-06-26T05:27:30.773

3

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.

Dan Bron

Posted 2017-06-19T20:12:03.273

Reputation: 421

What does this theoretically print on a 128 bit os? – tuskiomi – 2017-06-28T17:51:04.427

@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 – 2017-06-28T17:55:11.310

Ah. Maybe a different output for 16 bit then? – tuskiomi – 2017-06-28T18:02:48.897

@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 – 2017-06-28T18:06:50.480

2

tcl, 40

puts [expr 8*$tcl_platform(pointerSize)]

demo

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

sergiol

Posted 2017-06-19T20:12:03.273

Reputation: 3 055

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 – 2017-06-19T23:00:45.193

You can probably do $tcl_platform(pointerSize)<<3, but this is bigger. Disclaimer: I don't know TCL. It works in Python anyways :) – Restioson – 2017-06-22T15:58:51.677

@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 – 2017-06-22T22:00:04.817

@sergiol sorry, I don't know TCL :p – Restioson – 2017-06-23T09:25:24.433

@Restioson: no problem – sergiol – 2017-06-23T09:26:57.290

2

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

stevefestl

Posted 2017-06-19T20:12:03.273

Reputation: 539

I get Can't recognize 'x86' as an internal or external command, or batch script. before wine cmd prints 64. – Ruslan – 2017-06-26T15:11:56.363

Weird.. did you use the same script I posted? – stevefestl – 2017-06-26T22:36:14.287

@Ruslan I've edited the script, please check does this works on 32 bit systems – stevefestl – 2017-06-26T22:39:49.280

No, still the same error message, and both current and previous versions output 64 on a 32-bit system. – Ruslan – 2017-06-27T05:28:13.980

@Ruslan Did you get the Can't recognize error message on Wine or cmd? – stevefestl – 2017-06-27T08:52:12.017

I saved the command to test.bat and ran wine cmd /c test.bat. I also tried simply running wine cmd and typing the string there, with the same result. – Ruslan – 2017-06-27T11:46:44.583

I assume this issue is caused by 2 problems. A: Wine can't handle if statement syntax properly. B: Wine does not come with the thing we need for the script to work. – stevefestl – 2017-06-27T13:07:59.530

You don't need the ()s around the echo 32. – Neil – 2018-04-05T00:50:12.390

2

PHP, 25 Bytes

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

Mr. Alien

Posted 2017-06-19T20:12:03.273

Reputation: 141

1That detects if PHP is 32 or 64 bit, not the OS. – Petah – 2017-06-20T12:55:16.153

1this will print 64 if there is an 8-bit operating system – tuskiomi – 2017-06-21T13:38:07.190

1@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 – 2017-06-21T15:28:38.600

@Bja https://codegolf.meta.stackexchange.com/a/11355/60951 if only 32 or 64 bit systems are supported, it will never print the 3rd case.

– tuskiomi – 2017-06-21T15:31:54.040

@tuskiomi Sorry I didn't know about the loophole rules in codegolf, My mistake. I have deleted my other comments. – Bja – 2017-06-21T15:37:53.997

@Bja I am not expecting it to compile into 8-bit code, but that it should theoretically work if compiled for 8-bit machines. – tuskiomi – 2017-06-21T15:41:54.467

@tuskiomi how about "<?=PHP_INT_SIZE==4?32:PHP_INT_SIZE==8?64:0;" as an answer (45 Bytes) – Bja – 2017-06-21T15:47:17.117

1@Bja that's a valid competitive answer. – tuskiomi – 2017-06-21T15:48:18.590

2

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

Hankrecords

Posted 2017-06-19T20:12:03.273

Reputation: 149

1From other answers I don't think you need to return a string, if so: _=>navigator.userAgent.includes\x64`+1<<5` – Gustavo Rodrigues – 2017-06-20T13:05:05.983

@GustavoRodrigues Clever approach! Thanks :) – Hankrecords – 2017-06-20T13:10:13.750

Doesn't work on my laptop, it prints 32 instead of 64 – Thomas Ayoub – 2017-06-20T13:21:00.037

@ThomasAyoub Could you please post your laptop's full navigator.userAgent string? – Hankrecords – 2017-06-20T13:45:05.000

"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0" and here's a screenshot – Thomas Ayoub – 2017-06-20T13:56:44.097

@ThomasAyoub Thanks, updated! – Hankrecords – 2017-06-20T13:59:47.270

2How about using regular expressions? _=>/64/.test(navigator.userAgent)+1<<5 – Gustavo Rodrigues – 2017-06-20T18:17:03.270

@GustavoRodrigues Edited! ty :) – Hankrecords – 2017-06-21T06:39:11.560

2

Java 7, 77 71 bytes

A java pointer-size solution

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

Twometer

Posted 2017-06-19T20:12:03.273

Reputation: 281

2

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.

peter ferrie

Posted 2017-06-19T20:12:03.273

Reputation: 804

1

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.

Adám

Posted 2017-06-19T20:12:03.273

Reputation: 37 779

1

SAS, 28

%put%eval(&syssizeoflong*8);

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

user3490

Posted 2017-06-19T20:12:03.273

Reputation: 809

1

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.

user2428118

Posted 2017-06-19T20:12:03.273

Reputation: 2 000

I thought that it had to print 64, 32, and a non empty string respectively? – Restioson – 2017-06-23T09:26:45.687

1

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

Taylor Scott

Posted 2017-06-19T20:12:03.273

Reputation: 6 709

1

This seems more preferable: IntPtr.Size*8 https://msdn.microsoft.com/en-us/library/system.intptr.size(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

– Alexander - Reinstate Monica – 2017-06-21T05:27:06.913

@Alexander thanks! I had looked for an Intger Bitwidth myself but I never would have thought to use IntPtr – Taylor Scott – 2017-06-21T13:56:27.963

1

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.

Kaz

Posted 2017-06-19T20:12:03.273

Reputation: 372

1

Go, 94 bytes

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

Avdept

Posted 2017-06-19T20:12:03.273

Reputation: 111

1

Python 2.7, 49 bytes

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

Koishore Roy

Posted 2017-06-19T20:12:03.273

Reputation: 1 144

1

Perl 6, 17 bytes

say $*KERNEL.bits

bb94

Posted 2017-06-19T20:12:03.273

Reputation: 1 831

0

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.

Chris J

Posted 2017-06-19T20:12:03.273

Reputation: 199

Wait, Powershell on Linux?? Sign me up! But really, did you just paste this into the shell or what? – Cullub – 2017-06-21T12:30:38.850

2

Yes - see https://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 – 2017-06-21T12:42:50.813

this will print 32 for an 8-bit os, no? – tuskiomi – 2017-06-21T13:40:19.117

@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 – 2017-06-21T19:26:14.033

@ChrisJ agreed, but it does need an alternate case for non 32/64 bit systems in order to be competitive. Still valid though. – tuskiomi – 2017-06-21T19:28:12.913

@tuskiomi I don't quite get what you mean by an alternate case. Do you mean that the program should determine all the possible bitnesses beyond 32 and 64? This doesn't seem to have been specified in the rules. – Ruslan – 2017-06-27T12:08:58.290

@ruslan from the main post: Important: Print any other non-empty string of alphanumeric characters if it's neither 32 or 64 bit. – tuskiomi – 2017-06-27T12:23:37.420