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

Create a BYTE array with the label ‘input’. This array may be of any length betw

ID: 3593267 • Letter: C

Question

Create a BYTE array with the label ‘input’. This array may be of any length between 2 and 100.

Create a BYTE array with the label ‘output’. This array should be the same length as ‘input’.

Create a DWORD variable with the label ‘shift’. ‘shift’ should hold a single value. The value of ‘shift’ must be less than the length of ‘input’.

The program should then read each of the values from the array ‘input’ and place the values into the ‘output’ array but the location should be shifted by the amount in the ‘shift’ variable. If the shift would cause a value to be outside of the bounds of ‘output’, then the values should “wrap around” to the front of ‘output’.

For example:

My ‘input’ array is 5,0A,3,6,0C

‘shift’ is 3

The proper solution for ‘output’ is 3,6,0C,5,0A

Explanation / Answer

unsigned int reverseBits(unsigned int num)

{

    unsigned int NO_OF_BITS = sizeof(num) * 8;

    unsigned int reverse_num = 0, i, temp;

    for (i = 0; i < NO_OF_BITS; i++)

    {

        temp = (num & (1 << i));

        if(temp)

            reverse_num |= (1 << ((NO_OF_BITS - 1) - i));

    }

  

    return reverse_num;

}

/* Driver function to test above function */

int main()

{

    unsigned int x = 2;

    printf("%u", reverseBits(x));

    getchar();

}

unsigned int reverseBits(unsigned int num)

{

    unsigned int NO_OF_BITS = sizeof(num) * 8;

    unsigned int reverse_num = 0;

    int i;

    for (i = 0; i < NO_OF_BITS; i++)

    {

        if((num & (1 << i)))

           reverse_num |= 1 << ((NO_OF_BITS - 1) - i);

   }

    return reverse_num;

}