#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL data_val AS LONG
    LOCAL crc16_val AS LONG
    LOCAL crc32_val AS LONG
    LOCAL poly16 AS LONG
    LOCAL poly32 AS LONG
    LOCAL i AS LONG
    LOCAL bit_pos AS LONG
    LOCAL temp_val AS LONG
    LOCAL temp_str AS STRING

    poly16 = 40961
    poly32 = 79764919

    PRINT "CRC-16 and CRC-32 Calculation"
    PRINT "============================="
    PRINT ""

    ' CRC-16 for data 0x41 ('A')
    data_val = 65
    crc16_val = data_val
    crc16_val = crc16_val * 256
    i = 0
    WHILE i < 8
        bit_pos = crc16_val AND 32768
        crc16_val = crc16_val * 2
        IF bit_pos <> 0 THEN
            crc16_val = crc16_val XOR poly16
        END IF
        i = i + 1
    WEND

    temp_str = STR$(crc16_val)
    PRINT "CRC-16 for 'A' (ASCII 65): " + temp_str

    ' CRC-32 for data 0x41 ('A')
    crc32_val = data_val
    i = 0
    WHILE i < 8
        bit_pos = crc32_val AND 1
        crc32_val = crc32_val / 2
        IF bit_pos <> 0 THEN
            crc32_val = crc32_val XOR poly32
        END IF
        i = i + 1
    WEND

    temp_str = STR$(crc32_val)
    PRINT "CRC-32 for 'A' (ASCII 65): " + temp_str

    ' XOR vs AND vs OR demo
    PRINT ""
    PRINT "Bitwise operation demo:"
    temp_str = STR$(12 AND 7)
    PRINT "  12 AND 7 = " + temp_str
    temp_str = STR$(12 OR 7)
    PRINT "  12 OR 7 = " + temp_str
    temp_str = STR$(12 XOR 7)
    PRINT "  12 XOR 7 = " + temp_str

    ' CRC of larger data
    data_val = 16961
    crc16_val = data_val
    crc16_val = crc16_val * 256
    i = 0
    WHILE i < 16
        bit_pos = crc16_val AND 32768
        crc16_val = crc16_val * 2
        IF bit_pos <> 0 THEN
            crc16_val = crc16_val XOR poly16
        END IF
        i = i + 1
    WEND
    temp_str = STR$(crc16_val)
    PRINT "CRC-16 for 0x4241 (BA): " + temp_str

    ' CRC-32 for same data
    data_val = 16961
    crc32_val = data_val
    i = 0
    WHILE i < 16
        bit_pos = crc32_val AND 1
        crc32_val = crc32_val / 2
        IF bit_pos <> 0 THEN
            crc32_val = crc32_val XOR poly32
        END IF
        i = i + 1
    WEND
    temp_str = STR$(crc32_val)
    PRINT "CRC-32 for 0x4241 (BA): " + temp_str

    ' Calculate multiple CRCs
    PRINT ""
    PRINT "CRC comparison table:"
    PRINT "  Char  ASCII  CRC-16  CRC-32"
    i = 65
    WHILE i < 70
        data_val = i
        crc16_val = data_val * 256
        bit_pos = 0
        temp_val = 0
        WHILE temp_val < 8
            bit_pos = crc16_val AND 32768
            crc16_val = crc16_val * 2
            IF bit_pos <> 0 THEN
                crc16_val = crc16_val XOR poly16
            END IF
            temp_val = temp_val + 1
        WEND

        crc32_val = data_val
        temp_val = 0
        WHILE temp_val < 8
            bit_pos = crc32_val AND 1
            crc32_val = crc32_val / 2
            IF bit_pos <> 0 THEN
                crc32_val = crc32_val XOR poly32
            END IF
            temp_val = temp_val + 1
        WEND

        PRINT "  " + CHR$(i) + "     " + STR$(i) + "    " + STR$(crc16_val) + "  " + STR$(crc32_val)
        i = i + 1
    WEND

    PRINT ""
    PRINT "CRC demo complete."

    FUNCTION = 0
END FUNCTION
