#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL data_bits(7) AS LONG
    LOCAL parity_bits(3) AS LONG
    LOCAL encoded(7) AS LONG
    LOCAL decoded(7) AS LONG
    LOCAL received(7) AS LONG
    LOCAL i AS LONG
    LOCAL ones_count AS LONG
    LOCAL parity_even AS LONG
    LOCAL parity_odd AS LONG
    LOCAL error_pos AS LONG
    LOCAL syndrome AS LONG
    LOCAL bit_val AS LONG
    LOCAL temp_str AS STRING

    PRINT "Parity Check and Hamming(7,4) Code"
    PRINT "=================================="
    PRINT ""

    ' Even parity calculation
    data_bits(0) = 1
    data_bits(1) = 0
    data_bits(2) = 1
    data_bits(3) = 1

    ones_count = 0
    i = 0
    WHILE i < 4
        IF data_bits(i) = 1 THEN
            ones_count = ones_count + 1
        END IF
        i = i + 1
    WEND

    IF ones_count MOD 2 = 0 THEN
        parity_even = 0
    ELSE
        parity_even = 1
    END IF
    parity_odd = 1 - parity_even

    temp_str = STR$(ones_count)
    PRINT "Data: 1011"
    PRINT "  Ones count: " + temp_str
    PRINT "  Even parity bit: " + STR$(parity_even)
    PRINT "  Odd parity bit: " + STR$(parity_odd)

    ' Hamming(7,4) encoding
    ' Data bits: d1 d2 d3 d4 at positions 3, 5, 6, 7
    ' Parity bits: p1 (pos 1), p2 (pos 2), p3 (pos 4)
    ' p1 = d1 XOR d2 XOR d4
    ' p2 = d1 XOR d3 XOR d4
    ' p3 = d2 XOR d3 XOR d4

    data_bits(0) = 1
    data_bits(1) = 0
    data_bits(2) = 1
    data_bits(3) = 1

    parity_bits(0) = data_bits(0) XOR data_bits(1) XOR data_bits(3)
    parity_bits(1) = data_bits(0) XOR data_bits(2) XOR data_bits(3)
    parity_bits(2) = data_bits(1) XOR data_bits(2) XOR data_bits(3)

    encoded(0) = parity_bits(0)
    encoded(1) = parity_bits(1)
    encoded(2) = data_bits(0)
    encoded(3) = parity_bits(2)
    encoded(4) = data_bits(1)
    encoded(5) = data_bits(2)
    encoded(6) = data_bits(3)

    PRINT ""
    PRINT "Hamming(7,4) encoding of [1 0 1 1]:"
    temp_str = ""
    i = 0
    WHILE i < 7
        temp_str = temp_str + STR$(encoded(i))
        i = i + 1
    WEND
    PRINT "  Codeword: " + temp_str

    ' Introduce 1-bit error at position 3
    i = 0
    WHILE i < 7
        received(i) = encoded(i)
        i = i + 1
    WEND
    error_pos = 3
    IF received(error_pos) = 0 THEN
        received(error_pos) = 1
    ELSE
        received(error_pos) = 0
    END IF

    temp_str = ""
    i = 0
    WHILE i < 7
        temp_str = temp_str + STR$(received(i))
        i = i + 1
    WEND
    PRINT "  Received (error at pos " + STR$(error_pos) + "): " + temp_str

    ' Syndrome calculation
    syndrome = 0
    i = 0
    WHILE i < 7
        IF received(i) = 1 THEN
            syndrome = syndrome XOR (i + 1)
        END IF
        i = i + 1
    WEND

    temp_str = STR$(syndrome)
    PRINT "  Syndrome: " + temp_str
    IF syndrome = 0 THEN
        PRINT "  No error detected"
    ELSE
        ' Flip the erroneous bit
        IF received(syndrome - 1) = 0 THEN
            received(syndrome - 1) = 1
        ELSE
            received(syndrome - 1) = 0
        END IF
        PRINT "  Error corrected at position " + STR$(syndrome - 1)
    END IF

    ' Extract data bits from corrected codeword
    decoded(0) = received(2)
    decoded(1) = received(4)
    decoded(2) = received(5)
    decoded(3) = received(6)

    temp_str = ""
    i = 0
    WHILE i < 4
        temp_str = temp_str + STR$(decoded(i))
        i = i + 1
    WEND
    PRINT "  Decoded data: " + temp_str

    ' More parity examples
    PRINT ""
    PRINT "Parity check examples:"
    PRINT "  1001 (2 ones) -> even=0, odd=1"
    PRINT "  1111 (4 ones) -> even=0, odd=1"
    PRINT "  1010 (2 ones) -> even=0, odd=1"

    PRINT ""
    PRINT "Parity check demo complete."

    FUNCTION = 0
END FUNCTION
