Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Based on http://stackoverflow.com/a/25934909
- static unsigned int const pow10[] = {
- 1,
- 10,
- 100,
- 1000,
- 10000,
- 100000,
- 1000000,
- 10000000,
- 100000000,
- 1000000000,
- };
- unsigned int base2digits(unsigned int x) {
- return x ? 32 - __builtin_clz(x) : 0;
- }
- unsigned int base10digits(unsigned int x) {
- static unsigned int const guess[33] = {
- 0, 0, 0, 0, 1, 1, 1, 2, 2, 2,
- 3, 3, 3, 3, 4, 4, 4, 5, 5, 5,
- 6, 6, 6, 6, 7, 7, 7, 8, 8, 8,
- 9, 9, 9
- };
- unsigned int digits = guess[base2digits(x)];
- return digits + (x >= pow10[digits]);
- }
- unsigned int concat(unsigned int a, unsigned int b) {
- return a * pow10[base10digits(b)] + b;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement