Question

In: Computer Science

/* * bitCount - returns count of number of 1's in word * Examples: bitCount(5) =...

/*

* bitCount - returns count of number of 1's in word

* Examples: bitCount(5) = 2, bitCount(7) = 3

* Legal ops: ! ~ & ^ | + << >>

* Max ops: 40

* Rating: 4

*/

int bitCount(int x) {

return 2;

}

Can you please complete this code in C language without the use of any loops such as if, do, while, for, switch, etc., macros, other operations, such as &&, ||, -, or ?: .

Solutions

Expert Solution

//C Code

#include <math.h>
#include <stdio.h>
// Recursive approach to find the
// number of set bit in 1
int recursiveCount(int N)
{

// Base Case
if (N == 0) {
return 0;
}

// Return recursively
return (N & 1) + recursiveCount(N >> 1);
}

// Function to find 1s complement
int onesComplement(int n)
{
// Find number of bits in the
// given integer
int N = floor(log2(n)) + 1;

// XOR the given integer with
// pow(2, N) - 1
return ((1 << N) - 1) ^ n;
}

// Function to count the number of
// 1s in binary representation of N
void bitCount(int N)
{
// Initialise the count variables
int count1;

// Function call to find the number
// of set bits in N
count1 = recursiveCount(N);

// Function call to find 1s complement
N = onesComplement(N);

// Print the count
printf("Count of 1s is %d\n", count1);
}

// Driver Code
int main()
{
int N=7;
  
// Function Call
bitCount(N);
return 0;
}

Code Snapshot:

Output Snapshot:


Related Solutions

Java Question 5: Count elements in the heap Write a function that returns the number of...
Java Question 5: Count elements in the heap Write a function that returns the number of elements in a min heap strictly less than a given number. Method signature: public static int elemNumHeap(PriorityQueue minHeap, int val) Please also include testers.
Count the number of 1’s and 0’s Write a MIPS program that will ask the user...
Count the number of 1’s and 0’s Write a MIPS program that will ask the user to enter an integer, and then output the number of 1’s and 0’s that are present in the integer’s signed 32-bit binary representation. For example, 15 has a binary representation of 0000 0000 0000 0000 0000 0000 0000 1111, which has 28 zeroes and 4 ones. We have provided you the starter code that deals with the input/output logic. The integer input is saved...
how can you count the number of different letters there are in a word in C...
how can you count the number of different letters there are in a word in C programming?
advantaged of making animal testing illegal? supportive ideas and examples for your answers word count :...
advantaged of making animal testing illegal? supportive ideas and examples for your answers word count : 1000-1200 comments : I need references or citations subject : rhetorical and composition course I will give you a thumb up afterwards if all requirements are met. THANK YOU IN ADVANCE ( this is very important , I should write a whole report and argumentative essay about it that carry 35% of my whole grade, so your help is much appreciated)
match word count, include analysis and do not waste word count on extended definitions. question 3...
match word count, include analysis and do not waste word count on extended definitions. question 3 :Explain what is national culture and its main six dimensions with example for each (650 words)....the six national culture dimension : •High versus low power distance •High versus low uncertainty avoidance •Masculinity versus femininity •Collectivism versus Individualism •Long Term versus Short Term Orientation •Indulgence versus Restraint.
WORD COUNT 200 1-Explain what is a yield curve , and how it might be used...
WORD COUNT 200 1-Explain what is a yield curve , and how it might be used by a management accountant working with treasury professionals with Example .
Take a tour of the Soap Factory and describe the process used? (Marks 1) (word count...
Take a tour of the Soap Factory and describe the process used? (Marks 1) (word count maximum: 100) ** Please I do not want answer from internet ** Plagiarism not allowed
Please answer the following Question in 300 word count Please answer in your own Count. if...
Please answer the following Question in 300 word count Please answer in your own Count. if citing source please add reference at the end of question. You are the chief financial officer (CFO) at a community hospital. One of the comments that has come back from patient surveys is the need for a commercial 24-hour pharmacy within the hospital. In this way, patients or their families will be able to fill prescriptions and begin taking ordered medication right away instead...
Please answer the following Question in 300 word count Please answer in your own Count. if...
Please answer the following Question in 300 word count Please answer in your own Count. if citing source please add reference at the end of question. You are the chief financial officer (CFO) at a community hospital. One of the comments that has come back from patient surveys is the need for a commercial 24-hour pharmacy within the hospital. In this way, patients or their families will be able to fill prescriptions and begin taking ordered medication right away instead...
Please answer the following Question in 300 word count Please answer in your own Count. if...
Please answer the following Question in 300 word count Please answer in your own Count. if citing source please add reference at the end of question. You are the chief financial officer (CFO) at a community hospital. One of the comments that has come back from patient surveys is the need for a commercial 24-hour pharmacy within the hospital. In this way, patients or their families will be able to fill prescriptions and begin taking ordered medication right away instead...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT