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 ; }
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; }
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; }
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;
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...
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"...
***Python Hailstones, also known as the Collatz sequence, are a mathematical curiosity. For any number in...
***Python Hailstones, also known as the Collatz sequence, are a mathematical curiosity. For any number in the sequence, the next number in the sequence is determined by two simple rules: If the current number n is odd, the next number in the sequence is equal to 3 * n + 1 If the current number n is even instead, the next number in the sequence is equal to one half of n (i.e., n divided by 2) We repeat this...