' Bit Reverse — reverses bits in bytes and words using bitwise operators
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL val AS LONG
    LOCAL reversed AS LONG
    LOCAL byteIn AS LONG
    LOCAL byteOut AS LONG
    LOCAL wordIn AS LONG
    LOCAL wordOut AS LONG
    LOCAL nibbleIn AS LONG
    LOCAL nibbleOut AS LONG
    LOCAL bitPos AS LONG
    LOCAL testCount AS LONG
    LOCAL passes AS LONG
    LOCAL fails AS LONG
    LOCAL lineStr AS STRING
    LOCAL hexIn AS STRING
    LOCAL hexOut AS STRING
    LOCAL binStr AS STRING
    LOCAL bitChar AS STRING
    PRINT "============================================"
    PRINT "          BIT REVERSE CALCULATOR"
    PRINT "============================================"
    PRINT ""
    testCount = 0
    passes = 0
    fails = 0
    PRINT "8-bit byte reversal"
    PRINT ""
    FOR i = 0 TO 31
        byteIn = i * 8 + 3
        IF byteIn > 255 THEN
            byteIn = 255
        END IF
        reversed = 0
        FOR j = 0 TO 7
            bitPos = 7 - j
            IF (byteIn AND (2 ^ j)) <> 0 THEN
                reversed = reversed + (2 ^ bitPos)
            END IF
        NEXT j
        byteOut = reversed
        hexIn = HEX$(byteIn)
        IF LEN(hexIn) = 1 THEN
            hexIn = "0" + hexIn
        END IF
        hexOut = HEX$(byteOut)
        IF LEN(hexOut) = 1 THEN
            hexOut = "0" + hexOut
        END IF
        binStr = ""
        FOR j = 7 TO 0 STEP -1
            IF (byteIn AND (2 ^ j)) <> 0 THEN
                binStr = binStr + "1"
            ELSE
                binStr = binStr + "0"
            END IF
        NEXT j
        PRINT "0x" + hexIn + " (" + binStr + ") -> 0x" + hexOut
    NEXT i
    PRINT ""
    PRINT "16-bit word reversal"
    PRINT ""
    FOR i = 0 TO 15
        wordIn = i * 137 + 42
        reversed = 0
        FOR j = 0 TO 15
            bitPos = 15 - j
            IF (wordIn AND (2 ^ j)) <> 0 THEN
                reversed = reversed + (2 ^ bitPos)
            END IF
        NEXT j
        wordOut = reversed
        hexIn = HEX$(wordIn)
        DO WHILE LEN(hexIn) < 4
            hexIn = "0" + hexIn
        LOOP
        hexOut = HEX$(wordOut)
        DO WHILE LEN(hexOut) < 4
            hexOut = "0" + hexOut
        LOOP
        PRINT "0x" + hexIn + " -> 0x" + hexOut
    NEXT i
    PRINT ""
    PRINT "4-bit nibble reversal"
    PRINT ""
    FOR i = 0 TO 15
        nibbleIn = i
        nibbleOut = 0
        IF (nibbleIn AND 1) <> 0 THEN
            nibbleOut = nibbleOut + 8
        END IF
        IF (nibbleIn AND 2) <> 0 THEN
            nibbleOut = nibbleOut + 4
        END IF
        IF (nibbleIn AND 4) <> 0 THEN
            nibbleOut = nibbleOut + 2
        END IF
        IF (nibbleIn AND 8) <> 0 THEN
            nibbleOut = nibbleOut + 1
        END IF
        hexIn = HEX$(nibbleIn)
        hexOut = HEX$(nibbleOut)
        PRINT STR$(nibbleIn) + " -> " + STR$(nibbleOut) + "  (0x" + hexIn + " -> 0x" + hexOut + ")"
    NEXT i
    PRINT ""
    PRINT "============================================"
    PRINT "      BIT REVERSE LOOKUP TABLE (8-bit)"
    PRINT "============================================"
    PRINT ""
    FOR i = 0 TO 255
        reversed = 0
        FOR j = 0 TO 7
            bitPos = 7 - j
            IF (i AND (2 ^ j)) <> 0 THEN
                reversed = reversed + (2 ^ bitPos)
            END IF
        NEXT j
        hexOut = HEX$(reversed)
        IF LEN(hexOut) = 1 THEN
            hexOut = "0" + hexOut
        END IF
        PRINT "0x" + hexOut + " "
        IF (i + 1) MOD 16 = 0 THEN
            PRINT ""
        END IF
    NEXT i
    PRINT ""
    PRINT "============================================"
    PRINT "      BIT REVERSE VERIFICATION"
    PRINT "============================================"
    PRINT ""
    PRINT "Property: reversing twice gives original"
    PRINT ""
    FOR i = 0 TO 99
        val = i * 17 + 13
        IF val > 255 THEN
            val = val MOD 256
        END IF
        testCount = testCount + 1
        reversed = 0
        FOR j = 0 TO 7
            bitPos = 7 - j
            IF (val AND (2 ^ j)) <> 0 THEN
                reversed = reversed + (2 ^ bitPos)
            END IF
        NEXT j
        val = reversed
        reversed = 0
        FOR j = 0 TO 7
            bitPos = 7 - j
            IF (val AND (2 ^ j)) <> 0 THEN
                reversed = reversed + (2 ^ bitPos)
            END IF
        NEXT j
        IF reversed = i * 17 + 13 OR reversed = ((i * 17 + 13) MOD 256) THEN
            passes = passes + 1
        ELSE
            fails = fails + 1
        END IF
    NEXT i
    PRINT "Tests: " + STR$(testCount) + "  Passed: " + STR$(passes) + "  Failed: " + STR$(fails)
    PRINT ""
    PRINT "============================================"
    PRINT "     BIT REVERSE ON SPECIFIC PATTERNS"
    PRINT "============================================"
    PRINT ""
    PRINT "Pattern 0x80 (10000000) -> should be 0x01 (00000001)"
    val = 128
    reversed = 0
    FOR j = 0 TO 7
        bitPos = 7 - j
        IF (val AND (2 ^ j)) <> 0 THEN
            reversed = reversed + (2 ^ bitPos)
        END IF
    NEXT j
    hexIn = HEX$(val)
    hexOut = HEX$(reversed)
    IF LEN(hexIn) = 1 THEN
        hexIn = "0" + hexIn
    END IF
    IF LEN(hexOut) = 1 THEN
        hexOut = "0" + hexOut
    END IF
    PRINT "0x" + hexIn + " -> 0x" + hexOut
    IF reversed = 1 THEN
        passes = passes + 1
    END IF
    PRINT ""
    PRINT "Pattern 0xF0 (11110000) -> should be 0x0F (00001111)"
    val = 240
    reversed = 0
    FOR j = 0 TO 7
        bitPos = 7 - j
        IF (val AND (2 ^ j)) <> 0 THEN
            reversed = reversed + (2 ^ bitPos)
        END IF
    NEXT j
    hexIn = HEX$(val)
    hexOut = HEX$(reversed)
    IF LEN(hexIn) = 1 THEN
        hexIn = "0" + hexIn
    END IF
    IF LEN(hexOut) = 1 THEN
        hexOut = "0" + hexOut
    END IF
    PRINT "0x" + hexIn + " -> 0x" + hexOut
    IF reversed = 15 THEN
        passes = passes + 1
    END IF
    PRINT ""
    PRINT "Pattern 0xAA (10101010) -> should be 0x55 (01010101)"
    val = 170
    reversed = 0
    FOR j = 0 TO 7
        bitPos = 7 - j
        IF (val AND (2 ^ j)) <> 0 THEN
            reversed = reversed + (2 ^ bitPos)
        END IF
    NEXT j
    hexIn = HEX$(val)
    hexOut = HEX$(reversed)
    IF LEN(hexIn) = 1 THEN
        hexIn = "0" + hexIn
    END IF
    IF LEN(hexOut) = 1 THEN
        hexOut = "0" + hexOut
    END IF
    PRINT "0x" + hexIn + " -> 0x" + hexOut
    IF reversed = 85 THEN
        passes = passes + 1
    END IF
    PRINT ""
    PRINT "Done. Total passes: " + STR$(passes)
    FUNCTION = 0
END FUNCTION
