Bit masks - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Bit masks (/thread-20432.html) |
Bit masks - leodavinci1990 - Aug-10-2019 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. RE: Bit masks - ThomasL - Aug-11-2019 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") |