#include <iostream>
unsigned count_digits( unsigned value, unsigned base = 10 )
{
unsigned n_digits = 0 ;
do
{
++ n_digits;
value / = base;
} while ( value) ;
return n_digits;
}
void print_digit_count( unsigned value)
{
std:: cout << "In base 10, " << value << " has " << count_digits( value) << " digits.\n " ;
std:: cout << "In base 2, " << value << " has " << count_digits( value, 2 ) << " digits.\n " ;
std:: cout << "In base 16, " << value << " has " << count_digits( value, 16 ) << " digits.\n \n " ;
}
int main( )
{
print_digit_count( 56 ) ;
print_digit_count( 0 ) ;
print_digit_count( 1 ) ;
print_digit_count( 128 ) ;
print_digit_count( 0xFFFF ) ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgoKdW5zaWduZWQgY291bnRfZGlnaXRzKHVuc2lnbmVkIHZhbHVlLCB1bnNpZ25lZCBiYXNlID0gMTApCnsKICAgIHVuc2lnbmVkIG5fZGlnaXRzID0gMDsKICAgIGRvCiAgICB7CiAgICAgICAgKytuX2RpZ2l0czsKICAgICAgICB2YWx1ZSAvPSBiYXNlOwogICAgfSB3aGlsZSAodmFsdWUpOwoKICAgIHJldHVybiBuX2RpZ2l0czsKfQoKdm9pZCBwcmludF9kaWdpdF9jb3VudCh1bnNpZ25lZCB2YWx1ZSkKewogICAgc3RkOjpjb3V0IDw8ICJJbiBiYXNlIDEwLCAiIDw8IHZhbHVlIDw8ICIgaGFzICIgPDwgY291bnRfZGlnaXRzKHZhbHVlKSA8PCAiIGRpZ2l0cy5cbiI7CiAgICBzdGQ6OmNvdXQgPDwgIkluIGJhc2UgMiwgIiA8PCB2YWx1ZSA8PCAiIGhhcyAiIDw8IGNvdW50X2RpZ2l0cyh2YWx1ZSwgMikgPDwgIiBkaWdpdHMuXG4iOwogICAgc3RkOjpjb3V0IDw8ICJJbiBiYXNlIDE2LCAiIDw8IHZhbHVlIDw8ICIgaGFzICIgPDwgY291bnRfZGlnaXRzKHZhbHVlLCAxNikgPDwgIiBkaWdpdHMuXG5cbiI7Cn0KCmludCBtYWluKCkKewogICAgcHJpbnRfZGlnaXRfY291bnQoNTYpOwogICAgcHJpbnRfZGlnaXRfY291bnQoMCk7CiAgICBwcmludF9kaWdpdF9jb3VudCgxKTsKICAgIHByaW50X2RpZ2l0X2NvdW50KDEyOCk7CiAgICBwcmludF9kaWdpdF9jb3VudCgweEZGRkYpOwp9
stdout
In base 10, 56 has 2 digits.
In base 2, 56 has 6 digits.
In base 16, 56 has 2 digits.
In base 10, 0 has 1 digits.
In base 2, 0 has 1 digits.
In base 16, 0 has 1 digits.
In base 10, 1 has 1 digits.
In base 2, 1 has 1 digits.
In base 16, 1 has 1 digits.
In base 10, 128 has 3 digits.
In base 2, 128 has 8 digits.
In base 16, 128 has 2 digits.
In base 10, 65535 has 5 digits.
In base 2, 65535 has 16 digits.
In base 16, 65535 has 4 digits.