Find the number obtained by concatenating binary representations of all numbers up to N Last Updated : 25 May, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report Given an integer N, the task is to find the decimal value of the binary string formed by concatenating the binary representations of all numbers from 1 to N sequentially. Examples: Input: N = 12Output: 118505380540Explanation: The concatenation results in "1101110010111011110001001101010111100". The equivalent decimal value is 118505380540. Input: N = 3Output: 27Explanation: In binary, 1, 2, and 3 correspond to "1", "10", and "11". Their concatenation results in "11011", which corresponds to the decimal value of 27. Approach: The idea is to iterate over the range [1, N]. For every ith number, concatenate the binary representation of the number i using the Bitwise XOR property. Follow the steps below to solve the problem: Initialize two variables, l, and ans with 0, where l stores the current position of the bit in the final binary string of any ith number and ans will store the final answer.Iterate from i = 1 to N + 1.If (i & ( i - 1 )) is equal to 0, then simply increment the value of l by 1, where & is the Bitwise AND operator.After that, the left shift ans by l and then bitwise OR the result with i.After and traversing, print ans as the answer. Below is the implementation of the above approach: C++ // C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find the decimal value by // concatenating the numbers from 1 to N int concatenatedBinary(int n) { // Stores count of // bits in a number int l = 0; // Stores decimal value by // concatenating 1 to N int ans = 0; // Iterate over the range [1, n] for (int i = 1; i < n + 1; i++){ // If i is a power of 2 if ((i & (i - 1)) == 0) l += 1; // Update ans ans = ((ans << l) | i); } // Return ans return ans; } // Driver Code int main() { int n = 3; // Function Call cout << (concatenatedBinary(n)); return 0; } // This code is contributed by mohiy kumar 29 Java // Java program for the above approach class GFG { // Function to find the decimal value by // concatenating the numbers from 1 to N static int concatenatedBinary(int n) { // Stores count of // bits in a number int l = 0; // Stores decimal value by // concatenating 1 to N int ans = 0; // Iterate over the range [1, n] for (int i = 1; i < n + 1; i++){ // If i is a power of 2 if ((i & (i - 1)) == 0) l += 1; // Update ans ans = ((ans << l) | i); } // Return ans return ans; } // Driver Code public static void main (String[] args) { int n = 3; // Function Call System.out.println(concatenatedBinary(n)); } } // This code is contributed by AnkThon Python3 # Python program for the above approach # Function to find the decimal value by # concatenating the numbers from 1 to N def concatenatedBinary(n): # Stores count of # bits in a number l = 0 # Stores decimal value by # concatenating 1 to N ans = 0 # Iterate over the range [1, n] for i in range(1, n + 1): # If i is a power of 2 if i & (i - 1) == 0: # Update l l += 1 # Update ans ans = ((ans << l) | i) # Return ans return(ans) # Driver Code if __name__ == '__main__': n = 3 # Function Call print(concatenatedBinary(n)) C# // C# program to implement // the above approach using System; class GFG { // Function to find the decimal value by // concatenating the numbers from 1 to N static int concatenatedBinary(int n) { // Stores count of // bits in a number int l = 0; // Stores decimal value by // concatenating 1 to N int ans = 0; // Iterate over the range [1, n] for (int i = 1; i < n + 1; i++) { // If i is a power of 2 if ((i & (i - 1)) == 0) l += 1; // Update ans ans = ((ans << l) | i); } // Return ans return ans; } // Driver Code public static void Main () { int n = 3; // Function Call Console.WriteLine(concatenatedBinary(n)); } } // This code is contributed by sanjoy_62 JavaScript <script> //Javascript program to implement // the above approach // Function to find the decimal value by // concatenating the numbers from 1 to N function concatenatedBinary(n) { // Stores count of // bits in a number var l = 0; // Stores decimal value by // concatenating 1 to N var ans = 0; // Iterate over the range [1, n] for (var i = 1; i < n + 1; i++){ // If i is a power of 2 if ((i & (i - 1)) == 0) l += 1; // Update ans ans = parseInt((ans << l) | i); } // Return ans return ans; } var n = 3; // Function Call document.write(concatenatedBinary(n)); //This code is contributed by SoumikMondal </script> Output: 27 Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Find the number obtained after concatenation of binary representation of M and N S saikumarkudikala Follow Improve Article Tags : DSA interview-preparation binary-representation Numbers Bitwise-OR Bitwise-AND +2 More Practice Tags : Numbers Similar Reads Find the number obtained after concatenation of binary representation of M and N Given two integers M and N the task is to find the number formed by concatenating the binary equivalents of M and N i.e. M + N. Examples: Input: M = 4, N = 5 Output: 37 Binary equivalent of 4 is 100 and for 5 it is 101 after concatenation, the resultant binary number formed is 100101 whose decimal e 11 min read Distinct Numbers obtained by generating all permutations of a Binary String Given a binary string S, the task is to print all distinct decimal numbers that can be obtained by generating all permutations of the binary string. Examples: Input: S = "110"Output: {3, 5, 6}Explanation: All possible permutations are {"110", "101", "110", "101", "011", "011"}.Equivalent decimal num 8 min read Sum of decimals that are binary representations of first N natural numbers Given a positive integer N, the task is to calculate the sum of all decimals which can be expressed as binary representations of first N natural numbers. Examples: Input: N = 3Output: 22Explanation:The Binary Representation of 1 is 01.The Binary Representation of 2 is 10.The Binary Representation of 9 min read 1 to n bit numbers with no consecutive 1s in binary representation. Given a number n, our task is to find all 1 to n bit numbers with no consecutive 1s in their binary representation. Examples: Input : n = 4 Output : 1 2 4 5 8 9 10 These are numbers with 1 to 4 bits and no consecutive ones in binary representation. Input : n = 3 Output : 1 2 4 5 Recommended: Please 6 min read Minimum number of Binary strings to represent a Number Given a number N. The task is to find the minimum number of binary strings required to represent the given number as the sum of the binary strings.Examples: Input : 131 Output : Minimum Number of binary strings needed: 3 111 10 10 Input : 564 Output :Minimum Number of binary strings needed: 6 111 11 7 min read Binary representation of next greater number with same number of 1's and 0's Given a binary input that represents binary representation of positive number n, find binary representation of smallest number greater than n with same number of 1's and 0's as in binary representation of n. If no such number can be formed, print "no greater number".The binary input may be and may n 12 min read Like