Find the Number Using Bitwise Questions I Last Updated : 14 Jun, 2024 Summarize Suggest changes Share Like Article Like Report Given a task to find a number n. There is a pre-defined API int commonSetBits(int val) that returns the number of bits where both n and val have a value of 1 in the corresponding position of their binary representation. In other words, it returns the number of set bits in the bitwise AND (&) operation of n and val. The task is to return the number n. Example: Input: n = 31Output: 31Explanation: It can be proven that it's possible to find 31 using the provided API. Input: n = 33Output: 33Explanation: It can be proven that it's possible to find 33 using the provided API. Approach: To find the bits of a hidden number using the magic function with binary numbers that have only one 1 in them, we can follow this approach: Get the binary numbers by performing a left shift operation on 1 and adding the numbers as: ∑ for n = 0 to n = 30 (2^n * (1 * bitPresentOrNot) Steps-by-step approach: findNumber Function:This function calculates the number by iterating through the first 31 bits (from 0 to 30).For each bit position i, it checks if commonSetBits(1 << i) is non-zero.If the condition is true, it adds 1 << i (which is equivalent to 2^i) to val.Below is the implementation of the above approach: C++ #include <bits/stdc++.h> using namespace std; // Assuming `Problem` class is defined somewhere with the // function `commonSetBits` For the purpose of this example, // let's define a dummy `Problem` class with // `commonSetBits`. // Dummy implementation of commonSetBits function int commonSetBits(int n) { // Replace this with the actual logic of commonSetBits return n & 1; // For demonstration, let's assume it // returns 1 for odd numbers } int findNumber() { int num = 0; // Loop through the first 31 bits for (int i = 0; i <= 30; i++) { // Check if the common set bits of (1 << i) is not // zero if (commonSetBits(1 << i) != 0) { // Add the value of (1 << i) to num num += 1 << i; } } return num; } // Driver code int main() { int result = findNumber(); cout << "The result is: " << result << endl; return 0; } Java public class Main { // Dummy implementation of commonSetBits function static int commonSetBits(int n) { // Replace this with the actual logic of commonSetBits return n & 1; // For demonstration, let's assume it // returns 1 for odd numbers } static int findNumber() { int num = 0; // Loop through the first 31 bits for (int i = 0; i <= 30; i++) { // Check if the common set bits of (1 << i) is not zero if (commonSetBits(1 << i) != 0) { // Add the value of (1 << i) to num num += 1 << i; } } return num; } // Driver code public static void main(String[] args) { int result = findNumber(); System.out.println("The result is: " + result); } } // This code is contributed by Shivam JavaScript // Dummy implementation of commonSetBits function function commonSetBits(n) { // Replace this with the actual logic of commonSetBits return n & 1; // For demonstration, let's assume it returns 1 for odd numbers } function findNumber() { let num = 0; // Loop through the first 31 bits for (let i = 0; i <= 30; i++) { // Check if the common set bits of (1 << i) is not zero if (commonSetBits(1 << i) !== 0) { // Add the value of (1 << i) to num num += 1 << i; } } return num; } // Driver code let result = findNumber(); console.log("The result is: " + result); // This code is contributed by Rambabu OutputThe result is: 1 Time Complexity: O(1)Auxiliary Space: O(1) Advertise with us Next Article Find the smallest number with n set and m unset bits A anshu_1230 Follow Similar Reads Minimum number using set bits of a given number Given an unsigned number, find the minimum number that could be formed by using the bits of the given unsigned number. Examples : Input : 6 Output : 3 Binary representation of 6 is 0000....0110. Smallest number with same number of set bits 0000....0011. Input : 11 Output : 7 Simple Approach: 1. Find 4 min read Store two numbers in one Byte using Bit manipulation Given two values A and B (A, B < 16), the task is to store the numbers in one byte. Examples: Input: A = 5, B = 9Output: 89Explanation: 5 in binary form is 0101 and the binary form for 9 is 1001. Since 1 byte contains 8 bits, we can store these two nibbles in that 1 bit, forming 01011001 which wh 8 min read Find the smallest number with n set and m unset bits Given two non-negative numbers n and m. The problem is to find the smallest number having n number of set bits and m number of unset bits in its binary representation.Constraints: 1 <= n, 0 <= m, (m+n) <= 31Note : 0 bits before leading 1 (or leftmost 1) in binary representation are counted 7 min read Check if a number is multiple of 9 using bitwise operators Given a number n, write a function that returns true if n is divisible by 9, else false. The most simple way to check for n's divisibility by 9 is to do n%9. Another method is to sum the digits of n. If sum of digits is multiple of 9, then n is multiple of 9. The above methods are not bitwise operat 5 min read Find position of the only set bit Given a number n containing only 1 set bit in its binary representation, the task is to find the position of the only set bit. If there are 0 or more than 1 set bits, then return -1. Note: Position of set bit '1' should be counted starting with 1 from the LSB side in the binary representation of the 8 min read Check if a number is divisible by 8 using bitwise operators Given a number n, check if it is divisible by 8 using bitwise operators. Examples: Input : 16 Output :YES Input :15 Output :NO Recommended PracticeCheck if a number is divisible by 8Try It! Approach: Result = (((n >> 3) << 3) == n). First we shift the 3 bit right then we shift the 3 bit 3 min read Article Tags : Bit Magic DSA Intuit Practice Tags : IntuitBit Magic Like