9. If a sum in binary system is written up to 2 bits , it is equal to 100
TRUE
FALSE
Answers
Answer:
false
please make me brainest.
Explanation:
Given a number n. Find sum of all number upto n whose 2 bits are set.
Examples:
Input : 10
Output : 33
3 + 5 + 6 + 9 + 10 = 33
Input : 100
Output : 762
Recommended: Please try your approach on {IDE} first, before moving on to the solution.
Naive Approach: Find each number upto n whose 2 bits are set. If its 2 bits are set add it to the sum.
// CPP program to find sum of numbers
// upto n whose 2 bits are set
#include <bits/stdc++.h>
using namespace std;
// To count number of set bits
int countSetBits(int n)
{
int count = 0;
while (n) {
n &= (n - 1);
count++;
}
return count;
}
// To calculate sum of numbers
int findSum(int n)
{
int sum = 0;
// To count sum of number
// whose 2 bit are set
for (int i = 1; i <= n; i++)
if (countSetBits(i) == 2)
sum += i;