Is it possible to write any function to find out the dimension of the int type on a specific controller?
1 answer
This is how you can count the number of bits per byte
int CharBit() { unsigned char c = ~0U; int res = 1; for(; c >>= 1; ++res) {} return res; } and then (sizeof (int) * CharBit ()). That is, the number of chars and int'e multiplied by the number of bits in char'e is equal to the number of bits in int'e. For x86: 4 * 8 = 32
Here's another:
int IntBit() { int tmp = 0, res = 0; // 0xfffff... while(++res, tmp >>= 1) {} return res; } - And what is this
constexpr? (especially in C) Anyway,resshould be initialized not by zero, but by 1. - avp - Yes you are right! sorry! corrected! - cipher_web
|
sizeof()gives the size not in bytes, but in the number of elements of typechar(in most cases they coincide with the size in 8-bit bytes). The size of thechartype in bits is defined in<limits.h>and is calledCHAR_BIT. So The number of bits in anintmust be calculated in such a way. sizeof (int) * CHAR_BIT In the same file, constants are defined that describe the maximum and minimum values for different integer types. “However, using known bit operations, they are instantly calculated.” For example, INT_MAX: int int_max_value = ((unsigned) (~ 0)) >> 1; - avp