Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

write a C/C++ program to find if a machine is 32 bit or 64 bit

Tags:

c++

I searched in web, could not get any program.
I found following links size of machine 64 or 32 bit and processing files in 64 bit machine but developing in 32 bit machine.
Now it is clear that sizeof(int*) is not the way. Because it will return 4/8 based on the architecture of the machine used for compilation. So then how to find it?
Condition: do not use any system/os/library call.
Actually it is a question asked in the interview.

like image 872
user875036 Avatar asked Aug 02 '11 17:08

user875036


People also ask

How you can check whether a machine is 64-bit or 32-bit in C?

You can try sizeof(char*) , in general the pointer-to-char has the machine-size. If it's 8, then your machine is 64 bit, if 4, then 32.

Is my CPU 32 or 64-bit?

Click Start, type system in the search box, and then click System Information in the Programs list. When System Summary is selected in the navigation pane, the operating system is displayed as follows: For a 64-bit version operating system: X64-based PC appears for the System Type under Item.

What is 32-bit and 64-bit machine?

32-bit means that a microprocessor can execute 4 bytes of data in one instruction cycle while 64-bit means that a microprocessor can execute 8 bytes of data in one instruction cycle. Since microprocessor needs to talk to other parts of computer to get and send data i.e. memory, data bus and video controller etc.

How do I tell if computer is 64-bit?

Right-click “This PC” on the left side of the screen. Select “Properties” on the menu. The “System Properties” window will open. This window will list both the computer's operating system and CPU type.


6 Answers

32-bit system address spaces cannot address more than 4gb of memory. Assuming the 64-bit platform has that amount available free (debatable), you could try and allocate more than 4 gig in a single chunk. This will most certainly fail on a 32-bit system.

This is just a thought, and I'll probably be down-voted to hell and back, but it's just a suggestion :)

like image 71
Moo-Juice Avatar answered Oct 05 '22 23:10

Moo-Juice


Compile the program as 64 bit and try if it can be executed on the target machine or not?

like image 22
sth Avatar answered Oct 06 '22 00:10

sth


What about inline assembly? :)

This is based solely on information read with CPUID instruction. It doesn't matter what OS is used.

#include <iostream>

bool is64Bit()
{
    int ExtendedFeatureFlags;
    asm ( "mov $0x80000001, %%eax; " // 0x80000001 gets Extended Feature Flags
              "cpuid; "                 // Call CPUID instruction.
              "mov %%edx, %0; "         // Copy EDX into first output variable.
              :"=r"(ExtendedFeatureFlags)  // Output variable.
              :                            // No input variables.
              :"%eax","%ebx","%ecx","%edx" // Clobbered registers.
            );
    return ExtendedFeatureFlags & (1<<29);
    // If the 29th bit is on, the processor supports 64bit
    // extensions.
}

int main()
{
    std::cout << "Is 64bit?: " << (is64Bit() ? "YES" : "NO") << std::endl;
    return 0;
}
like image 36
Michał Bentkowski Avatar answered Oct 05 '22 23:10

Michał Bentkowski


I'll be very impressed if you manage to find any way aisde from sizeof(int*) that doesn't use an operating system call. I think that you probably already have as good an answer as they were looking for :p

like image 31
John Humphreys Avatar answered Oct 06 '22 00:10

John Humphreys


With C++ an int on a 64 bit machine with a 64 bit compiler should be 64 bits, likewise for a 32 bit machine, so sizeof(int*) should work

like image 37
alex28 Avatar answered Oct 06 '22 01:10

alex28


The 32-bit environment sets int, long and pointer to 32 bits and generates code that runs on any i386 system.

The 64-bit environment sets int to 32 bits and long and pointer to 64 bits and generates code for AMD's x86-64 architecture.

You can use

#include <stdio.h>
int main(){
        long z; printf("Long int size is %i bytes long!\n", sizeof(z)); return 0;
}

and compile with -m32 and -m64 in gcc. If its a 64bit platform the program will run and output will be 8 else program will die.

like image 34
Vijay Sharma Avatar answered Oct 06 '22 01:10

Vijay Sharma