- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Prime Number of Set Bits in Binary Representation in C++

In this problem, we are given two integers L and R. Our task to print the total numbers that have set bits counting to a prime number that is in between L to R.

Let’s take an example to understand the problem

Input: L = 7, R = 12 Output: 6 Explanation: 7 -> 111 , set bits = 2, prime number. 8 -> 1000 , set bits = 1, not prime number. 9 -> 1001 , set bits = 2, prime number 10 -> 1010 , set bits = 2, prime number 11 -> 1011, set bits = 3, prime number 12 -> 1100, set bits = 2, prime number

To solve this problem, we will traverse each and every element within the range. And check the total number of set bits in the number, for this we will use a predefined function in CPP _builtin_popcount(). Then, we will check the set bits of the number for prime. If yes increase count otherwise not.

Program to show the implementation of our solution

## Example

#include <iostream> using namespace std; bool isPrimeNumber(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n%2 == 0 || n%3 == 0) return false; for (int i=5; i*i<=n; i=i+6) if (n%i == 0 || n%(i+2) == 0) return false; return true; } void printPrimeSetBits(int l, int r) { int tot_bit, count = 0; for (int i = l; i <= r; i++) { tot_bit = __builtin_popcount(i); if (isPrimeNumber(tot_bit)) count++; } cout<<count; } int main() { int L = 7, R = 13; cout<<"Total numbers with prime set bits between "<<L<<" and "<<R<<" are : "; printPrimeSetBits(L, R); return 0; }

## Output

Total numbers with prime set bits between 7 and 13 are : 6

- Related Articles
- Prime Number of Set Bits in Binary Representation in Python
- Binary representation of next number in C++
- Binary representation of previous number in C++
- Binary representation of a given number in C++
- Binary Number with Alternating Bits in C++
- Print the number of set bits in each node of a Binary Tree in C++ Programming.
- Number of leading zeros in binary representation of a given number in C++
- Number of integers with odd number of set bits in C++
- Find the Number of 1 Bits in a large Binary Number in C++
- Minimum number using set bits of a given number in C++
- Next higher number with same number of set bits in C++
- Number of Steps to Reduce a Number in Binary Representation to One in C++
- Count number of trailing zeros in Binary representation of a number using Bitset in C++
- Find consecutive 1s of length >= n in binary representation of a number in C++
- Maximum number of contiguous array elements with same number of set bits in C++

Advertisements