Welcome to Subscribe On Youtube

3204. Bitwise User Permissions Analysis 🔒

Description

Table: user_permissions

+-++
\| user_id     \| int     \|
\| permissions \| int     \|
+-++-+
\| user_id \| permissions \|
++-+
 

Output:

+-+--+
\| common_perms \| any_perms   \|
+--+-+
\| 0            \| 15          \|
+--+-+
    

Explanation:

  • common_perms: Represents the bitwise AND result of all permissions:
    • For user 1 (5): 5 (binary 0101)
    • For user 2 (12): 12 (binary 1100)
    • For user 3 (7): 7 (binary 0111)
    • For user 4 (3): 3 (binary 0011)
    • Bitwise AND: 5 & 12 & 7 & 3 = 0 (binary 0000)
  • any_perms: Represents the bitwise OR result of all permissions:
    • Bitwise OR: 5 \| 12 \| 7 \| 3 = 15 (binary 1111)

</div>

Solutions

Solution 1: Bitwise Operations

We can use the BIT_AND and BIT_OR functions to calculate common_perms and any_perms.

  • # Write your MySQL query statement below
    SELECT
        BIT_AND(permissions) AS common_perms,
        BIT_OR(permissions) AS any_perms
    FROM user_permissions;
    
    

All Problems

All Solutions