c++ || bit manipulation || fully explained solution
Countbits function returns the number of bits in the number n\nLet\'s understand this with an example, \n\nWe will take the example of a decimal number(base 10)
Countbits function returns the number of bits in the number n\nLet\'s understand this with an example, \n\nWe will take the example of a decimal number(base 10).\nIf we take log10(n)+1 it will return the number of digits in the base 10.\n\nFor example, \n\nlog10(121)+1 = 3\nlog10(87)+1 = 2\nlog10(2)+1 = 1\n\nNow, the same goes for a log base 2 in a binary system.\nlog2(5)+1 = 3\nlog2(3)+1 = 2\nlog2(1)+1 = 2\n\nOnce we have the number of bits, we can continue ahead\n\n1. we just left shift our temp variable by the number of bits in ith number\n2. then we xor temp with i\n\nFor example,\n\ntemp=0;\nn=2;\ni->1 to 2;\nFor i=1,\n(temp<<1)^i -> (0<<1)^1 -> (00)^01 -> 01\ntemp = 01\n\nnext ,\n\nFor i=2,\n(temp<<2)^i -> (01<<2)^2 -> (0100)^10 -> 0110\ntemp = 110 -> 6\nso 6 will be the answer in this way\n\n```\nclass Solution {\npublic:\n    \n    long long int MOD = 1e9+7;\n    \n    int countbits(int n){\n        return log2(n)+1;\n    }\n    \n    int concatenatedBinary(int n) {\n        \n        long long temp = 0;\n        for(int i=1;i<=n;i++) temp = ((temp<<countbits(i))^i)%MOD;\n        return temp%MOD;\n        \n    }\n};\n```
['Bit Manipulation', 'C']