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 ;
}
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:-
Get Answers For Free
Most questions answered within 1 hours.