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
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...
Design a recursive divide-and-conquer algorithm A(n) that takes an integer input n ≥ 0, and returns...
Design a recursive divide-and-conquer algorithm A(n) that takes an integer input n ≥ 0, and returns the total number of 1’s in n’s binary representation. Note that the input is n, not its binary representation. For example, A(9) should return 2 as 9’s binary representation is 1001, while A(7) should return 3 since 7 is 111 in binary. Note that your algorithm should have a running time of O(log n). Justify your answer. You need to do the following: (1)...
#include <stdio.h> /* Returns the number of occurrences of string t in s * * Example:...
#include <stdio.h> /* Returns the number of occurrences of string t in s * * Example: * substring_n("hello", "el") => 1 * substring_n("", "ab") => 0 * substring_n("utilities", "ti") => 2 * */ int substring_n(char *s, char *t) { return -1;
void insertion_sort(element a[], int n) // Put a[0]..a[n-1] into ascending order by insertion sort. { for...
void insertion_sort(element a[], int n) // Put a[0]..a[n-1] into ascending order by insertion sort. { for (int k = 1; k < n; k++) { // At this point, a[0]..a[k-1] are already in order. // Insert a[k] where it belongs among a[0]..a[k]. You need to write code for this insertion as the body of the for-k loop. } //endfor k } a) Write the code for the body of the for-k loop to complete the insertion sort routine. b) Count...
Evaluating if a value is negative using bitwise operators int test_dl3(int x) {     int i;...
Evaluating if a value is negative using bitwise operators int test_dl3(int x) {     int i;     for (i = 0; i < 32; i+=2)        if ((x & (1<          return 0;            return 1; } Legal ops: ! ~ & ^ | + << >> Max ops: 12 I have a few questions similar to this one, but I'm running into much the same problem for all of them. The behavior of this code appears to be that...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT