How do you find out if a machine is 32 bit or 64 bit?

C

codergem

One common answer is that all compilers keep the size of integer the
same as the size of the register on a particular architecture. Thus, to
know whether the machine is 32 bit or 64 bit, just see the size of
integer on it.

Is it Correct??
Or there any other way of finding it out?
Do I need to mention that through a C++ program..??
 
I

Ian Collins

One common answer is that all compilers keep the size of integer the
same as the size of the register on a particular architecture. Thus, to
know whether the machine is 32 bit or 64 bit, just see the size of
integer on it.

Is it Correct??
No.

Or there any other way of finding it out?

sizeof(void*) is a better bet, but why would a running program care?
 
C

codergem

But the sizes of an int and void* is same.
as far as i know.
And yes these sizes tell about the compiler is 16bit or 32 bit.
and the ques is to find out of a machine.
So are both the things same?
 
A

Alex Buell

One common answer is that all compilers keep the size of integer the
same as the size of the register on a particular architecture. Thus,
to know whether the machine is 32 bit or 64 bit, just see the size of
integer on it.

Is it Correct??

No. Something like this will do the trick for integers, as below. You
will need to include <limits> in your code.

const int bits = std::numeric_limits<int>::digits;

Then if the type is really 32 bits, the variable bits will contain the
value 32. It's that simple.
 
I

Ian Collins

(e-mail address removed) wrote:

Please don't top post! fixed.
But the sizes of an int and void* is same.
as far as i know.
And yes these sizes tell about the compiler is 16bit or 32 bit.
and the ques is to find out of a machine.
So are both the things same?

They most likely are not the same on a 64 bit system, try this example:

#include <iostream>

int
main()
{
std::cout << sizeof(int) << std::endl;
std::cout << sizeof(long) << std::endl;
std::cout << sizeof(void*) << std::endl;
}

I still don't know what you are trying to achieve, if a program is
compiled on a 32 system, or 32 bit on a 64 bit system, it will run as 32
bit no matter what the machine architecture is.

There may be a target specific means to determine the machine size, but
there isn't a standard C++ one.
 
J

John Carson

But the sizes of an int and void* is same.
as far as i know.

You know wrong. On 64 bit Windows systems, an int is 32 bits and a pointer
is 64 bits.

If you want to write a program for a 64 bit system, you will need a new
compiler or at least you need to set a switch on the compiler. As Ian
suggests, if you compile a 32 bit program and run it on a 64 bit system,
then it works via the 64 bit system emulating a 32 bit system. Thus it is
always your compiler that determines whether you need to program for a 32
bit or 64 bit environment.

If you want to use a common code base for both 32 bit and 64 bit programs,
you can test in your code for the compiler or compiler switch. Mostly, you
should just avoid making any assumptions about size in your coding. Size
should only become an issue with vendor specific extensions to the Standard.
 
Y

yoleung

I am not sure I am asking the correct question
When we say 32/64 bit, are we talking the addressing mode or the
instruction size, yes, most processor nowadays insycn these two values,
but not necessary always,

For example, instruction size could be 32 bit while addressing is 64
bit, just one memory slot contains two instruction, possible?
 
I

Ian Collins

I am not sure I am asking the correct question
When we say 32/64 bit, are we talking the addressing mode or the
instruction size, yes, most processor nowadays insycn these two values,
but not necessary always,

For example, instruction size could be 32 bit while addressing is 64
bit, just one memory slot contains two instruction, possible?
Please don't top post!

<OT> The general loose definition of a processor's bit size is its
register and ALU - the CPU core - sizes.

A processor can have a 64 core, but be restricted to a smaller address
range or external data bus size. It is still a 64 bit processor.

The instruction size on most modern CPUs is variable.</OT>
 
P

Patrick

Ian said:
Please don't top post!

<OT> The general loose definition of a processor's bit size is its
register and ALU - the CPU core - sizes.

A processor can have a 64 core, but be restricted to a smaller address
range or external data bus size. It is still a 64 bit processor.

The instruction size on most modern CPUs is variable.</OT>

So do you mean the size of the register?
 
N

Noah Roberts

One common answer is that all compilers keep the size of integer the
same as the size of the register on a particular architecture. Thus, to
know whether the machine is 32 bit or 64 bit, just see the size of
integer on it.

Is it Correct??
Or there any other way of finding it out?
Do I need to mention that through a C++ program..??

sizeof int is 4 on amd64.

long might be a better choice. Is it not specified in the standard as
the word size of the architecture? Oh well, not near by...you'll have
to check.

Or as someone said, void* is supposed to be big enough to hold any
pointer, no? Don't know if it has to be word size by standard or not
though...but it likely is.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

sizeof int is 4 on amd64.

You might have compiled this in 32-bit mode, see below.
long might be a better choice. Is it not specified in the standard as
the word size of the architecture? Oh well, not near by...you'll have
to check.

Or as someone said, void* is supposed to be big enough to hold any
pointer, no? Don't know if it has to be word size by standard or not
though...but it likely is.

If I remember correctly, in C (and I suppose also in C++) what is
specified is that a char is one byte (thought nothing said about how big
a byte is) and char <= short, short <= int and int <= long. I also seem
to recall that an int should be the "natural" size of the architecture
which would make it 32 on a 32-bit machine and 64 on a 64-bit machine
(and 16 on one of those machines).

In C there is also a macro telling the number of bits of a char, which
multiplied with sizeof(int) (which returns the number of chars per int)
gave the number of bits of an int.

Anyone know if this is valid in C++ also?

Erik Wikström
 
I

Ian Collins

Erik said:
You might have compiled this in 32-bit mode, see below.



If I remember correctly, in C (and I suppose also in C++) what is
specified is that a char is one byte (thought nothing said about how big
a byte is) and char <= short, short <= int and int <= long. I also seem
to recall that an int should be the "natural" size of the architecture
which would make it 32 on a 32-bit machine and 64 on a 64-bit machine
(and 16 on one of those machines).
Try the example I posted earlier, odds are you will see 32 bit for int.
In C there is also a macro telling the number of bits of a char, which
multiplied with sizeof(int) (which returns the number of chars per int)
gave the number of bits of an int.

Anyone know if this is valid in C++ also?
Yes.
 
A

Alex Buell

sizeof int is 4 on amd64.

long might be a better choice. Is it not specified in the standard as
the word size of the architecture? Oh well, not near by...you'll have
to check.

Use limits!
 
M

Marcus Kwok

Erik Wikstr?m said:
If I remember correctly, in C (and I suppose also in C++) what is
specified is that a char is one byte (thought nothing said about how big
a byte is)

IIRC it does specify that a byte is *at least* 8 bits.
In C there is also a macro telling the number of bits of a char, which
multiplied with sizeof(int) (which returns the number of chars per int)
gave the number of bits of an int.

Anyone know if this is valid in C++ also?

Should be. There is also <limits>, which has
std::numeric_limits<unsigned char>::digits.
 
S

SuperKoko

Noah said:
sizeof int is 4 on amd64.

long might be a better choice. Is it not specified in the standard as
the word size of the architecture? Oh well, not near by...you'll have
to check.

Or as someone said, void* is supposed to be big enough to hold any
pointer, no? Don't know if it has to be word size by standard or not
though...but it likely is.
The word size is not well defined.
Is the word size 16 or 32 bits on the 68000 CPU?

Defining a word size is a bad thing... Assuming that there is a
"machine word which works for pointers & integers" is really not
portable... That is one reason why the B language is obsolete.
The CPU architecture doesn't dictate a particular pointer size (and
pointer sizes depend on the type pointed-to by the pointer, even if
there are constraints like the fact that all pointers to structures
have the same size & representation).
Remember that "16 bits" x86 compilers had several memory models (most
common were tiny, small, medium, compact, large and huge).
tiny : sizeof(int)==2, sizeof(any object pointer)==2, sizeof(any
function pointer)==2, CHAR_BIT==8
small : same as tiny for pointer sizes (but pointer to object and
pointer to functions lived in two different segments).
medium : sizeof(int)==2, sizeof(object pointer)==2, sizeof(function
pointer)==4
compact : sizeof(int)==2, sizeof(object pointer)==4, sizeof(function
pointer)==2
large : sizeof(int)==2, sizeof(object pointer)==4, sizeof(function
pointer)==4
huge : sizeof(int)==2, sizeof(object pointer)==4, sizeof(function
pointer)==4 (but pointer arithmetic was adjusted and allowed to have
memory blocks greater than 64KB).

Now, the question is: What's the size of a pointer on a 8086 CPU? There
is no answer... It is implementation-defined.... And compiler flags can
even change the size of a pointer.

Note also that sizeof(void*) can be smaller than
sizeof(any_other_pointer), even if a void* pointer can hold any other
pointer.

For instance, a bound-checked implementation on the IA-32 architecture
may use 96 bits pointers for all types except char*, signed char*,
unsigned char* and void* which would be 32 bits (they would not hold
any bound information).
Because char* is not designed to be bound checked:
For instance:

int array[3][3];
int* pi=&array[0][0];
for(size_t i=0;i<5;++i) pi=0; // undefined behavior : pointer
arithmetic must not go out of the bounds of an array (and the real
static type pointed-to by pi is int[3]).
// This is likely to throw an exception on bound-checked
implementations.

char* pc=static_cast<char*>(static_cast<void*>(array));
for(size_t i=0;i<sizeof(p);++i) *pc=0; // valid
If I remember correctly, in C (and I suppose also in C++) what is
specified is that a char is one byte (thought nothing said about how big
a byte is) and char <= short, short <= int and int <= long. I also seem
to recall that an int should be the "natural" size of the architecture
which would make it 32 on a 32-bit machine and 64 on a 64-bit machine
(and 16 on one of those machines).
Natural size is not well defined.
After thought, 32 bits integer is quite natural on 64 bits platform
(and LP64 is really used by many compilers).
http://www.unix.org/whitepapers/64bit.html
In C there is also a macro telling the number of bits of a char, which
multiplied with sizeof(int) (which returns the number of chars per int)
gave the number of bits of an int.
Yes, CHAR_BIT
C++ too, has this macro.

Note that, as far as I remember, the first C implementation was on the
PDP-11, a 9-bits byte machine.

Anyway, I think that knowing if a machine is 64 bits or 32 bits (since
there is no formal definition of "being 64 bits") is useless.
On the other side, knowing that sizeof(int)==4 or sizeof(int)==8 is
useful... And it is an information useful, independently of
sizeof(void*) or sizeof(struct MyStruct*).
 
S

SuperKoko

SuperKoko said:
char* pc=static_cast<char*>(static_cast<void*>(array));
for(size_t i=0;i<sizeof(p);++i) *pc=0; // valid
Sorry, but I posted incorrect code.
I wanted to mean pc=0; (and not *pc=0).
 
N

Noah Roberts

Erik said:
You might have compiled this in 32-bit mode, see below.

No. I know the difference. I found it rather surprising myself when I
compiled my first program.

However, I should have stated: sizeof int is 4 on the gnu compiler
with amd64 as the target architecture. Other compilers can be
different afaik.
 
N

Noah Roberts

Noah said:
sizeof int is 4 on amd64.

long might be a better choice. Is it not specified in the standard as
the word size of the architecture? Oh well, not near by...you'll have
to check.

Or as someone said, void* is supposed to be big enough to hold any
pointer, no? Don't know if it has to be word size by standard or not
though...but it likely is.

I should have also mentioned that the BEST way to do this has nothing
to do with C++. Use autoconf/automake. This is the sort of thing it
was invented for. You run a 'configure' before a build, it generates
your makefile and defines various sizes and other platform dependent
things for you in a config.h (if you tell it to) and then you just use
preproc checks when you need to be platform dependant but want to
target multiple architectures.

Obviously you have to break out of the IDE if you are using MSVC++ and
their stupid project files instead of Makefiles. In fact, you don't
ever actually make the Makefile at all, configure does that.
 
P

Patrick

Ian said:
Please don't top post!

<OT> The general loose definition of a processor's bit size is its
register and ALU - the CPU core - sizes.

A processor can have a 64 core, but be restricted to a smaller address
range or external data bus size. It is still a 64 bit processor.

The instruction size on most modern CPUs is variable.</OT>

Intstruciton sizes might be variable, but the whole instruction with
paratmers is usually fixed. Otherwise the IP will have unaligned
address which I doubt the CPU could fetch it and instruction acrossing
two words will make the CPU design insane...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top