HomeLinuxBit Masking in C++

Bit Masking in C++


In C++, a way known as bit masking is used to manage particular bits inside a binary integer utilizing the bitwise operators. It entails making a bit sample that displays the actual bits that it’s good to alter and carry out completely different operations on the binary quantity utilizing the bitwise operators like AND, OR, and XOR. When working with integers or different binary knowledge sorts, bit masking is often used to extract or change sure bits. You could accomplish particular targets reminiscent of setting or clearing a selected bit, eradicating a selected assortment of bits from an integer, or finishing up completely different bitwise operations by combining these operators with bit patterns.

Syntax of Bit Masking in C++
Bitwise operators and bit patterns can be utilized in C++ to supply bit masking. Right here is an illustration of bit masking syntax. First, we make the most of the bitwise OR operator (|) with a bit sample that incorporates a “1” within the location of the bit that we want to set and a “0” in all different positions to set a selected bit in an integer variable.

Second, the bitwise AND operator (&) with a bit sample incorporates a “0” within the location of the bit that we want to clear and a “1” in all different positions to clear a selected bit in an integer variable.

Third, we use the bitwise XOR operator () and a bit sample with a “1” within the location of the bit that you just want to toggle and a “0” in all different positions to toggle a selected bit in an integer variable.

Using the Bit Masking in Changing the 32-Bit Shade Code to RGB Worth

To know the ideas, we reveal a easy instance the place we convert the 32-bit shade code of purple, inexperienced, and yellow into RGB values. First, we embody the fundamental header information in this system. By together with this header file, we achieve entry to a number of helpful lessons and features to work with enter and output operations.

Then, we construct three-bit masks with the next hexadecimal values: redMask with a price of 0xFF0000, greenMask with a price of 0x00FF00, and blueMask with a price of 0x0000FF. We are able to now extract the particular RGB values due to this. We use the bit masks to separate the bits that correspond to the colour code of purple, inexperienced, and blue values.

Then, we apply every bit masks to the colour code utilizing the bitwise AND operator (&). With all different bits set to 0, the end result is a brand new binary worth that solely incorporates the bits that correspond to the purple, inexperienced, or blue worth relying on the RGB worth that we extract. The bits are then shifted to the fitting by the mandatory variety of positions utilizing the fitting shift operator (>>).

The bits that we want to extract should not within the least important bit place, that’s why that is required. RedValue, greenValue, and blueValue are integer variables that maintain the extracted purple, inexperienced, and blue values, respectively. To verify that we correctly retrieved the fitting RGB values from the colour code, we output these values utilizing the cout instructions.

#embody <iostream>
utilizing namespace std;
int major()
{
    int colorCode = 0x7FFFB5;
    int redMask = 0xFF0000;
    int greenMask = 0x00FF00;
    int blueMask = 0x0000FF;
    int redValue = (colorCode & redMask) >> 16;
    int greenValue = (colorCode & greenMask) >> 8;
    int blueValue = colorCode & blueMask;
    cout << “Crimson: “ << redValue << endl;
    cout << “Inexperienced: “ << greenValue << endl;
    cout << “Blue: “ << blueValue << endl;
    return 0;
}

Right here is the output of the previously-implemented instance:

Performing Totally different Bitwise Operations on Two Integers

This code demonstrates carry out the completely different bitwise operations on two integers. First, we add the “iostream” library in order that we will get the enter and output. Subsequent, we use the key phrase to carry your entire std namespace into the present scope of our program. Then, we name the principle() perform. In the principle() perform, we outline two integer variables, “a” and “b”, and provides every of them the numbers 10 which is binary 1010 and 12 which is binary 1100. Then, we use the bitwise AND, OR, XOR, NOT, left shift, and proper shift operators.

First, we carry out the bitwise AND operation. The 2 integer variables “a” and “b” are bitwise operated on utilizing the bitwise AND operator. The binary representations of “a” and “b” are in contrast utilizing the & operator which creates a brand new worth with every bit set to “1” if and provided that each enter bits are “1”. So, the worth of c is assigned to the results of the bitwise AND operation which is 8 (binary 1000).

The bitwise OR operation is completed subsequent. If both of the enter bits is “1”, the OR operator compares the bits within the binary representations of “a” and “b” and creates a brand new worth within the “d” variable with every bit set to “1”.

Now, shifting on to the following operation is bitwise XOR. In XOR, the 2 enter values “a” and “b” are bitwise mixed and produce the output 6 that’s saved in “e” variable. Subsequent, now we have a NOT operator that’s utilized to the one enter worth. The bitwise NOT operation flips the entire bits within the worth and provides “1” to the consequence. So, we get the “-11” output (binary 11111111111111111111110101) within the “f” variable.

#embody <iostream>
utilizing namespace std;
int major() b = “ << d << endl;
    int e = a ^ b;
    cout << “a ^ b = “ << e << endl;    
    int f = ~a;
    cout << “~a = “ << f << endl;
    int g = a << 2;
    cout << “a << 2 = “ << g << endl;    
    int h = b >> 2;
    cout << “b >> 2 = “ << h << endl;
    return 0;

The following operator is the left shift operator () which strikes the enter worth’s bits to the left by a predetermined variety of locations. The a << 2 produces the output 40 of binary worth 101000 as a result of the bits of “a” are shifted two positions to the left. Just like the left shift operator, the fitting shift operator (>>) strikes the bits of the enter worth to the fitting by a predetermined quantity, so b >> 2 leads to output 3. On the finish of this system, we return “0” to the principle() perform in order that the execution of this system stops and it shows the output.

Conclusion

This text demonstrated what bit masking in C++ programming is. We’ve many operators to hold out the varied bit masking operations. To raised comprehend the concept, we practiced the varied situations in addition to realized implement the bit masking in C++ programming language.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments