Find all powers of 2 less than or equal to a given number Last Updated : 24 Apr, 2023 Comments Improve Suggest changes Like Article Like Report Given a positive number N, the task is to find out all the perfect powers of two which are less than or equal to the given number N. Examples: Input: N = 63 Output: 32 16 8 4 2 1 Explanation: There are total of 6 powers of 2, which are less than or equal to the given number N. Input: N = 193 Output: 128 64 32 16 8 4 2 1 Explanation: There are total of 8 powers of 2, which are less than or equal to the given number N. Naive Approach: The idea is to traverse each number from N to 1 and check if it is a perfect power of 2 or not. If yes, then print that number. Another Approach: The idea is to find all powers of 2 and simply print the powers that are lesser than or equal to N. Another Approach: The idea is based on the concept that all powers of 2 has all bits set, in its binary form. Bitset function is used in this approach solve the above problem. Below are the steps: Find the largest power of 2(say temp) which is used to evaluate the number less than or equal to N.Initialise an bitset array arr[] of maximum size 64, to store the binary representation of the given number N.Reset all the bits in the bitset array using reset() function.Iterate a loop from total to 0, and sequentially make each bit 1, and find the value of that binary expression and then reset the bit. Below is the implementation of the above approach: CPP // C++ program for the above approach #include <bits/stdc++.h> using namespace std; const int MAX = 64; // Function to return max exponent of // 2 which evaluates a number less // than or equal to N int max_exponent(int n) { return (int)(log2(n)); } // Function to print all the powers // of 2 less than or equal to N void all_powers(int N) { bitset<64> arr(N); // Reset all the bits arr.reset(); int total = max_exponent(N); // Iterate from total to 0 for (int i = total; i >= 0; i--) { // Reset the next bit arr.reset(i + 1); // Set the current bit arr.set(i); // Value of the binary expression cout << arr.to_ulong() << " "; } } // Driver Code int main() { // Given Number int N = 63; // Function Call all_powers(N); return 0; } Java // Java program for the above approach import java.util.*; class GFG { // Function to return max exponent of // 2 which evaluates a number less // than or equal to N static int max_exponent(int n) { return (int)(Math.log(n) / Math.log(2)); } // Function to print all the powers // of 2 less than or equal to N static void all_powers(int N) { int total = max_exponent(N); // Reset all the bits char[] arr = new char[total + 2]; for (int i = 0; i <= total + 1; i++) arr[i] = '0'; // Iterate from total to 0 for (int i = 1; i < total + 2; i++) { // Reset the next bit arr[i - 1] = '0'; // Set the current bit arr[i] = '1'; // Value of the binary expression System.out.print( Integer.parseInt(new String(arr), 2) + " "); } System.out.print("\n"); } // Driver Code public static void main(String[] args) { // Given Number int N = 63; // Function Call all_powers(N); } } // This code is contributed by phasing17. Python3 # Python3 program for the above approach from math import log MAX = 64; # Function to return max exponent of # 2 which evaluates a number less # than or equal to N def max_exponent(n): return int(log(n,2)) # Function to print all the powers # of 2 less than or equal to N def all_powers(N): total = max_exponent(N); # Reset all the bits arr = ['0' for _ in range(total + 2)] # Iterate from total to 0 for i in range(1, total + 2): # Reset the next bit arr[i - 1] = '0'; # Set the current bit arr[i] = '1'; # Value of the binary expression print( int("".join(arr), 2), end = " ") print() # Driver Code # Given Number N = 63; # Function Call all_powers(N) # This code is contributed by phasing17. C# // C# program for the above approach using System; using System.Collections.Generic; class GFG { // Function to return max exponent of // 2 which evaluates a number less // than or equal to N static int max_exponent(int n) { return (int)(Math.Log(n) / Math.Log(2)); } // Function to print all the powers // of 2 less than or equal to N static void all_powers(int N) { int total = max_exponent(N); // Reset all the bits char[] arr = new char[total + 2]; for (int i = 0; i <= total + 1; i++) arr[i] = '0'; // Iterate from total to 0 for (int i = 1; i < total + 2; i++) { // Reset the next bit arr[i - 1] = '0'; // Set the current bit arr[i] = '1'; // Value of the binary expression Console.Write(Convert.ToInt32(new string(arr), 2) + " "); } Console.Write("\n"); } // Driver Code public static void Main(string[] args) { // Given Number int N = 63; // Function Call all_powers(N); } } // This code is contributed by phasing17. JavaScript // JS program for the above approach let MAX = 64; // Function to return max exponent of // 2 which evaluates a number less // than or equal to N function max_exponent(n) { return Math.floor(Math.log(n) / Math.log(2)) } // Function to print all the powers // of 2 less than or equal to N function all_powers(N) { let total = max_exponent(N); let arr = new Array(total + 1); // Reset all the bits arr.fill(0) // Iterate from total to 0 for (var i = total; i >= 0; i--) { // Reset the next bit arr[i + 1] = 0; // Set the current bit arr[i] = 1; // Value of the binary expression process.stdout.write( parseInt(arr.join(""), 2)/2 + " "); } } // Driver Code // Given Number let N = 63; // Function Call all_powers(N); // This code is contributed by phasing17. Output:32 16 8 4 2 1 Time Complexity: O(log N) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Powers of 2 to required sum S shauryarehangfg Follow Improve Article Tags : Algorithms Mathematical Competitive Programming C++ Programs DSA Arrays setBitCount Bit Algorithms maths-power +5 More Practice Tags : AlgorithmsArraysMathematical Similar Reads Bit Manipulation for Competitive Programming Bit manipulation is a technique in competitive programming that involves the manipulation of individual bits in binary representations of numbers. It is a valuable technique in competitive programming because it allows you to solve problems efficiently, often reducing time complexity and memory usag 15+ min read Count set bits in an integer Write an efficient program to count the number of 1s in the binary representation of an integer.Examples : Input : n = 6Output : 2Binary representation of 6 is 110 and has 2 set bitsInput : n = 13Output : 3Binary representation of 13 is 1101 and has 3 set bits[Naive Approach] - One by One CountingTh 15+ min read Count total set bits in first N Natural Numbers (all numbers from 1 to N) Given a positive integer n, the task is to count the total number of set bits in binary representation of all natural numbers from 1 to n. Examples: Input: n= 3Output: 4Explanation: Numbers from 1 to 3: {1, 2, 3}Binary Representation of 1: 01 -> Set bits = 1Binary Representation of 2: 10 -> Se 9 min read Check whether the number has only first and last bits set Given a positive integer n. The problem is to check whether only the first and last bits are set in the binary representation of n.Examples: Input : 9 Output : Yes (9)10 = (1001)2, only the first and last bits are set. Input : 15 Output : No (15)10 = (1111)2, except first and last there are other bi 4 min read Shortest path length between two given nodes such that adjacent nodes are at bit difference 2 Given an unweighted and undirected graph consisting of N nodes and two integers a and b. The edge between any two nodes exists only if the bit difference between them is 2, the task is to find the length of the shortest path between the nodes a and b. If a path does not exist between the nodes a and 7 min read Calculate Bitwise OR of two integers from their given Bitwise AND and Bitwise XOR values Given two integers X and Y, representing Bitwise XOR and Bitwise AND of two positive integers, the task is to calculate the Bitwise OR value of those two positive integers.Examples:Input: X = 5, Y = 2 Output: 7 Explanation: If A and B are two positive integers such that A ^ B = 5, A & B = 2, the 7 min read Unset least significant K bits of a given number Given an integer N, the task is to print the number obtained by unsetting the least significant K bits from N. Examples: Input: N = 200, K=5Output: 192Explanation: (200)10 = (11001000)2 Unsetting least significant K(= 5) bits from the above binary representation, the new number obtained is (11000000 4 min read Find all powers of 2 less than or equal to a given number Given a positive number N, the task is to find out all the perfect powers of two which are less than or equal to the given number N. Examples: Input: N = 63 Output: 32 16 8 4 2 1 Explanation: There are total of 6 powers of 2, which are less than or equal to the given number N. Input: N = 193 Output: 6 min read Powers of 2 to required sum Given an integer N, task is to find the numbers which when raised to the power of 2 and added finally, gives the integer N. Example : Input : 71307 Output : 0, 1, 3, 7, 9, 10, 12, 16 Explanation : 71307 = 2^0 + 2^1 + 2^3 + 2^7 + 2^9 + 2^10 + 2^12 + 2^16 Input : 1213 Output : 0, 2, 3, 4, 5, 7, 10 Exp 10 min read Print bitwise AND set of a number N Given a number N, print all the numbers which are a bitwise AND set of the binary representation of N. Bitwise AND set of a number N is all possible numbers x smaller than or equal N such that N & i is equal to x for some number i. Examples : Input : N = 5Output : 0, 1, 4, 5 Explanation: 0 & 8 min read Like