Skip to content

Instantly share code, notes, and snippets.

@saiday
Created January 22, 2024 10:00
Show Gist options
  • Save saiday/e27ace94164c0039edc6f993edf5f41e to your computer and use it in GitHub Desktop.
Save saiday/e27ace94164c0039edc6f993edf5f41e to your computer and use it in GitHub Desktop.
bit masing for user visibility
# Constants representing each status as bit flags
ENABLE = 1 << 0 # 0b000001
BLOCKED_BY_GLOBAL = 1 << 1 # 0b000010
BLOCKED_BY_CN = 1 << 2 # 0b000100
COPYRIGHT_GLOBAL = 1 << 3 # 0b001000
COPYRIGHT_CN = 1 << 4 # 0b010000
PENDING_DELETION = 1 << 5 # 0b100000
def set_status(status_flags, condition):
"""Set a specific condition."""
return status_flags | condition
def unset_status(status_flags, condition):
"""Unset a specific condition."""
return status_flags & ~condition
def is_visible(status_flags):
"""Check if the item is visible based on the conditions."""
# Check if ENABLE is 1 and all other flags (1 to 5) are 0
return status_flags == ENABLE
# Usage:
status_flags = 0 # Initially, no flags are set
# Setting some statuses as an example
status_flags = set_status(status_flags, BLOCKED_BY_GLOBAL)
status_flags = set_status(status_flags, COPYRIGHT_CN)
# Unsetting a status
status_flags = unset_status(status_flags, BLOCKED_BY_GLOBAL)
# Setting the 'ENABLE' status
status_flags = set_status(status_flags, ENABLE)
# Check if the item is visible
print("Is visible:", is_visible(status_flags)) # This will print: Is visible: False
# To make it visible, ensure that only 'ENABLE' flag is set
status_flags = ENABLE # Resetting to only ENABLE flag
print("Is visible:", is_visible(status_flags)) # This will print: Is visible: True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment