You are here:

C++/C code to know how many bits processor we are using


QUESTION: Can we write a C code without using a "sizeof()" to know how many bits processor are we using.


---------- FOLLOW-UP ----------

QUESTION: I dont want to find out the size of the variable I want to find out the number of bits the processor is using.

Eg: IF it is a 32bit processor then the size of integer is 4bytes, if it is 16bit then it is 2bytes.

So what I want to ask is how can I know through C code if it is 32bit or 16bit processor. I know it can be done by bitwise operator but want to exactly how can I do it.
ANSWER: The size of int is same as the word size of the processor. So if you understand that SIZE(x) as defined below is the size of a type

#define SIZE(x) ((unsigned int)((char *)(&x + 1) - (char *)&x)),

then word size = SIZE(int)

---------- FOLLOW-UP ----------

QUESTION: Thanks I got it, its really a great idea to implement it like this. Could you please help me on the bitwise operator side.

How can you implement the above using bitwise operator.

I don't have an answer to your question. I thought I can use left shift by 32 bits (<<32) on int=1. After the shift, I  expected to get a 0 in case of word size 32, but I got 1. In case of word size 64, I expect to get a large integer after shift.    

What do you think about this?


All Answers

Answers by Expert:

Ask Experts


Amit Kumar


I can answer Cplusplus language and library questions, including STL, ACE, Boost. I have a good background in Mathematics.


Programming in CPP for about 8 years. Industry experience of 4 years.

Bachelors and Masters in Computer Science from Indian Institute of Technology Delhi.

©2016 All rights reserved.