Skip to main content
imvinojanv.dev
  • About
  • Blog
  • Projects
  • Snippets

Command Palette

Search for a command to run...

imvinojanv.dev
Open to Hire

I'm always open to discussing software engineering work or partnership.

HomeAboutResumeUses
BlogProjectsSnippets
© 2026 Vinojan Veerapathirathasan —— Colombo, Sri Lanka.
MediumGitHubLinkedInRSS
Back

Mastering Bitmasking: The Smart Approach to Tracking Product Defects

Learn how bitmasking efficiently tracks and manages multiple product defects with minimal storage and fast operations.

Published on June 13, 2024
5 min read
Mastering Bitmasking: The Smart Approach to Tracking Product Defects

Hi there 👋,

Welcome to an another insightful exploration of a powerful technique that revolutionizes the way we track and manage product defects in manufacturing — Bitmasking. 📑In this article, we’ll dive deep into the nuts and bolts of bitmasking, showing you how this simple yet potent method can transform your defect management system, saving time and reducing errors. Ready to unlock the full potential of your quality control strategy? Let’s get started! 🎊

Bitmasking is an efficient technique commonly used in programming and data handling to manage multiple binary states or conditions compactly within a single integer. This approach leverages the binary number system, where each bit in a number can represent two states: on (1) or off (0). By using bitmasking, programmers can track multiple conditions, such as settings or defects, in a very space-efficient manner, while also ensuring high-speed operations due to the nature of bitwise arithmetic.

#1: Introduction to Bitmasking

The concept of bitmasking revolves around using individual bits of a binary number to represent boolean (true/false) conditions. In a bitmask, each bit position represents a specific condition or feature, with ‘1’ indicating the presence or ‘true’ state of a condition, and ‘0’ representing its absence or ‘false’ state.

#2: Benefits of Bitmasking

  1. Efficiency: Uses minimal memory, storing multiple boolean conditions in a single integer.
  2. Speed: Bitwise operations are extremely fast and are supported directly by the processor.
  3. Simplicity: Allows for the easy toggling, setting, and clearing of conditions with simple bitwise operations.

#3: Limitations of Bitmasking

  1. Scalability: Adding new defects beyond the current bitmask size requires significant adjustments.
  2. Readability: Bitwise operations can be less intuitive and harder to debug compared to more straightforward methods.
Figure 1: Bitmasking

#4: Example: Tracking Product Defects

Consider a manufacturing scenario where each product needs to be checked for various potential defects. Using bitmasking, each defect is assigned to a specific bit in an integer. This method is especially useful in quality control processes, where checking and recording multiple conditions efficiently is crucial.

#5: Database Schema

Here’s how you might define a table in SQL to store product information along with defect data using bitmasking:

SQL
CREATE TABLE Products (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(255),
    defects INT -- This column will store our bitmask representing defects
);

#6: Defects and Their Bit Positions

Assume we are tracking the following defects:

  1. Broken Stitch — Bit 0
  2. Skip Stitch — Bit 1
  3. Open Stitch — Bit 2
  4. Over Stitch — Bit 3
  5. Uneven Stitch — Bit 4
  6. Run of Stitch — Bit 5
  7. Incomplete Stitch — Bit 6
  8. Tension Loose — Bit 7

#7: Storing Defects

Using TypeScript, you could create a function to calculate the bitmask for a set of defects detected during inspection:

// Defect types
type Defect = 'Broken Stitch' | 'Skip Stitch' | 'Open Stitch' | 'Over Stitch' |
              'Uneven Stitch' | 'Run of Stitch' | 'Incomplete Stitch' | 'Tension Loose';
function calculateDefectsMask(defects: Defect[]): number {
    let mask = 0;
 
    const defectPositions: {
        [key in Defect]: number
    } = {
        'Broken Stitch': 0,
        'Skip Stitch': 1,
        'Open Stitch': 2,
        'Over Stitch': 3,
        'Uneven Stitch': 4,
        'Run of Stitch': 5,
        'Incomplete Stitch': 6,
        'Tension Loose': 7
    };
 
    defects.forEach(defect => {
        mask |= (1 << defectPositions[defect]);
    });
 
    return mask;
}
 
// Example usage
const defectsDetected: Defect[] = ['Broken Stitch', 'Over Stitch', 'Run of Stitch'];
const defectMask = calculateDefectsMask(defectsDetected);
// Store the defectMask to Products table in the database

Retrieving and Decoding Defects

When fetching the defect data from the database, you need a function to decode the bitmask back into human-readable defect names:

function decodeDefectsMask(mask: number): Defect[] {
    const defects: Defect[] = [];
    const positions: Defect[] = ['Broken Stitch', 'Skip Stitch', 'Open Stitch', 'Over Stitch',
        'Uneven Stitch', 'Run of Stitch', 'Incomplete Stitch', 'Tension Loose'
    ];
 
    positions.forEach((defect, index) => {
        if (mask & (1 << index)) {
            defects.push(defect);
        }
    });
 
    return defects;
}
 
// Assuming defectMask is fetched from the database
const defects = decodeDefectsMask(defectMask);
console.log(defects); // Outputs: ['Broken Stitch', 'Over Stitch', 'Run of Stitch']

SQL Query to Retrieve Defect Data

To retrieve the defect bitmask for a specific product, you could use the following SQL query:

SELECT defects FROM Products WHERE id = <PRODUCT_ID>;

After fetching the bitmask, use the decodeDefectsMask function to determine which defects are present.

Bitmasking is particularly advantageous in scenarios where the dataset is extensive, and performance is crucial. This method is widely applicable in manufacturing, where tracking multiple potential issues efficiently can lead to significant improvements in quality assurance processes. This makes bitmasking an excellent choice for industries where real-time data processing and minimal data storage are critical.


I appreciate you taking the time to read this article.🙌

Before you move on to explore the next article, don’t forget to give your claps 👏 for this article and share with your friends. Stay connected with me on social media. Thanks for your support and have a great rest of your day! 🎊

✍️ Vinojan Veerapathirathasan.

0
0
0
0
0

On This Page

#1: Introduction to Bitmasking#2: Benefits of Bitmasking#3: Limitations of Bitmasking#4: Example: Tracking Product Defects#5: Database Schema#6: Defects and Their Bit Positions#7: Storing DefectsRetrieving and Decoding DefectsSQL Query to Retrieve Defect Data
Last updated: June 13, 2024