473,385 Members | 1,470 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

how to detect CPU architecture..

hi
i have to detect the type of CPU.
whether it is 32-bit or 64-bit..
how can this be done..

can anyone please help me..

thanks.

Sep 1 '06 #1
13 7073
On 31 Aug 2006 23:14:23 -0700, rr********@gmail.com wrote:
>hi
i have to detect the type of CPU.
whether it is 32-bit or 64-bit..
how can this be done..

can anyone please help me..

thanks.
Standard C does not provide any way to detect whether the type of CPU
is 32-bit or 64-bit or even 8-bit or 16-bit or 128-bit. Since this
newsgroup is about Standard C, no one here can really help you.

--
jay
Sep 1 '06 #2
rr********@gmail.com wrote:
i have to detect the type of CPU.
whether it is 32-bit or 64-bit..
how can this be done..
It can't. The CPU is 8-bit.

Richard






(Well, it could be. I still have computers like that. And define "the
type". And explain why you want to know. You probably _think_ you need
to, but you're equally probably mistaken.)
Sep 1 '06 #3
Try to use ULONG_MAX in gcc macro if you use Unix.
or sizeof(long): 4 for 32bit, 8 for 64bit arch.

Sep 1 '06 #4
In article <7a********************************@4ax.com>,
jaysome <ja*****@spamcop.netwrote:
>On 31 Aug 2006 23:14:23 -0700, rr********@gmail.com wrote:
>>i have to detect the type of CPU.
whether it is 32-bit or 64-bit..
how can this be done..
>Standard C does not provide any way to detect whether the type of CPU
is 32-bit or 64-bit or even 8-bit or 16-bit or 128-bit. Since this
newsgroup is about Standard C, no one here can really help you.
Furthermore, Standard C does not provide any what to determine what any
particular person -means- by a CPU being 32 bit or 64 bit or something
else. Bits in int? Bits in long? Bits in long long? Bits in a pointer?
Maximum theoretical addressible memory? Bits in the data bus between
the CPU and memory? Maximum number of bits in a result produced by an
operation? (that's a trick question -- integer multiple and divide
instructions are often exceptions to the rest of the architecture.)

Then there are systems like all of the SGI MIPS-based computers
produced in the last decade, all of which have CPUs which can
switch between 32 bit pointers and 64 bit pointers (per process),
any particular test run would not tell you what the CPU is capable
of, only how that particular test program was set up to run.
--
Prototypes are supertypes of their clones. -- maplesoft
Sep 1 '06 #5
posted:
hi
i have to detect the type of CPU.
whether it is 32-bit or 64-bit..
how can this be done..

I suppose the closest thing might be something like:

#include <limits.h>

#if UINT_MAX == 4294967295U
/* We have 32-Bit */
#elsif UINT_MAX == 18446744073709551615U
/* We have 64-Bit */
#else
/* Neither */
#endif

--

Frederick Gotham
Sep 1 '06 #6
Frederick Gotham wrote:
posted:

>>hi
i have to detect the type of CPU.
whether it is 32-bit or 64-bit..
how can this be done..

I suppose the closest thing might be something like:

#include <limits.h>

#if UINT_MAX == 4294967295U
/* We have 32-Bit */
#elsif UINT_MAX == 18446744073709551615U
/* We have 64-Bit */
#else
/* Neither */
#endif
No. This would "detect" a 32 bit system in a 64 bit
unix. Use "long" for that. In 64 bit Unix, you have
(in some implementations, specifically gcc)
long 64 bits
int 32 bits

64 bit windows has
long 32 bits
int 32 bits

jacob
Sep 1 '06 #7
In article <FI*******************@news.indigo.ie>,
Frederick Gotham <fg*******@SPAM.comwrote:
[someone not named by Frederick posted]
>i have to detect the type of CPU.
whether it is 32-bit or 64-bit..
>I suppose the closest thing might be something like:
>#include <limits.h>
>#if UINT_MAX == 4294967295U
/* We have 32-Bit */
#elsif UINT_MAX == 18446744073709551615U
/* We have 64-Bit */
#else
/* Neither */
#endif

SGI IRIX in -64 mode would have UINT_MAX as 4294967295U .

Perhaps you meant to use ULONG_MAX instead of UINT_MAX ?
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
Sep 1 '06 #8
jacob navia posted:
No. This would "detect" a 32 bit system in a 64 bit
unix.

Yes, I realise that. It was a merely fully-portable attempt to detect CPU
bitness.

--

Frederick Gotham
Sep 1 '06 #9
"dkrot" <j.*****@rambler.ruwrites:
Try to use ULONG_MAX in gcc macro if you use Unix.
or sizeof(long): 4 for 32bit, 8 for 64bit arch.
Please quote enough context so we can tell what you're talking about
even if we haven't seen the parent article.

ULONG_MAX (which is standard C, not specific to either gcc or Unix)
only tells you what the compiler writer has chosen as the maximum
value of type unsigned long. It tells you nothing about the
underlying CPU architecture. The compiler writer's choices for the
attributes of the predefined types are typically guided by the CPU
architecture, but they're not mandated by it.

It's not even clear what "32-bit architecture" or "64-bit
architecture" actually means. It might have some accepted meaning for
particular sets of architectures (e.g., the x86 family), but that
won't necessarily apply to other architectures (PowerPC, SPARC,
AS/400, DS9K, etc.).

And even if you can rigorously define the terms, there's not likely to
be any way to distinguish between them in portable C.

The entire question is largely meaningless and off-topic.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 1 '06 #10
Frederick Gotham <fg*******@SPAM.comwrites:
jacob navia posted:
>No. This would "detect" a 32 bit system in a 64 bit
unix.

Yes, I realise that. It was a merely fully-portable attempt to detect CPU
bitness.
Portability isn't a virtue if your program doesn't solve the problem.

Here's another fully-portable attempt to detect CPU bitness:

#include <stdio.h>
int main(void)
{
printf("Hello, world\n");
return 0;
}

Yours, I'm afraid, was only marginally better.

The fact is, there is no fully-portable solution, and it's a waste of
time trying to construct one.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 1 '06 #11
>i have to detect the type of CPU.
>whether it is 32-bit or 64-bit..
how can this be done..
An N-bit CPU architecture is an *ADVERTISING* and *MARKETING*
concept. It can change with no physical change in the CPU, and it
can change before the CPU is even built.

What is it you think is indicated by "this CPU has an N-bit
architecture", and why do you need to know?

1. This CPU uses N bits for char.
2. This CPU uses N bits for int.
3. This CPU uses N bits for long.
4. This CPU uses N bits for long long.
5. This CPU uses N bits for pointers.
6. This CPU uses an N-bit-wide memory bus.

These won't necessarily occur together.

#1 won't happen very often, especially on desktop computers or servers,
as opposed to embedded processors.
#6 is likely detectable from software only by measuring performance,
and possibly not even then, unless software can read the invoice for
the purchase of the system.
#4 is unlikely to indicate much as lots of machines provide a 64-bit long
long with multiple-precision arithmetic, even on machines arguably of
16/32 bit architecture.

I think the most likely meaning of "64-bit architecture" is that
*POINTERS* are 64 bits, so no nasty 4GB addressing space limit.
There is no agreement on that.

Sep 1 '06 #12
go***********@burditt.org (Gordon Burditt) writes:
>>i have to detect the type of CPU.
whether it is 32-bit or 64-bit..
how can this be done..
The above was posted by rr********@gmail.com. Gordon insists on
deleting attribution lines, which is extraordinarily rude.
An N-bit CPU architecture is an *ADVERTISING* and *MARKETING*
concept. It can change with no physical change in the CPU, and it
can change before the CPU is even built.

What is it you think is indicated by "this CPU has an N-bit
architecture", and why do you need to know?

1. This CPU uses N bits for char.
2. This CPU uses N bits for int.
3. This CPU uses N bits for long.
4. This CPU uses N bits for long long.
5. This CPU uses N bits for pointers.
6. This CPU uses an N-bit-wide memory bus.

These won't necessarily occur together.
And most of these are choice of the compiler implementer, not
necessarily attributes of the CPU. Two different compilers, or the
same compiler in two different modes, can choose different sizes for
int on the same CPU.

[...]
I think the most likely meaning of "64-bit architecture" is that
*POINTERS* are 64 bits, so no nasty 4GB addressing space limit.
There is no agreement on that.
Yes, on most CPUs, pointer size is fairly likely to match what most
people think of as the "bitness" of the architecture. But it's far
from universal. There have been, and still are, plenty of 8-bit CPUs,
but few if any of them have 8-bit pointers.

What's probably relevant is that the "32-bit" and "64-bit" versions of
a given architecture may have different APIs (or different flavors of
the same API, or whatever). As a programmer, what you really need to
know is which API to use. There may be some attribute you can test to
determine this, but it's likely to vary from one architecture family
to another; the details are system-specific and therefore off-topic
here.

If your first question is "Is this a 32-bit or a 64-bit CPU?", your
zeroth question should be "Why do I want to know this?".

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 1 '06 #13
rr********@gmail.com wrote:
# hi
# i have to detect the type of CPU.
# whether it is 32-bit or 64-bit..
# how can this be done..

Not from C by itself, but if you know, for example that on one cpu
long is 4 bytes and 8 bytes on the other, then you can use
if (sizeof(long)==4) {
32 bit code
}else if (sizeof(long)==8) {
64 bit code
}else }
...
}

You can also pull some information from limits.h

--
SM Ryan http://www.rawbw.com/~wyrmwif/
She broke your heart and inadvertently drove men to deviant lifestyles.
Sep 1 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: David Noble | last post by:
We've been developing a web site using 3-tier architecture for 18 months now. There is a common layer that defines the classes - using XML schemas. The data layer acts as a wrapper to 3 databases...
2
by: Vicent | last post by:
Hi all: I would like to Know if a host is still alive in the network. We can suposse that we Know the name of the PC. Any source code would be appreciatted. Thanks in advance: Vicent Vidal
4
by: apngss | last post by:
what's the differences between collocated architecture and distributed architecture?? My understanding is that collocated architecture has everything in a single machine? i.e. There is only 1...
6
by: Adam Warner | last post by:
Hi all, Is this a (C99) portable way to detect whether the C stack grows upwards (1) or downwards (-1)? #include <stdio.h> int stack_direction=0; void detect_stack_direction(void *...
6
by: carsonbj | last post by:
I have an issue where the below operation works on a little-endian architecture but not on a big-endian architecture. I was under the impression that pointer arithmetic is architecture independant...
6
by: rrs.matrix | last post by:
hi i have to detect the type of CPU. whether it is 32-bit or 64-bit.. how can this be done.. can anyone please help me.. thanks.
0
by: srikar | last post by:
Hi all, I am having a problem, when I am compiling the code in 32 bit option on a 64 bit machine using the macro CPPFLAGS= -m32 I am getting the following warnings from the linker . ...
15
by: Eric Renken | last post by:
Is there something in System.Environment that can tell me if the program is running on a 64bit OS? What I am thinking is right now is to check the size of IntPtr and if it is 4 then it is 32bit,...
5
by: =?Utf-8?B?S3VtYXIuQS5QLlA=?= | last post by:
Kumar.A.P.P Software Engineer, Infosys Technologies Limited. http://techaid.wordpress.com/
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.