' Bitwise Flag Manipulation — permission flags, 32-bit combinations
#COMPILE EXE
#DIM ALL

%FLAG_READ = 1
%FLAG_WRITE = 2
%FLAG_EXECUTE = 4
%FLAG_DELETE = 8
%FLAG_SHARE = 16
%FLAG_ADMIN = 32

SUB ShowFlags(flags AS DWORD)
    PRINT "Flags: " & STR$(flags) & " ("
    IF flags AND %FLAG_READ THEN PRINT "READ "
    IF flags AND %FLAG_WRITE THEN PRINT "WRITE "
    IF flags AND %FLAG_EXECUTE THEN PRINT "EXECUTE "
    IF flags AND %FLAG_DELETE THEN PRINT "DELETE "
    IF flags AND %FLAG_SHARE THEN PRINT "SHARE "
    IF flags AND %FLAG_ADMIN THEN PRINT "ADMIN "
    PRINT ")"
END SUB

SUB FlagCombinations()
    LOCAL i AS LONG
    LOCAL flags AS DWORD
    PRINT "=== FLAG COMBINATIONS ==="
    FOR i = 0 TO 15
        flags = i
        PRINT "Value " & STR$(i) & ": "
        IF flags AND %FLAG_READ THEN PRINT "R";
        IF flags AND %FLAG_WRITE THEN PRINT "W";
        IF flags AND %FLAG_EXECUTE THEN PRINT "X";
        IF flags AND %FLAG_DELETE THEN PRINT "D";
        PRINT
    NEXT i
    PRINT
END SUB

SUB PermissionsDemo()
    LOCAL userFlags AS DWORD
    LOCAL adminFlags AS DWORD
    userFlags = %FLAG_READ OR %FLAG_WRITE
    adminFlags = %FLAG_READ OR %FLAG_WRITE OR %FLAG_EXECUTE OR %FLAG_DELETE OR %FLAG_ADMIN
    PRINT "=== PERMISSIONS DEMO ==="
    PRINT "User permissions:"
    CALL ShowFlags(userFlags)
    PRINT "Admin permissions:"
    CALL ShowFlags(adminFlags)
    PRINT
    PRINT "Can user execute? "
    IF userFlags AND %FLAG_EXECUTE THEN PRINT "Yes" ELSE PRINT "No"
    PRINT "Can admin delete? "
    IF adminFlags AND %FLAG_DELETE THEN PRINT "Yes" ELSE PRINT "No"
    PRINT
    PRINT "Granting execute to user..."
    userFlags = userFlags OR %FLAG_EXECUTE
    CALL ShowFlags(userFlags)
    PRINT "Revoking write from admin..."
    adminFlags = adminFlags AND (NOT %FLAG_WRITE)
    CALL ShowFlags(adminFlags)
    PRINT
END SUB

SUB ToggleDemo()
    LOCAL flags AS DWORD
    LOCAL i AS LONG
    PRINT "=== TOGGLE DEMO ==="
    flags = 0
    FOR i = 0 TO 7
        flags = flags XOR (1 SHL i)
        PRINT "After toggle bit " & STR$(i) & ": " & STR$(flags)
    NEXT i
    PRINT
    PRINT "Toggling all bits back off:"
    FOR i = 0 TO 7
        flags = flags XOR (1 SHL i)
        PRINT "After toggle bit " & STR$(i) & ": " & STR$(flags)
    NEXT i
    PRINT
END SUB

SUB BitMaskExtract()
    LOCAL value AS DWORD
    LOCAL lowNibble AS DWORD
    LOCAL highNibble AS DWORD
    LOCAL byte0 AS DWORD
    LOCAL byte1 AS DWORD
    value = &HA5B3
    PRINT "=== BIT MASK EXTRACTION ==="
    PRINT "Value: &H" & FORMAT$(value, "&H")
    lowNibble = value AND &HF
    highNibble = (value SHR 4) AND &HF
    byte0 = value AND &HFF
    byte1 = (value SHR 8) AND &HFF
    PRINT "Low nibble:  &H" & FORMAT$(lowNibble, "&H") & " = " & STR$(lowNibble)
    PRINT "High nibble: &H" & FORMAT$(highNibble, "&H") & " = " & STR$(highNibble)
    PRINT "Byte 0:      &H" & FORMAT$(byte0, "&H") & " = " & STR$(byte0)
    PRINT "Byte 1:      &H" & FORMAT$(byte1, "&H") & " = " & STR$(byte1)
    PRINT
END SUB

SUB BitCountDemo()
    LOCAL v AS DWORD
    LOCAL count AS LONG
    LOCAL i AS LONG
    PRINT "=== POPULATION COUNT (32-bit) ==="
    FOR v = 0 TO 31
        count = 0
        FOR i = 0 TO 31
            IF (v SHR i) AND 1 THEN count = count + 1
        NEXT i
        PRINT "popcount(" & STR$(v) & ") = " & STR$(count)
    NEXT v
    PRINT
END SUB

FUNCTION PBMAIN() AS LONG
    PRINT "==========================="
    PRINT " Bitwise Flags & Permissions"
    PRINT "==========================="
    PRINT
    CALL FlagCombinations()
    CALL PermissionsDemo()
    CALL ToggleDemo()
    CALL BitMaskExtract()
    CALL BitCountDemo()
    PRINT "Flag manipulation complete."
    FUNCTION = 0
END FUNCTION
