How to know if my OS is 64bit or 32bit?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
junkoi
Member
Member
Posts: 63
Joined: Wed Jan 23, 2008 8:55 pm

How to know if my OS is 64bit or 32bit?

Post by junkoi »

Hi,

Given the value of all the x86 registers (general registers, segment registers, CR<n>, MSR, ...), how can we tell the OS is 64bit or 32bit?

Many thanks,
Jun
User avatar
zity
Member
Member
Posts: 99
Joined: Mon Jul 13, 2009 5:52 am
Location: Denmark

Re: How to know if my OS is 64bit or 32bit?

Post by zity »

If you're given at list of all the registers on a system, simply look for the largest register in use.

Take the ax-register for an example
rax = 64 bit
eax = 32 bi
ax = 16 bit

Code: Select all

|63..32|31..16|15-8|7-0|
               |AX.....|
       |EAX............|
|RAX...................|
If that's what you mean?

EDIT: You could also check for e.g. the long mode enable bit.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: How to know if my OS is 64bit or 32bit?

Post by pcmattman »

how can we tell the OS is 64bit or 32bit
Why would you need to? Can you possibly give us a little more information about what you're trying to achieve?
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: How to know if my OS is 64bit or 32bit?

Post by NickJohnson »

If you are talking about your host operating system (or at least your host compiler, which is what matters), this will tell you:

Code: Select all

int main() {
 switch (sizeof(void*)) {
  case 1: printf("8 bit\n"); break;
  case 2: printf("16 bit\n"); break;
  case 4: printf("32 bit\n"); break;
  case 8: printf("64 bit\n"); break;
  default: printf("uh...\n");
 }
 return 0;
}
Fanael
Member
Member
Posts: 38
Joined: Fri Oct 16, 2009 9:20 am

Re: How to know if my OS is 64bit or 32bit?

Post by Fanael »

If EFER.LMA is set, then it is 64-bit, else if CR0.PE is set and gdt_entry(CS).D is set then it is 32-bit, else it is 16-bit.
fronty
Member
Member
Posts: 188
Joined: Mon Jan 14, 2008 5:53 am
Location: Helsinki

Re: How to know if my OS is 64bit or 32bit?

Post by fronty »

NickJohnson wrote:If you are talking about your host operating system (or at least your host compiler, which is what matters), this will tell you:

Code: Select all

int main() {
 switch (sizeof(void*)) {
  case 1: printf("8 bit\n"); break;
  case 2: printf("16 bit\n"); break;
  case 4: printf("32 bit\n"); break;
  case 8: printf("64 bit\n"); break;
  default: printf("uh...\n");
 }
 return 0;
}
Or even

Code: Select all

int main(void) {
        printf("%d bit\n", sizeof(void *) * 8);
        return 0;
}
;)
Fanael
Member
Member
Posts: 38
Joined: Fri Oct 16, 2009 9:20 am

Re: How to know if my OS is 64bit or 32bit?

Post by Fanael »

Or portably:

Code: Select all

#include <limits.h>
#include <stdio.h>
int main(void)
{
  printf("%d bits", sizeof(void*) * CHAR_BIT);
  return 0;
}
:P
Post Reply