Question

In: Computer Science

Problem: Write a C/C++ program to implement a function that returns 1 when an integer x...

Problem: Write a C/C++ program to implement a function that returns 1 when an integer x contains an odd number
of 1s (in its binary representation) and 0 otherwise. You can consider x as a four byte integer, i.e. w = 32 bits.


Constraint: Your solution can use at most 12 arithmetic, bitwise, and logical operations.

Solutions

Expert Solution

To solve the given problem I created a function in C++ called oddBits() which takes the given no. as argument. Then based on the no. of set bits i.e. no. of 1's in the binary representation of the no. the while loop in the function updates the variable count.

Now after the while loop we check whether the count of 1's in the number is odd or even and based on that it returns 1 or 0 as output.

I have used the conditional operator in the code which is equivalent to 'if-else' block in C++. So if there is any problem understanding that part of the solution feel free to ask in the comments section.

Based on the given constraints I have come up with the following C++ code:

Code:

#include<iostream>
using namespace std;

int oddBits(int num)
{
int count=0; // initialisation of count to '0' is important
while(num!=0)
{
num&=(num-1);
count++;
}

return count%2==1?1:0; // conditional operator
}

int main()
{
int n;
cout<<"Enter no.: "; // taking the number from the user
cin>>n;
int x=oddBits(n);

cout<<x;

}

Some of the outputs based on the above code are:

As the binary representation of '3' contains only 2 1's which are even in no. and hence the output is '0'.

As the binary representation of '14' contains 3 1's which are odd in no. and hence the output is '1'.

For any doubt in code or solution do comment in the comments sections.


Related Solutions

Using C++ Write a template function that accepts an integer parameter and returns its integer square...
Using C++ Write a template function that accepts an integer parameter and returns its integer square root. The function should return -1, if the argument passed is not integer. Demonstrate the function with a suitable driver program .
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for...
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for Bubble Sort b. Use a dynamic array of integers in a variable size of n. c. Display the following information: 1) Total counts of comparisons 2) Total counts of shifts / moves / swaps, whichever applies d. Write a main() function to test a best, and an average cases in terms of time efficiency i. Fill out the array with random numbers for an...
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a...
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a weighted sum of that numbers digits. The last digit of the number (the ones digit) has a weight of 1, so should be added to the sum "as is". The second from last digit (the tens digit) has a weight of 2, and so should be multiplied by 2 then added to the sum. The third from last digit should be multiplied by 1...
Write a c program Write a function to swap two elements of an integer array. Call...
Write a c program Write a function to swap two elements of an integer array. Call the function to swap the first element, i[0] with last element i[n], second element i[1] with the last but one element i[n-1] and so on. Should handle arrays with even and odd number of elements. Call the swap function with the following arrays and print results in each case before and after swapping. i. int arr1[] = {0, 1, 2, 3, 30, 20, 10,...
Program this in C thank you PROBLEM DESCRIPTION: Write a program to implement the following requirement:...
Program this in C thank you PROBLEM DESCRIPTION: Write a program to implement the following requirement: The program will read from standard input two things - a string str1 on the first line of stdin (this string may be an empty string) - a string str2 on the second line of stdin (this string may be an empty string) Note that stdin does not end with '\n'. The program will output a string that is the concatenation of string str1...
[C] Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:
6.19 LAB: Convert to binary - functionsWrite a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is:As long as x is greater than 0    Output x % 2 (remainder is either 0 or 1)    x = x / 2Note: The above algorithm outputs the 0's and 1's in reverse order. You will need to write a second function to reverse the string.Ex: If the input is:6the output is:110Your program must define and call the following two functions. The IntegerToReverseBinary function...
Problem 1 Write a program that prompts the user to enter an integer It then tells...
Problem 1 Write a program that prompts the user to enter an integer It then tells the user if the integers is a multiple of 2, 3, 5, 7 or none of the above. Program language is C Ex. Enter an integer 12 You entered 12 The number you entered is a multiple of 2 ----------------------------------------------- Enter an integer 11 You entered 11 The number you entered is not a multiple of 2, 3, 5, or 7
In this task you will implement a C++ function with arguments: a sorted integer array, size...
In this task you will implement a C++ function with arguments: a sorted integer array, size of the array, and a target integer value. Find all combinations of two elements in the sorted array which sum up to the target value. When found, add the combinations into an array, and print it. Where there is greater than one combination, you may use the number 200 as a separator for the combinations in the output array. Do not use more than...
Write a C++ program that prompts the user (or “Player 1”) to input an integer value...
Write a C++ program that prompts the user (or “Player 1”) to input an integer value between 1 and 3 (where 1=paper, 2=scissor, and 3=rock). This input should be passed into a string function called player_RPS(), and returns a string value indicating the selection of paper, scissors, or rock (as mentioned above). Next, the returned string value, along with a generated input from the computer, should be passed into a void function called RPS_comparison(), and determines whether the user’s input...
Question : Write a C program that asks the user to enter an integer between 1...
Question : Write a C program that asks the user to enter an integer between 1 and 7 that represents a weekday number (1 = Sunday, 2 = Monday , …… , 6 = Friday , 7 = Saturday) and it prints the day (i.e., Sunday, Monday, …… , Friday or Saturday). The program continuously asks the user to enter a weekday number till the user responds by ‘N’. and give me an output please use printf and scanf #include...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT