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

C ++ program that uses a function “IsDivisibleBy5” that returns a 1 if the input integer...
C ++ program that uses a function “IsDivisibleBy5” that returns a 1 if the input integer is evenly divisible by 5. Return 0 otherwise. Also include a function “IsOdd()” that returns a 1 if the input integer is odd. Return 0 otherwise.
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,...
Write a C code program to implement the comparisons of three integer numbers, using only conditional...
Write a C code program to implement the comparisons of three integer numbers, using only conditional operators (no if statements). Please ask the user to input random three integers. Then display the minimum, middle, and maximum number in one line. Also, please calculate and display the sum and the average value (save two digits after decimal point) of these three integers. Please write the comments line by line.
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...
Write a function convert_date that takes an integer as a parameter and returns three integer values...
Write a function convert_date that takes an integer as a parameter and returns three integer values representing the input converted into days, month and year (see the function docstring). Write a program named t03.py that tests the function by asking the user to enter a number and displaying the output day, month and year. Save the function in a PyDev library module named functions.py A sample run for t03.py: Enter a date in the format MMDDYYYY: 05272017 The output will...
[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: Write a C++ program that will implement and test the five functions described below that...
Problem: Write a C++ program that will implement and test the five functions described below that use pointers and dynamic memory allocation. The Functions: You will write the five functions described below. Then you will call them from the main function, to demonstrate their correctness. 1. minimum: takes an int array and the array's size as arguments. It should return the minimum value of the array elements. Do not use square brackets anywhere in the function, not even the parameter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT