Python/JS/Go/C++ O(n) by bit operation [w/Comment]
Python/JS/Go/C++ O(n) by bit operation \n\n---\n\nDemonstration with n = 3\n\n1 = 0b 1\n2 = 0b 10\n3 = 0b 11\n\nConcatenation from 1 to 3 in binary = 0b 1 10 11
Python/JS/Go/C++ O(n) by bit operation \n\n---\n\nDemonstration with n = 3\n\n1 = 0b 1\n2 = 0b 10\n3 = 0b 11\n\nConcatenation from 1 to 3 in binary = 0b 1 10 11 = 0b 11011 = 27 in decimal\n\n---\n\n**Implementation**:\n\nPython:\n\n```\nclass Solution:\n    def concatenatedBinary(self, n: int) -> int:\n\n        constant = 10**9 + 7\n        summation = 0\n        bit_width = 0\n        \n        # iterate from 1 to n\n        for i in range(1, n+1):\n            \n            # update binary bit width when we meet power of 2\n            if i & i-1 == 0:\n                bit_width += 1\n            \n            # use binary left rotation to implement concatenation\n            summation = summation << bit_width\n            \n            # add with current number\n            summation = summation | i\n            \n            # mod with constant defined by description\n            summation %= constant\n        \n        return summation\n```\n\n---\n\nJavascript\n\n```\nvar concatenatedBinary = function(n) {\n    \n    const constant = 10**9 + 7;\n    let summation = 0;\n    let bit_width = 0;\n\n    // iterate from 1 to n\n    for( let i = 1 ; i <= n ; i++ ){\n\n        // update binary bit width when we meet power of 2\n        if( 0 == (i & i-1) ){\n            bit_width += 1;\n        }\n\n\n        // use binary left rotation to implement concatenation\n        summation = summation * (2 ** bit_width);\n       \n        // add with current number\n        summation = summation + i;\n\n        // mod with constant defined by description\n        summation = summation % constant;\n    }\n\n    return summation;\n        \n};\n```\n\n---\n\nGo:\n\n```\nfunc concatenatedBinary(n int) int {\n        \n    const constant = int(1e9+7)\n    summation := 0\n    bit_width := 0\n\n    // iterate from 1 to n\n    for i := 1 ; i <= n ; i++ {\n\n        // update binary bit width when we meet power of 2\n        if 0 == (i & (i-1) ){\n            bit_width += 1;\n        }\n\n\n        // use binary left rotation to implement concatenation\n        summation = summation << bit_width;\n       \n        // add with current number\n        summation = summation | i;\n\n        // mod with constant defined by description\n        summation = summation % constant;\n    }\n\n    return summation;\n}\n```\n\n---\n\nC++\n\n```\nclass Solution {\npublic:\n    int concatenatedBinary(int n) {\n\n        int constant = 1e9 + 7;\n        long summation = 0;\n        int bit_width = 0;\n        \n        // iterate from 1 to n\n        for( int i = 1 ; i <= n ; i++ ){\n            \n            // update binary bit width when we meet power of 2\n            if( 0 == (i & i-1) ){\n                bit_width += 1;\n            }\n                \n            \n            // use binary left rotation to implement concatenation\n            summation = summation << bit_width;\n            \n            // add with current number\n            summation = summation | i;\n            \n            // mod with constant defined by description\n            summation %= constant;\n        }\n        \n        return int(summation);\n        \n        \n    }\n};\n```\n\n
['Bit Manipulation', 'C', 'Python', 'Go', 'JavaScript']