Number of bits in a hex digit: 4. Number of bits in a byte: 8. Number of bits per C++ datatype on a 32-bit machine:
bits in a char: 8
bits in a short: 16
bits in an int: 32
bits in a long: 32
bits in a long long: 64
bits in a pointer: 32
template <class TYPE>
int count_bits(TYPE t) {
int n_bits=0;
while (t!=0) {
t=t+t; /* double the type value */
n_bits++;
}
return n_bits;
}
int foo(void) {
std::cout<<" bits in a char: "<< count_bits((char)1) <<"\n";
std::cout<<" bits in a short: "<< count_bits((short)1) <<"\n";
std::cout<<" bits in an int: "<< count_bits((int)1) <<"\n";
std::cout<<" bits in a long: "<< count_bits((long)1) <<"\n";
std::cout<<" bits in a long long: "<< count_bits((long long)1) <<"\n";
//std::cout<<" bits in a pointer: "<< count_bits((char *)1) <<"\n";
return 0;
}
(Try this in NetRun now!)