I have two tasks for an assignment, one return the number of bits in type int on any machine. I thought I would write my function like so:
int CountIntBitsF() {
int x = sizeof(int) / 8;
return x;
}
Does that look right?
The second part is to return the number of any bits of any data type with a macro, and the macro can be taken from limits.h. I looked up limits.h on my machine, and also http://www.opengroup.org/onlinepubs/007908799/xsh/limits.h.html, but I don't think I really understand how any of those would return the num开发者_运维问答ber of bits in any data type. Any thoughts? Thanks.
The fundamental unit of storage is a char. It is not always 8 bits wide. CHAR_BIT is defined in limits.h and has the number of bits in a char.
It's *
, not /
.
As for the second part, see the "Numerical Limits" section.
If you want the number of bits used to store an int in memory, use Justin's answer, sizeof(int)*CHAR_BIT
. If you want to know the number of bits used in the value, use slebetman's answer.
Although to get the bits in an INT, you should probably use INT_MAX rather than UINT_MAX. I can't remember whether C99 actually guarantees that int
and unsigned int
are the same width, or just that they're the same storage size. I suspect only the latter, since in 6.2.6.2 we have "if there are M value bits in the signed type and N in the unsigned type, then M <= N", not "M = N or M = N-1".
In practice, integral types don't have padding bits in any implementation I've used, so you most likely get the same answer for all, +/- 1 for the sign bit.
In limits.h
, UINT_MAX is the maximum value for an object of type unsigned int. Which means it is an int with all bits set to 1. So, counting the number of bits in an int:
#include <limits.h>
int intBits () {
int x = INT_MAX;
int count = 2; /* start from 1 + 1 because we assume
* that sign uses a single bit, which
* is a fairly reasonable assumption
*/
/* Keep shifting bits to the right until none is left.
* We use divide instead of >> here since I personally
* know some compilers which does not shift in zero as
* the topmost bit
*/
while (x = x/2) count++;
return count;
}
With g++ -O2 this function evaluates to an inline constant:
#include <climits>
#include <stddef.h>
#include <stdint.h>
#include <cstdio>
template <typename T>
size_t num_bits()
{
return sizeof (T) * (CHAR_BIT);
}
int main()
{
printf("uint8_t : %d\n", num_bits<uint8_t>());
printf("size_t : %d\n", num_bits<size_t>());
printf("long long : %d\n", num_bits<long long>());
printf("void* : %d\n", num_bits<void*>());
printf("bool : %d\n", num_bits<bool>());
printf("float : %d\n", num_bits<float>());
printf("double : %d\n", num_bits<double>());
printf("long double : %d\n", num_bits<long double>());
return 0;
}
outputs:
uint8_t : 8
size_t : 32
long long : 64
void* : 32
bool : 8
float : 32
double : 64
long double : 96
Generated X86 32-bit assember:
---SNIP---
movl $32, 8(%esp) <--- const $32
movl $.LC1, 4(%esp)
movl $1, (%esp)
call __printf_chk
movl $64, 8(%esp) <--- const $64
movl $.LC2, 4(%esp)
movl $1, (%esp)
call __printf_chk
---SNIP---
Are you sure you want number of bits, not number of bytes? In C, for a given type T
, you can find the number of bytes it takes by using the sizeof
operator. The number of bits in a byte is CHAR_BIT
, which usually is 8, but can be different.
So, given a type T
, the number of bits in an object of type T
is:
#include <limits.h>
size_t nbits = sizeof(T) * CHAR_BIT
Note that, except for unsigned char
type, all possible combinations of nbits
bits above may not represent a valid value of type T
.
For the second part, note that you can apply sizeof
operator to an object as well as a type. In other words, given a type T
and an object x
of such type:
T x;
You can find the size of T by sizeof(T)
, and the size of x
by sizeof x
. The parentheses are optional if sizeof
is used for an object.
Given the information above, you should be able to answer your second question. Ask again if you still have issues.
Your formula is incorrect: instead of dividing sizeof(int)
by 8
, you should multiply the number of bytes in an int
(sizeof(int)
) by the number of bits in a byte, which is indeed defined in <limits.h>
as the value of macro CHAR_BIT
.
Here is the corrected function:
#include <limits.h>
int CountIntBitsF(void) {
return sizeof(int) * CHAR_BIT;
}
精彩评论