Advertisement
szaszm01

unsigned int concat

Apr 28th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.66 KB | None | 0 0
  1. // Based on http://stackoverflow.com/a/25934909
  2.  
  3. static unsigned int const pow10[] = {
  4.     1,
  5.     10,
  6.     100,
  7.     1000,
  8.     10000,
  9.     100000,
  10.     1000000,
  11.     10000000,
  12.     100000000,
  13.     1000000000,
  14. };
  15.  
  16. unsigned int base2digits(unsigned int x) {
  17.     return x ? 32 - __builtin_clz(x) : 0;
  18. }
  19.  
  20. unsigned int base10digits(unsigned int x) {
  21.     static unsigned int const guess[33] = {
  22.         0, 0, 0, 0, 1, 1, 1, 2, 2, 2,
  23.         3, 3, 3, 3, 4, 4, 4, 5, 5, 5,
  24.         6, 6, 6, 6, 7, 7, 7, 8, 8, 8,
  25.         9, 9, 9
  26.     };
  27.     unsigned int digits = guess[base2digits(x)];
  28.     return digits + (x >= pow10[digits]);
  29. }
  30.  
  31. unsigned int concat(unsigned int a, unsigned int b) {
  32.     return a * pow10[base10digits(b)] + b;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement