-3

Is there a way to know whether CPU is 32 bit or 64 bit without the use of sizeOf operator?

Can any other code be written for this?

n3wcod3r
  • 114
  • 10
  • Using `cpuid` via inline assenbly if the target is x86? – MikeCAT Mar 10 '16 at 14:29
  • 7
    and how do you do it using `sizeOf` (_mind the case_) operator? – Sourav Ghosh Mar 10 '16 at 14:29
  • 1
    And what do you mean by 32 or 64bit CPU? Bits of what? – Eugene Sh. Mar 10 '16 at 14:30
  • 3
    You should give us more details, in particular: is this Linux or Windows? – 4pie0 Mar 10 '16 at 14:31
  • 2
    Possible duplicate of [how to find if the machine is 32bit or 64bit](http://stackoverflow.com/questions/2401756/how-to-find-if-the-machine-is-32bit-or-64bit) – SandBag_1996 Mar 10 '16 at 14:32
  • Are you looking for an arithmetic expression you can evaluate? Like `(unsigned)-1L == 0xffffffffUL`? (don't actually use that, win64 uses 32bit `long`, and IIRC 32bit Linux uses 64bit `long long`. And the x32 Linux ABI has 32bit `intptr_t`, but runs the CPU in long mode). Or are you looking for a preprocessor macro to figure out things about your compile target? – Peter Cordes Mar 10 '16 at 14:33
  • 3
    **Why** exactly **do you ask?** Can't you simply use `` then `int32_t`, `int64_t`, `intptr_t` and so on? Please edit your question to improve it... (and what about compiling with `gcc -m32` on an x86-64 Linux system; and what about [x32 ABI](https://en.wikipedia.org/wiki/X32_ABI) ?? – Basile Starynkevitch Mar 10 '16 at 14:36
  • 1
    Remember that 64 bit CPU+OS will often run 32 bit code just fine, so you can't tell by using 32-bit code. And 64-bit code won't run on a 32-bit OS, so you're not going to find out that way either. – Mark Ransom Mar 10 '16 at 14:40
  • You can check if your program is running on a 32bit or 64bit OS, but I don't think you can check if CPU supports 64bit without knowing its vendor and model – Mr. E Mar 10 '16 at 14:47
  • @Mr.E ..and how do you check if your program is running on a 32bit or 64bit OS? – SandBag_1996 Mar 10 '16 at 14:49
  • @UnderDog Apparently I was wrong. You can actually check if CPU supports 64 bits. I'll post an answer – Mr. E Mar 10 '16 at 14:55

3 Answers3

2

In this question How to determine whether a given Linux is 32 bit or 64 bit?

To check if system is 32 or 64 bit kernel, you can call

system("getconf LONG_BIT")

And check it's return. If it says 64 it's a 64bit kernel, if it's 32 it's 32bit one.

To check if the cpu supports 64bits, you can check it in the file /proc/cpuinfo if it has the flag "lm" (Long Mode)

system("grep flags /proc/cpuinfo | grep -c lm")

If the return is 1 then lm flag is present (64 bit), if it's 0 it's not (32 bit)

This is linux only though. Other options are in the question linked at the begining. Some includes checking limits.h for example.

Community
  • 1
  • 1
Mr. E
  • 2,070
  • 11
  • 23
1

Your code should have been built for the processor that it is running on, so it will be in the compiler directives. Look at how the maths libraries handle it, and do that. It is different for different compilers, but you cant universally do it with C code. For example: all platforms should support 64 bit values. How they handle them will vary depending on compiler directives.

Steve McK
  • 11
  • 2
  • 1
    With C99 it is wrong. You can handle 64 bits using `int64_t` from `` and the code won't depend upon directives... – Basile Starynkevitch Mar 10 '16 at 14:37
  • @BasileStarynkevitch: I think he means whether an add is a single machine instruction, or if it requires `add` / `adc`, or whatever add-with-carry is called on the target CPU. On second reading, this answer does seem to say that you might need different source code, though. As you say, not the case with C99. – Peter Cordes Mar 10 '16 at 14:53
-1

How about pointer math? Take the address of two elements in an array of pointers, determine if they are 8 or 4 bytes apart.

{
    char * pa[2];
    char * pa1 = (char *)&pa[1];
    char * pa0 = (char *)&pa[0];
    if (pa1 - pa0 > 4)
        /* 64 bit pointers */;
    else 
        /* ... */;
}
aghast
  • 14,785
  • 3
  • 24
  • 56
  • Not my downvote. The code is absolutely machine dependent. It cannot be generalized, no. You can have 32bit pointers on a 64bit machine. Also, see "far pointer". You can have a 32bit far pointer on a 16bit machine. – SandBag_1996 Mar 10 '16 at 14:43
  • If you want to know if pointers are 64bit, you should just test `UINTPTR_T > 0xFFFFFFFFUL`. Note that the Linux x32 ABI runs on a 64bit CPU (amd64), but uses 32bit pointers. It can still work with 64bit integers without add-with-carry and stuff like that, and has efficient atomic 64bit ops. – Peter Cordes Mar 10 '16 at 14:59
  • Did you mean UINTPTR_MAX? – aghast Mar 10 '16 at 15:01
  • Well take an upvote from me - this is how you do `sizeof` without actually using `sizeof`. – Mark Ransom Mar 10 '16 at 16:34