Dark Mode On/Off

Posted in Programming
LAST UPDATED: APRIL 11, 2023

Firstly, let us try to understand what a mask represents in terms of programming. What do masks mean in general term? Suppose there is a face mask. When a person wears a mask, his/her/their representation changes. They look different from the way they did before or we can say that their actual face is hidden and the usage of a mask introduces new changes. After wearing the mask, a person can choose to show whatever necessary details they want to show us like eyes and/or nose.

In programming, we use masking in a similar way and call it bit masking because masking is performed on bits. This is because data is stored in the form of bits in the computer’s memory. We can say that we use a mask of bits in masking. When we use this mask with our original value, we can show only the required data and rest of it is hidden. We know that some bits are set(1) and some are unset(0). We use bit masking to operate on such bits. We can also say that we use bit masking to store many layers of values in the same set of bits like our face has multiple layers when we use a mask.

In C programming, we use Bitwise operators for bit masking. They are-

**& (bitwise AND) :**The result of AND is 1 only if both of the bits are 1.**| (bitwise OR) :**The result of OR is 1 if either of the bits are 1.**^ (bitwise XOR) :**The result of XOR is 1 if the bits are different (one 0 and the other 1), and 0 if the bits are the same (both 0s or both 1s).**<< (left shift) :**Left shifts the bits of the first operand by places specified in the second operand.**>> (right shift) :**Right shifts the bits of the first operand by places specified in the second operand.**~ (bitwise NOT) :**The result is the opposite of the input value (1 -> 0, 0 -> 1).

Suppose we have a set S = {2, 3, 6}. We represent this set as 100110. This sequence tells us which number is included in the set as follows-

The items present in the set are marked by 1 and the rest by 0.

We can find all subsets of a set using bit masking but it takes exponential time.

Masking is done by setting all the bits except the one(s) we want to 0. Let's take a simple example. We know that we can check if a number if even or odd using modulo operator. If the number give remainder 0 when divided by 2, it is even otherwise it is odd. We can do this in another way-

Perform bitwise AND with the number that we have to check. If it is odd, it will result in 1, otherwise 0 as follows-

101 & 001 = 001

110 & 001 = 000

Here our mask is 001 which we use to check whether our given number is even or odd. We hide the unnecessary details and output only the result.

Let's say we have a 8 bit variable and we want to check if the 3rd bit from the left is a 1. Let's say our value is 00101110. To mask all the other bits, we set all the bits except the 3rd one to 0 using the & operator:

00101110 & 00100000 = 00100000

This tells use whether the 3rd bit was 0 or 1. Here, 00100000 is the mask.

If we want to keep only middle four bits of our original value and set the rest to 0, here is how we can do it-

Value = 11111111, mask = 00111100

Using AND operation on them, 11101111 & 00111100 = 00101100 (the result we required)

If we want to flip the middle four bits, we can perform XOR operation on the same value and mask.

11101111 ^ 00111100 = 11010011 (middle 4 bits are flipped)

```
#include <stdio.h>
int main()
{
int n = 5;
if(n & 1)
printf("%d is odd.", n);
else
printf("%d is odd.", n);
return 0;
}
```

5 is odd.

```
#include <stdio.h>
int main()
{
unsigned int n = 31;
n ^= 1 << 2;
printf("After flipping the second bit from right: %d", n);
//31 = 00011111, 27 = 00011011. So we can see that second bit(0 indexed) from the right has been flipped.
return 0;
}
```

27

```
#include <stdio.h>
int main()
{
unsigned int n = 24;
n |= 1 << 5;
printf("After setting the fifth bit from right to 1: %d", n);
n = 24;
n &= ~ (n << 1);
printf("\nAfter unsetting the first bit from left to 0: %d", n);
return 0;
}
```

After setting the fifth bit from right to 1: 56

After unsetting the first bit from left to 0: 8

In the above code, 24 in binary is 00011000. So, when we set the fifth bit (counting from 0) to 1, it becomes 00111000 which is 56 in decimal. When we unset the first significant bit from the left to 0, 00011000 becomes 00001000 which is 8 in decimal.

```
#include <stdio.h>
int main()
{
unsigned int n = 24;
printf("To divide a number by 2, right-shift it by one position: %d", n >> 1);
printf("\nTo multiply a number by 2, left-shift it by one position: %d", n << 1);
return 0;
}
```

To divide a number by 2, right-shift it by one position: 12

To multiply a number by 2, left-shift it by one position: 48

- It is useful for storing certain types of data in a very efficient manner.
- It can be used to solve problems where we need to deal with generating subsets of a set but this approach usually takes a lot of time.

Masking is an important programming concept that helps extract and modify specific bits in a data block while hiding or masking the rest. Bit masking is a technique that involves performing bitwise operations on data blocks to extract the required bits. In C language, bitwise operators like AND, OR, XOR, left shift, right shift, and NOT are used for bit masking.

We can represent sets and perform operations on them using masks. Masking can check whether a given number is odd or even, flip bits of a number, or set/unset specific bits in a number. Bit masking has various applications in various fields, including cryptography, computer graphics, and digital signal processing.

**1. What is the purpose of bit masking in C programming?**

The purpose of bit masking in C programming is to extract specific bits of data from a larger data value. By using bitwise operators and masks, programmers can manipulate individual bits within a variable to perform various operations.

**2. How do you perform masking in C?**

To perform masking in C, you can use bitwise operators like AND, OR, XOR, left shift, right shift, and NOT to manipulate individual bits in a variable. To create a mask, you set specific bits to 1 and others to 0.

**3. What are some common applications of bit masking in C programming?**

Some common applications of bit masking in C programming include checking the parity of a number (i.e., whether it is odd or even), extracting specific bits of data from a variable, and setting or unsetting bits within a variable.

**4. What are the advantages of using bit masking in C programming?**

The advantages of using bit masking in C programming include improved efficiency and reduced memory usage. By manipulating individual bits within a variable, programmers can perform complex operations with minimal memory overhead.

Tags:**masking****c-language**

RECENT POSTS

A Case for Using Joomla in 2024

How to Install PostgreSQL on Mac? Step by Step Guide

Identity Proofing 101: Essential Concepts And Terminologies You Should Know

How to Find Sales Tax Collected on Shopify – Mipler

End-to-End Encryption, Understanding The Basics and How to Get Started

How to Recover Lost or Deleted Data on Windows 11?