C++ || 10 4bit counters packet into 64bit integer || fast (0ms)
Please upvote if you like the solution, it motivates me to post more of them\n\nSolution 1: 10 4bit counters packet into a 64bit integer\n\nThis solution is bas
**Please upvote if you like the solution, it motivates me to post more of them**\n\n**Solution 1: 10 4bit counters packet into a 64bit integer**\n\nThis solution is basically do a frequency count. Since any digit (0 to 9) is for sure less frequent than 16 in a 32bit integer we can use 4 bits to count the frequency the digitis. With that we can pack 10 4bit counters easily into a 64bit integer. The solution has to functions:\n\nWe just need to compare the signature of ```n``` with the signature of all powers of 2.\n\n```\n    bool reorderedPowerOf2(int n) {\n        const uint64_t sig_n = signatur(n);\n        // Compare to all powers of 2.\n        for (int i = 0; i < 32; ++i) {\n            if (sig_n == signatur(1 << i)) return true;\n        }\n        return false;\n    }\n```\n\nThe more interesting part is to compute the singatures:\n\n```\n    // We use 4 bits to count each digit.\n    static uint64_t signatur(uint32_t n) {\n        uint64_t sig = 0;\n        while (n) {\n            sig += 1UL << ((n % 10) * 4);\n            n /= 10;\n        }\n        return sig;\n    }\n```\n\nAlternative to an ```uint64_t``` signature we could have used an ```array<char, 10>``` (or a similar structure) or just use a ```string``` and just sort the digits.\n\nAs always feedback is welcome.
['Bit Manipulation', 'C']