// Arup Guha
// 10/12/2026
// Solution to CIS 3362 Exam #2 Question #3

#include <stdio.h>

unsigned int xorSignature(unsigned int* message, int length);

int main() {

    // Set up a small test.
    unsigned int msg[4];
    msg[0] = 1 + 4 + 8 + (1<<16)*(2 + 4 + 16);
    msg[1] = 2 + 4 + 16 + (1<<16)*(1 + 32+ 128);
    msg[2] = 1 + 8 + 32 + (1<<16)*(1 + 8 + 128 + 256 + (1<<15));
    msg[3] = 1 + 64 + 1024;

    // Print out the result.
    unsigned int res = xorSignature(msg, 4);
    printf("%u\n", res);
    printf("%u\n", msg[2]);

    // Another test.
    unsigned int m[2];
    m[0] = 1 + 2 + 32 + (1<<14) + (1<<16) + (1<<18);
    m[1] = 8 + (1<<21) + (1<<30);
    unsigned int x = xorSignature(m , 2);
    printf("%u\n", x);
    return 0;
}

// Solution to the question.
unsigned int xorSignature(unsigned int* message, int length) {

    // Sets right 16 bits to 1.
    unsigned int right = (1<<16)-1;

    // Store result here.
    unsigned int res = 0;

    // Go through each item in the array.
    for (int i=0; i<length; i++) {

        // We just XOR the right 16 bits (first) with the left 16 bits (last)
        // Because it's unsigned, I am good just bitshifting it over to the left.
        res = res ^ (right&message[i]) ^ (message[i]>>16);
    }

    return res;
}
