Question

/* * bitSum8bit - returns count of number of 1's in a byte * Examples: bitSum8bit(257)...

/*

* bitSum8bit - returns count of number of 1's in a byte

* Examples: bitSum8bit(257) = 2, bitSum8bit(7) = 3

* Legal ops: & ( ) >> +

* Do not use a loop!

*/

int bitSum8bit(int input)

{

  // These are all lines of code that you need.

  int mask = ;

  int quarterSum = ;

  int mask2 = ;

  int halfSum = ;

  int mask3 = ;

  return ;

}

Homework Answers

Answer #1

// C program to return count of number of 1's in a byte
#include <stdio.h> 
  
int num_to_bits[16] = {
    0, 1, 1, 2, 1, 2, 2, 3,1, 2, 2, 3, 2, 3, 3, 4
}; 
  
/* Recursively get nibble of a given number  
  and map them in the array  */
unsigned int bitSum8bit(unsigned int input) 
{ 
    int nibble = 0; 
    if (0 == input) 
        return num_to_bits[0]; 
  
    // Find last nibble 
    nibble = input & 0xf; 
  
    // Use pre-stored values to find count 
    // in last nibble plus recursively add 
    // remaining nibbles. 
    return num_to_bits[nibble] + bitSum8bit(input >> 4); 
} 
  
int main() 
{ 
    int input = 257; 
    printf("%d\n", bitSum8bit(input)); 
} 
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
IN C /* * bitParity8bit - returns 1 if the number of 1's in a byte...
IN C /* * bitParity8bit - returns 1 if the number of 1's in a byte is odd, * else it returns 0   * Examples: bitParity8bit(257) = 0, bitParity8bit(7) = 1 * Legal ops: & ( ) >> + * Do not use a loop! */ int bitParity8bit(int input) {   // These are all lines of code that you need.   int mask = ;   int quarterParity = ;   int mask2 = ;   int halfParity = ;   int mask3 = ;...
complete the following c puzzles using the listed operators. no data control structures allowed (for, if,...
complete the following c puzzles using the listed operators. no data control structures allowed (for, if, etc.) /* * logicalShift - shift x to the right by n, using a logic\ al shift * Can assume that 0 <= n <= 31 * Examples: logicalShift(0x87654321,4) = 0x08765432 * Legal ops: ! ~ & ^ | + << >> * Max ops: 20 * Rating: 3 */ int logicalShift(int x, int n) { } /* * bitCount - returns count of...
write a subroutine that will count the number of 1’s in binary byte
write a subroutine that will count the number of 1’s in binary byte
C++ Write a function that returns a string of 1's and 0's. This needs to be...
C++ Write a function that returns a string of 1's and 0's. This needs to be the binary representation of a number. Do not use loops. Use only recursion. Do not change the function's parameters or add more parameters. Do not change the main program. #include #include string bin(int number) { //Code here } int main() {    cout << bin(43) << endl; // Should be 101011    return 0; }
3.plese to write the correct code: The code below will loop as long as the number...
3.plese to write the correct code: The code below will loop as long as the number that you enter isn’t negative. It will add each non negative number to the current sum. It will print out the sum and average of all of the numbers you have entered. But someone jumbled up the code. Can you reorganize and indent it so it works properly? count = count + 1 message = "Enter an integer or a negative number to stop"...
C++ code please: Write a program to count the number of positive and the number of...
C++ code please: Write a program to count the number of positive and the number of negative inputs values. The program will stop when a 0 is input. Ex: If the input is -5.5 568 2.332 0 the output is 2 positive number(s) and 1 negative number(s). Ex: If the input is 153.0 0.534 2.2 5.6 46.584 0.015 5 0 the output is 7 positive number(s) and 0 negative number(s).
JAVA /** * numDistinctElements returns the number of distinct elements in a given array of doubles....
JAVA /** * numDistinctElements returns the number of distinct elements in a given array of doubles. * * Some examples: * numDistinctElements(new double[] { }) is 0 * numDistinctElements(new double[] { 1, -4, -7, 7, 8, 11 }) is 6 * numDistinctElements(new double[] { -7, -4, -7, 3, 8, 8 }) is 4 */ Code Below: public static int numDistinctElements (double[] list) { return 0; }
convert the while loop into for loop x = int(input('Enter initial value: ')) count = 0...
convert the while loop into for loop x = int(input('Enter initial value: ')) count = 0 if(x<1): print('Error') exit() print('Initial value is: ',x,' ',end='') while(x!=1): if(x%2==0): x=x/2 else: x=3*x+1 count+=1 if(x!=1): rint('Next value is: ',int(x),' ',end='') else: print('Final value ',int(x),', ',end='') print('number of operations performed ',int(count),' ',end='') break
Game to guess a number %% Guess Number Game while(1) count=0; guess=input('Guess a number between 0...
Game to guess a number %% Guess Number Game while(1) count=0; guess=input('Guess a number between 0 to 10 '); if(R>guess) disp('Your guess is too small') elseif (R<guess) disp('Your guess is too large') elseif (R==guess) disp('You are correct! It is ');guess R=floor( rand()*10 ); end count=count+1; if(count==3) break; end end You need to modify this code: (1)Player fails the game if you cannot make it within 3 tries. (2)Player can do this game up to 10 times. (3)If player input is...
You can't use any constant larger than 8 bits Please don't use codes from other websites...
You can't use any constant larger than 8 bits Please don't use codes from other websites C program Task: * rotateLeft - Rotate x to the left by n bits * Can assume that 0 <= n <= 31 * Examples: rotateLeft(0x87654321,4) = 0x76543218 * Legal ops: ~ & ^ | + << >> ! * Max ops: 25 * Rating: 3 */ int rotateLeft(int x, int n) { return 2; }