// Arup Guha
// 9/23/2022
// CIS 3362 Example of using bitwise operators.

#include <stdio.h>

int bytexor(int* vals, int len);

int main(void) {

    // Basic test.
    int x = 147, y=89;
    print("%d %d %d\n", x&y, x|y, x^y);

    // Test my byte XOR function.
    int items[2];
    items[0] = (17<<24) + (132<<16) + (47<<8) + 136;
    items[1] = (47<<24) + (136<<16) + (132<<8) + 17;
    printf("%d\n", bytexor(items, 2));
    return 0;
}

int bytexor(int* vals, int len) {

    int res = 0;

    // Go through each item in the array.
    for (int i=0; i<len; i++) {

        int buffer = vals[i];

        // j is the byte from least to most significant.
        for (int j=0; j<4; j++) {

            // XORs in the last byte of buffer.
            res = res ^ (buffer & ( (1<<8) - 1 ) ); // 0xff

            // Just so you can see what was XORED
            int tmp = buffer & ( (1<<8) - 1 );
            printf("xored with %d to get %d\n", tmp, res);

            // Cuts off the last byte, sliding everything over to the right.
            buffer = buffer >> 8;
        }
    }
}
