c - Is there a standard way to detect bit width of hardware? -


Variables of type int is reportedly "a machine-type word in length" but in embedded systems, 8-bit micro 16 Use for interfacing for Beats !, (8 bits for unsigned char) then for more bits, int treats normally: 16 bit microx also has 16 bits, and 32 bit micro int 32 bits In, etc.

So, is there a powerful way to test it, something like BITSIZEOF (int)?

Like "sizeof" for bytes, but for bits

This was my first thought

  register c = 1; Int bitwidth = 0; Do {bitwidth ++; } While (c & lt; = lt; = 1); Printf ("register bit width:% d", bitwidth);  

But it takes c in the form of int, and it is normal in 16 bit to use int in 8 bit compilers, so it gives me 16 results, it looks like That "register width" as "int", (or is not respected)

Why do I have to know it? Suppose I need less than 256 values, so they require 8, 16, 32 bits, but using the right size (similar to memory and registers) will speed up things and save memory and if it does not

Edit After reading the answer, I found this good article

I will quote the conclusion (bold pair )

Thus the bottom line is this if you are skilled, portable Want to start to write embedded code, the first step you need to start using the CML data type 'less than' and 'fast'. If your compiler does not conform to C99 then make a complaint until it is not - or vendors change. If you make this change, then I think you'll be pleasantly surprised at the improvement in code size and speed.

I have to rewrite the function for every architecture

No, you do not use stdint.h of C99, which are the types of uint_fast8_t , which is able to capture 256 values Will happen, and early.

Then, not concerning the forum, the type will change accordingly and you will not change anything in your code. If your platform has no set of definitions, then you can add yourself

Reprinting each function is better than rewriting.


Comments

Popular posts from this blog

c# - sqlDecimal to decimal clr stored procedure Unable to cast object of type 'System.Data.SqlTypes.SqlDecimal' to type 'System.IConvertible' -

Calling GetGUIThreadInfo from Outlook VBA -

Obfuscating Python code? -