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

void SSL(bool* A, size_t size, size_t k) {
    k = k % size; 
    for (size_t i = 0; i < size - k; ++i) {
        A[i] = A[i + k];
    }
    for (size_t i = size - k; i < size; ++i) {
        A[i] = false;
    }
}

void SRL(bool* A, size_t size, size_t k) {
    k = k % size; 
    for (size_t i = size - 1; i >= k; --i) {
        A[i] = A[i - k];
    }
    for (size_t i = 0; i < k; ++i) {
        A[i] = false;
    }
}

void SLC(bool* A, size_t size, size_t k) {
    k = k % size; 
    bool* temp = (bool*)malloc(size * sizeof(bool));
    for (size_t i = 0; i < size; ++i) {
        temp[i] = A[i];
    }
    for (size_t i = 0; i < size; ++i) {
        A[i] = temp[(i + k) % size];
    }
    free(temp);
}

void AND(bool* A, bool* B, bool* result, size_t size) {
    for (size_t i = 0; i < size; ++i) {
        result[i] = A[i] && B[i];
    }
}

void OR(bool* A, bool* B, bool* result, size_t size) {
    for (size_t i = 0; i < size; ++i) {
        result[i] = A[i] || B[i];
    }
}

void XOR(bool* A, bool* B, bool* result, size_t size) {
    for (size_t i = 0; i < size; ++i) {
        result[i] = A[i] != B[i];
    }
}

void XNOR(bool* A, bool* B, bool* result, size_t size) {
    for (size_t i = 0; i < size; ++i) {
        result[i] = A[i] == B[i];
    }
}

void IMPL(bool* A, bool* B, bool* result, size_t size) {
    for (size_t i = 0; i < size; ++i) {
        result[i] = !A[i] || B[i];
    }
}

void flip(bool* bit_string, size_t size) {
    for (size_t i = 0; i < size; ++i) {
        bit_string[i] = !bit_string[i];
    }
}

void flip_position(bool* bit_string, size_t position) {
    bit_string[position] = !bit_string[position];
}

void print_array(bool* A, size_t size) {
    for (size_t i = 0; i < size; ++i) {
        printf("%d ", A[i]);
    }
    printf("\n");
}

int main() {
    size_t size = 8;
    bool A[] = {1, 0, 1, 0, 1, 0, 1, 0};
    bool B[] = {0, 1, 0, 1, 0, 1, 0, 1};
    bool result[size];

    printf("Original A: ");
    print_array(A, size);

    SSL(A, size, 2);
    printf("After SSL(A, 2): ");
    print_array(A, size);

    SRL(A, size, 2);
    printf("After SRL(A, 2): ");
    print_array(A, size);

    SLC(A, size, 2);
    printf("After SLC(A, 2): ");
    print_array(A, size);

    AND(A, B, result, size);
    printf("A AND B: ");
    print_array(result, size);

    OR(A, B, result, size);
    printf("A OR B: ");
    print_array(result, size);

    XOR(A, B, result, size);
    printf("A XOR B: ");
    print_array(result, size);

    XNOR(A, B, result, size);
    printf("A XNOR B: ");
    print_array(result, size);

    IMPL(A, B, result, size);
    printf("A IMPL B: ");
    print_array(result, size);

    flip(A, size);
    printf("After flip(A): ");
    print_array(A, size);

    flip_position(A, 3);
    printf("After flip(A, 3): ");
    print_array(A, size);


}

