Question

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 = ;

  return ;

}

Homework Answers

Answer #1

Program In C:-

# include <stdio.h>

/* This function returns 1 if the number of 1's in a byte is odd, else  it return 0 */
int bitParity8bit(int input)
{
  /* We use an 8 bit integer, for which we can first calculate left shift
  and then XOR */
  int mask = input ^ input >> 16;
  int quarterParity = mask ^ mask >> 8;
  int mask2 = quarterParity ^ quarterParity >> 4;
  int halfParity = mask2 ^ mask2 >> 2;
  int mask3 = halfParity ^ halfParity >> 1;

  return mask3 & 1 ;

}

int main()
{
  int n = 7;
        printf("Parity of number %d = %d\n",n,bitParity8bit(n));

  n = 257;
  printf("Parity of number %d = %d\n",n,bitParity8bit(n));
        getchar();
        return 0;
}

Output:-

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
/* * 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 ; }
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...
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; }
// Write a function that returns 0 if x is 0, returns -1 // if x...
// Write a function that returns 0 if x is 0, returns -1 // if x < 0, returns 1 if x > 0 // Your code must follow the Bit-Level Integer Coding Rules // on the textbook (located between hw 2.60 and 2.61). // You can assume w = 32. // The only allowed operations in your code are: // ! ~ & ^ | + << >> // This requirement is more restrictive than the coding rules. //...
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; }
C - Language Write an expression that executes the loop while the user enters a number...
C - Language Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may test code with different test values. This activity will perform three tests, with user input of 9, 5, 2, -1, then with user input of 0, -17, then with user input of -1. Also note: If the submitted code has an infinite loop, the system will stop running the code after a few seconds,...
from typing import List def longest_chain(submatrix: List[int]) -> int: """ Given a list of integers, return...
from typing import List def longest_chain(submatrix: List[int]) -> int: """ Given a list of integers, return the length of the longest chain of 1's that start from the beginning. You MUST use a while loop for this! We will check. >>> longest_chain([1, 1, 0]) 2 >>> longest_chain([0, 1, 1]) 0 >>> longest_chain([1, 0, 1]) 1 """ i = 0 a = [] while i < len(submatrix) and submatrix[i] != 0: a.append(submatrix[i]) i += 1 return sum(a) def largest_rectangle_at_position(matrix: List[List[int]], x:...
Q1: Given the following code, what is returned by tq(4)? int tq(int num){ if (num ==...
Q1: Given the following code, what is returned by tq(4)? int tq(int num){ if (num == 0) return 0; else if (num > 100) return -1; else     return num + tq( num – 1 ); } Group of answer choices: 0 4 -1 10 Q2: Given that values is of type LLNode<Integer> and references a linked list (non-empty) of Integer objects, what does the following code do if invoked as mystery(values)? int mystery(LLNode<Integer> list) {    if (list.getLink() ==...
i need this code to print a number of stars depending on how much the user...
i need this code to print a number of stars depending on how much the user wants and after * it prints no stars. if the user enters a negative number it should print "error invalid number" this is my code so far: def stars(n,i): stars(n, 1) if n <= 0: return "No stars" if i <= n: print("* ", end="") stars(n, i + 1) else: print("no stars") stars(n - 1, 1) n = int(input("enter an integer")) def main(): stars()...
#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;