Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Problem 2: Use the following information. Assumptions Data type int is 32 bits l

ID: 3746184 • Letter: P

Question

Problem 2: Use the following information. Assumptions Data type int is 32 bits long. *Right shifts of signed data are performed arithmetically Forbidden Casting, either explicit or implicit. * Relative comparison operators (,>, and) * Division, modulus, and multiplication. Conditionals(if or ?:), loops, switch statements, function calls, and macro invocations. Allowed operations All bit-level and logic operations Addition and Subtraction Equalityand inequality ( only for test cases Even with these ruies, you should try to make your code readable by choosing descriptive variable names and using comments to descibe the logic behind your solutions. Background: Everytime an x86-64 processor executes an arithmetic or logical operation, it sets a 1-bit condition code flag, named PF (for "parity flag"), to 1 when lower 8 bits in the resulting computation have an even number of ones and to 0 otherwise. Consider following table as example: 8 bits of data (count of 1-bits) PF 10100010 11010010 This computation of PF for lower 8 bits is performed by hardware as part of every arithmetic or logical operation In the following problem, you are required to replicate the behavior of parity flag computation for all bits in an int type data using C code agreeing with the rules listed above. Write code for the function with the following prototype (40 points) getParityFlag - return 1 when input contains an even number of 1-bits;e otherwise "Examples: getParityFlag(8xAAF)-1, getParityFlag(0x31)- Assume int is 32 bits int getParityFlag(unsigned int input);

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int getParity(unsigned int input){
    //variable to count ones
    int bits = 0;
    //Check 32 bits
    for(int i=0;i<32;i++){
        //Check if ith bit from left is 1
        if((input&(1<<i))!=0){
            bits++;
        }
    }
    return bits%2==0?1:0;
}

int main(int argc, char *argv[])
{
    printf("Parity is %d ",getParity(210));
    return 0;
}


OUTPUT :

Parity is 1

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote