<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mueed</title>
    <description>The latest articles on DEV Community by Mueed (@mueedx).</description>
    <link>https://dev.to/mueedx</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1704205%2F7f14018b-afb7-46e7-adef-23701a38ddcd.jpg</url>
      <title>DEV Community: Mueed</title>
      <link>https://dev.to/mueedx</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mueedx"/>
    <language>en</language>
    <item>
      <title>Bitmasking for Backend RBAC</title>
      <dc:creator>Mueed</dc:creator>
      <pubDate>Wed, 25 Jun 2025 12:58:09 +0000</pubDate>
      <link>https://dev.to/mueedx/bitmasking-for-backend-rbac-3490</link>
      <guid>https://dev.to/mueedx/bitmasking-for-backend-rbac-3490</guid>
      <description>

&lt;h2&gt;
  
  
  What is Bitmasking?
&lt;/h2&gt;

&lt;p&gt;Bitmasking is a technique that uses &lt;strong&gt;binary numbers&lt;/strong&gt; (bits) to store and manage data efficiently. Each bit (0 or 1) in a number represents a specific state, such as whether a permission is granted or a setting is enabled. By using &lt;strong&gt;bitwise operations&lt;/strong&gt; (like AND, OR, and XOR), we can combine, check, or toggle these states quickly. Bitmasking is powerful because it packs multiple pieces of information into a single number, making it compact and fast.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why is Bitmasking Important?
&lt;/h2&gt;

&lt;p&gt;Bitmasking is highly efficient and offers several key benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Saves Space&lt;/strong&gt;: A single number can represent multiple settings or permissions, reducing storage needs compared to lists of strings.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Super Fast&lt;/strong&gt;: Bitwise operations are low-level, executed directly by the computer's processor, making them much faster than operations like searching through lists.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flexible&lt;/strong&gt;: Easily combine, check, or modify states with simple operations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Universal&lt;/strong&gt;: Works across nearly all programming languages and is widely used in various industries.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Uses of Bitmasking
&lt;/h2&gt;

&lt;p&gt;Bitmasking is applied in both software and non-technical fields:&lt;/p&gt;

&lt;h3&gt;
  
  
  In Software
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Permissions and Access Control&lt;/strong&gt;: Manages user permissions (e.g., read, write, delete) in systems like Role-Based Access Control (RBAC).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flag Management&lt;/strong&gt;: Tracks settings or states (e.g., on/off flags) in low-memory systems, such as embedded devices, using minimal memory.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Networking&lt;/strong&gt;: Extracts information from IP addresses or subnet masks using bitwise operations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Graphics and Gaming&lt;/strong&gt;: Used for image masks, collision detection, or tracking game states (e.g., active power-ups).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Encryption&lt;/strong&gt;: Employs bitwise operations like XOR in algorithms such as AES or XOR ciphers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Optimization&lt;/strong&gt;: Speeds up set manipulations or mathematical calculations (e.g., multiplying by powers of 2).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  In Non-Technical Fields
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Electronics&lt;/strong&gt;: Controls hardware settings, such as enabling specific pins on a microcontroller.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Robotics&lt;/strong&gt;: Manages sensor states (e.g., which sensors are active) in compact systems.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Telecommunications&lt;/strong&gt;: Encodes signals or manages channel states in communication systems.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data Compression&lt;/strong&gt;: Represents patterns or flags in compressed data formats.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Bitmasking’s ability to simplify complex state management makes it a versatile technique across these domains.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why String-Based Permissions Are Problematic
&lt;/h2&gt;

&lt;p&gt;In traditional Role-Based Access Control (RBAC), roles are linked to lists of permissions stored as strings (e.g., &lt;code&gt;view_email&lt;/code&gt;, &lt;code&gt;update_phone&lt;/code&gt;). For example, a "Reviewer" role might have &lt;code&gt;view_email&lt;/code&gt; and &lt;code&gt;view_phone&lt;/code&gt;. To check if a user can &lt;code&gt;update_phone&lt;/code&gt;, the system searches through the list, comparing each string. This approach has two major drawbacks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Slow Performance&lt;/strong&gt;: Searching a list of strings takes time, especially with many permissions or frequent requests.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Large Data Size&lt;/strong&gt;: Storing and sending string lists, particularly in JSON Web Tokens (JWTs), increases data size, slowing network requests and often requiring extra backend calls.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Understanding Bitwise Operations
&lt;/h2&gt;

&lt;p&gt;Bitmasking relies on &lt;strong&gt;bitwise operations&lt;/strong&gt;, which work directly on binary digits and are extremely fast. The key operations are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;AND (&lt;code&gt;&amp;amp;&lt;/code&gt;)&lt;/strong&gt;: Checks if specific bits are set (e.g., to verify a permission).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;OR (&lt;code&gt;|&lt;/code&gt;)&lt;/strong&gt;: Combines settings by setting bits to 1 (e.g., to assign multiple permissions).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;XOR (&lt;code&gt;^&lt;/code&gt;)&lt;/strong&gt;: Toggles bits (flips 0 to 1 or 1 to 0), useful for switching states.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;LEFT SHIFT (&lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt;)&lt;/strong&gt;: Moves bits left, multiplying by powers of 2 (e.g., &lt;code&gt;1 &amp;lt;&amp;lt; 2 = 4&lt;/code&gt;, or &lt;code&gt;0b0100&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RIGHT SHIFT (&lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt;)&lt;/strong&gt;: Moves bits right, dividing by powers of 2.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, to create a permission for the 3rd bit (position 2, starting from 0):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;1 &amp;lt;&amp;lt; 2 = 4&lt;/code&gt; (binary: &lt;code&gt;0b0100&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  How Bitmasking Works in RBAC
&lt;/h2&gt;

&lt;p&gt;Bitmasking simplifies permission management by assigning each permission a unique &lt;strong&gt;power of two&lt;/strong&gt; (ensuring only one bit is 1 in its binary form):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;READ = 1&lt;/code&gt; (&lt;code&gt;0b0001&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;CREATE = 2&lt;/code&gt; (&lt;code&gt;0b0010&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;UPDATE = 4&lt;/code&gt; (&lt;code&gt;0b0100&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;DELETE = 8&lt;/code&gt; (&lt;code&gt;0b1000&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It uses two main steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Combining Permissions (OR)&lt;/strong&gt;:
Combine permissions into one number using &lt;code&gt;|&lt;/code&gt;.
&lt;strong&gt;Example&lt;/strong&gt;: A user with &lt;code&gt;READ&lt;/code&gt; (1) and &lt;code&gt;UPDATE&lt;/code&gt; (4):

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;0b0001 | 0b0100 = 0b0101&lt;/code&gt; (decimal: 5)
The number 5 represents both permissions.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Checking Permissions (AND)&lt;/strong&gt;:
Check if a user has a permission using &lt;code&gt;&amp;amp;&lt;/code&gt;. If the result equals the permission value, they have it.
&lt;strong&gt;Example&lt;/strong&gt;: User has &lt;code&gt;0b0101&lt;/code&gt; (5, READ + UPDATE).

&lt;ul&gt;
&lt;li&gt;Check &lt;code&gt;READ&lt;/code&gt; (1): &lt;code&gt;0b0101 &amp;amp; 0b0001 = 0b0001&lt;/code&gt; (equals 1) → &lt;strong&gt;Access Granted&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Check &lt;code&gt;CREATE&lt;/code&gt; (2): &lt;code&gt;0b0101 &amp;amp; 0b0010 = 0b0000&lt;/code&gt; (not 2) → &lt;strong&gt;Access Denied&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These operations are fast because they work at the binary level.&lt;/p&gt;




&lt;h2&gt;
  
  
  A Simple Example: Audio Mixer Permissions
&lt;/h2&gt;

&lt;p&gt;Imagine building an audio mixer with four channels: Left, Right, Up, and Down. Each channel is active (1) or inactive (0), represented by a 4-bit number:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;LEFT = 1 &amp;lt;&amp;lt; 0 = 1&lt;/code&gt; (&lt;code&gt;0b0001&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;RIGHT = 1 &amp;lt;&amp;lt; 1 = 2&lt;/code&gt; (&lt;code&gt;0b0010&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;UP = 1 &amp;lt;&amp;lt; 2 = 4&lt;/code&gt; (&lt;code&gt;0b0100&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;DOWN = 1 &amp;lt;&amp;lt; 3 = 8&lt;/code&gt; (&lt;code&gt;0b1000&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Activate Channels&lt;/strong&gt;&lt;br&gt;
To activate Left and Up:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;permissions = LEFT | UP = 0b0001 | 0b0100 = 0b0101&lt;/code&gt; (decimal: 5)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Toggle Channels&lt;/strong&gt;&lt;br&gt;
To toggle Right and Up (flip their states):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;permissions = permissions ^ (RIGHT | UP) = 0b0101 ^ (0b0010 | 0b0100) = 0b0101 ^ 0b0110 = 0b0011&lt;/code&gt; (decimal: 3, now Left and Right are active).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This uses one number to manage all channel states.&lt;/p&gt;


&lt;h2&gt;
  
  
  Benefits of Bitmasking
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Fast&lt;/strong&gt;: Bitwise operations are instant, regardless of the number of permissions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compact&lt;/strong&gt;: A single number (e.g., 5) holds multiple permissions, saving space in databases and JWTs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Flexible&lt;/strong&gt;: Combine or toggle permissions without creating new roles.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Universal&lt;/strong&gt;: Works in languages like Python, JavaScript, Java, and more.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Clean Code&lt;/strong&gt;: Simplifies permission logic for easier maintenance.&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  Implementing Bitmasking in a Backend
&lt;/h2&gt;

&lt;p&gt;Here’s a simple way to use bitmasking for RBAC:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Define Permissions&lt;/strong&gt;: Assign power-of-two values.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;READ&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;      &lt;span class="c1"&gt;# 0b0001
&lt;/span&gt;&lt;span class="n"&gt;CREATE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;    &lt;span class="c1"&gt;# 0b0010
&lt;/span&gt;&lt;span class="n"&gt;UPDATE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;    &lt;span class="c1"&gt;# 0b0100
&lt;/span&gt;&lt;span class="n"&gt;DELETE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;    &lt;span class="c1"&gt;# 0b1000
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Store Permissions&lt;/strong&gt;: Save the combined permissions (e.g., 5 for READ + UPDATE) in the database and JWT.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Check Permissions&lt;/strong&gt;: Verify access in your backend.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nf"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_permissions&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;required_permission&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;required_permission&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# Allow access
&lt;/span&gt;&lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;# Deny access (403 Forbidden)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Middleware&lt;/strong&gt;: Add a check before each route to ensure required permissions.&lt;/p&gt;&lt;/li&gt;

&lt;/ol&gt;

&lt;h3&gt;
  
  
  Handling Many Permissions
&lt;/h3&gt;

&lt;p&gt;A 64-bit number can hold up to 64 permissions. For more, use an array of numbers (virtual bitfield). For example, permission 70 would be in the second number (index 1, bit 6). This keeps bitmasking fast and scalable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Limitations of Bitmasking
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Learning Curve&lt;/strong&gt;: Bitwise operations can be confusing for beginners.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Limited Size&lt;/strong&gt;: A single number supports 32 or 64 permissions, though arrays can help.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Not Universal&lt;/strong&gt;: Best for permissions or flags, not all problems.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Try It Yourself: Hands-On Bitmasking Demo
&lt;/h2&gt;

&lt;p&gt;For a practical, hands-on experience with bitmasking in RBAC, check out the FastAPI Bitmasking RBAC Demo on GitHub:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/mueedx/demo-bitmasking" rel="noopener noreferrer"&gt;FastAPI Bitmasking RBAC Demo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This public repository provides a working FastAPI application with clear comments and a step-by-step guide to set up and test bitmasking-based permission control. Follow the instructions to explore how bitwise operations manage permissions efficiently in a real backend system.&lt;/p&gt;




</description>
      <category>rbac</category>
      <category>bitmasking</category>
      <category>backend</category>
      <category>security</category>
    </item>
  </channel>
</rss>
