bitness of machine using C language

S

Shri

How to identify if the given machine is 8/16/32 bit using C code ???

printf("Word length = %d", sizeof(int));

Doesn't that depend on the compiler you are using for compiling that
piece of code??? I think that there are compilers that use 4 bytes
for int irrespective of the underlying hardware ... please correct me
if I am wrong :) Actually I think to check the bit ness of machine
.... we need to check how much bit processor we are using ... and may
be that we can know if we know the size of the data bus or cpu
registers ... Am I right ??? ... then I suppose sizeof int may not
give compulsorily give the sizeof CPU register ... then how do we
check it using c code???

--Shri
 
B

Ben Pfaff

How to identify if the given machine is 8/16/32 bit using C code ???

How do you define "bitness"? For me the most useful
interpretation from a C language perspective would probably be
the number of value bits in an `unsigned int'. You can figure
this out by examining UINT_MAX in said:
printf("Word length = %d", sizeof(int));

Doesn't that depend on the compiler you are using for compiling that
piece of code??? I think that there are compilers that use 4 bytes
for int irrespective of the underlying hardware ... please correct me
if I am wrong :)

Yes, both of those assumptions are true.
Actually I think to check the bit ness of machine ... we need
to check how much bit processor we are using ... and may be
that we can know if we know the size of the data bus or cpu
registers ... Am I right ??? ... then I suppose sizeof int may
not give compulsorily give the sizeof CPU register ... then how
do we check it using c code???

What are you planning to do with this information? Most of it
is of little interest from a strict C language point of view.
 
M

Michael Mair

Shri said:
How to identify if the given machine is 8/16/32 bit using C code ???

printf("Word length = %d", sizeof(int));

Doesn't that depend on the compiler you are using for compiling that
piece of code??? I think that there are compilers that use 4 bytes
for int irrespective of the underlying hardware ... please correct me
if I am wrong :)

To find out how much bits int uses, rather use
printf("Word length = %d Bits", sizeof(int)*CHAR_BIT);
where you need to #include <limits.h> for CHAR_BIT.
You may have 32 Bit bytes and sizeof(int) == 1.
int was AFAIK _intended_ to be the "natural" processor word.

However, with 64 Bit architectures, you often have int to be 32Bits
Even worse, with C99 you have long long which is at least 64 Bit
-- even on 32 Bit machines. So, the bit length of the data types
will not tell you much about the processor words; with high probability,
the processor words' number of bits will be divisible by CHAR_BIT.
(A similar problem existed and maybe still exists with "castrated"
32 Bit processors and long/int)
Actually I think to check the bit ness of machine
... we need to check how much bit processor we are using ... and may
be that we can know if we know the size of the data bus or cpu
registers ... Am I right ??? ... then I suppose sizeof int may not
give compulsorily give the sizeof CPU register ... then how do we
check it using c code???

As your program will have to be compiled for the respective targets,
use the information available at compile time: The compiler has to
know the target.
Read your compiler manuals. Then, if there are still problems,
ask in the compiler specific newsgroups for more informations.


Cheers
Michael
 
G

Gordon Burditt

How to identify if the given machine is 8/16/32 bit using C code ???

The "bitness" of a particular CPU is defined by the *MARKETING
DEPARTMENT* of the manufacturer of a particular CPU. In other
words, it can (and often is) a bunch of hooey.

There are more objective measures, like the size of CPU general
registers (if it HAS general registers), the size of the data bus,
the size of the address bus, etc. However, these tend to disagree
with each other.

For example, both the 8088 and the 8086 have 16-bit registers and
almost identical instruction sets. But the 8088 has an 8-bit
external data bus and the 8086 has a 16-bit external data bus. A
compiler for the 8086 probably works fine for the 8088. A compiler
for the 8086 probably works fine for the Pentium 4 in real mode:
the programming models are virtually identical. Let's see, is the
data bus 32 or 64 bits wide (or have memory bus widths gone to 128
by now?), or does it vary by specific model? And some models have
a *36* bit-wide address bus. Now, we've got an instruction set
(and compiler that generates code for it) that runs on 8, 16, 32,
or 64-bit hardware without having to know which it's running on.
Why is the bitness of the processor relevant again?
printf("Word length = %d", sizeof(int));

Doesn't that depend on the compiler you are using for compiling that
piece of code???

Yes. And it might even depend on command-line options given to
the compiler.
I think that there are compilers that use 4 bytes
for int irrespective of the underlying hardware ...
please correct me
if I am wrong :)

I consider that to be two different compilers if the instruction sets
are substantially different, even if a great deal of the code is
common between them.
Actually I think to check the bit ness of machine
... we need to check how much bit processor we are using ... and may

"how much bit processor we are using" is FUZZY IN THE EXTREME.
be that we can know if we know the size of the data bus or cpu
registers ... Am I right ??? ... then I suppose sizeof int may not
give compulsorily give the sizeof CPU register ... then how do we
check it using c code???

I think you're going to have a heck of a time checking it using
*ALL* the hardware and software manuals for the processor, the
complete chip design and masks, and all the employees of the
manufacturer that had anything to do with the design of the processor.
You'll *STILL* get arguments.

Gordon L. Burditt
 
M

Michael Mair

Gordon said:
The "bitness" of a particular CPU is defined by the *MARKETING
DEPARTMENT* of the manufacturer of a particular CPU. In other
words, it can (and often is) a bunch of hooey.

There are more objective measures, like the size of CPU general
registers (if it HAS general registers), the size of the data bus,
the size of the address bus, etc. However, these tend to disagree
with each other.

For example, both the 8088 and the 8086 have 16-bit registers and
almost identical instruction sets. But the 8088 has an 8-bit
external data bus and the 8086 has a 16-bit external data bus. A
compiler for the 8086 probably works fine for the 8088. A compiler
for the 8086 probably works fine for the Pentium 4 in real mode:
the programming models are virtually identical. Let's see, is the
data bus 32 or 64 bits wide (or have memory bus widths gone to 128
by now?), or does it vary by specific model? And some models have
a *36* bit-wide address bus. Now, we've got an instruction set
(and compiler that generates code for it) that runs on 8, 16, 32,
or 64-bit hardware without having to know which it's running on.
Why is the bitness of the processor relevant again?




Yes. And it might even depend on command-line options given to
the compiler.




I consider that to be two different compilers if the instruction sets
are substantially different, even if a great deal of the code is
common between them.




"how much bit processor we are using" is FUZZY IN THE EXTREME.




I think you're going to have a heck of a time checking it using
*ALL* the hardware and software manuals for the processor, the
complete chip design and masks, and all the employees of the
manufacturer that had anything to do with the design of the processor.
You'll *STILL* get arguments.

Heck, the poor OP probably just wanted to know a third of that and
will not exactly be reassured by the "(if it HAS general registers)"
part... ;-)
However: Great article!

Cheers
Michael
 
P

pete

Shri said:
How to identify if the given machine is 8/16/32 bit using C code ???

printf("Word length = %d", sizeof(int));

Doesn't that depend on the compiler you are using for compiling that
piece of code???

On the same machine:
when I run Turbo C 2.0, sizeof(int) is 2.
when I run MSVC++ 5.0, sizeof(int) is 4.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top