Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Bit masks
#1
Hi, So I am writing this small program to determine door access allowed to users.
#create a special no. for storing an RFID card that determines which door I am #allowed to use
door1 = 1
door2 = 2
door3 = 4
door4 = 8
allowed = 0
#add a door or two: for example door 1 and door 3
allowed = allowed|door3
allowed = allowed|door1
print("Access to door 1 is: ", allowed&door1)
print("Access to door 2 is: ", allowed&door2)
print("Access to door 3 is: ", allowed&door3)
print("Access to door 4 is: ", allowed&door4)
The output is:
Access to door 1 is: 1
Access to door 2 is: 0
Access to door 3 is: 4
Access to door 4 is: 0

How can I change from outputting a number to outputting "allowed" and "not allowed" without creating a function to do so.
Reply
#2
doors = [0,1,2,4,8]
allowed = 0
#add a door or two: for example door 1 and door 3
allowed = allowed | doors[3]
allowed = allowed | doors[1]
for door in range(1,5):
    print(f"Access to door {door} is{'' if allowed&doors[door] else ' not'} allowed")
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020