![]() |
In this article, we are going to learn about Bitmask in C which is a powerful way to basically work with bits and functions upon boolean logic. It can be used to store data compactly and with much more efficiency in certain cases. What is a Bit?A bit is the smallest unit of data which can either store a 0 or 1 inside it. All the data in the computer is stored using these bits. These 2 possible values can also be represented as boolean values that are True or False. Using this we can apply boolean logic to manipulate data stored on the computer. Bitmasking in CBitmasking is a technique that involves bit manipulation. It is basically like putting a mask over certain bits and hiding the other un-useful bits, so as to make the program much more efficient and optimize the memory. A bitmask is a sequence of bits that can also be known as a bitset or bit field and is used to perform bitwise operations on the given data. There are basically 6 bitwise operators in C that can be used to manipulate bits which are as follows:
Using these operators, we perform different bit masking techniques according to the requirements. Let’s discuss these techniques and how to implement them. Bitmasking Techniques1. Setting a BitIn this technique, we set a particular bit to 1 without touching any of the other bits. For this, we use the bitwise OR ( | ) operator and the left shift (<<) operator. Basically, we take the integer 1 and using the left shift operator, shift the binary representation of 1 (that is 1 only) to n places where (n+1) is the place of bit which we want to set. Then using the bitwise OR operator we turn the given number’s (n+1)th bit to 1. Syntaxnumber | (1 << bit_position_to_set) Example:C
Output
Ans: 45 2. Clearing a BitIn this operation, we set a specific bit to 0 (as opposed to 1 in the previous case) without touching any of the other bits. We use the bitwise AND operator (&), bitwise NOT operator (~), and the left shift operator (<<) to achieve the task. Basically, we again take 1 and shift it the the specified position. Then, we perform the NOT operation on this to convert that into a 0 and other bits of the value (1<<n) to 1. Then we do the AND operation to clear the specified bit and obtain the result. Syntax:number & ~(1 << bit_position_to_clear) Example:C
Output
Ans: 9 3. Flipping a BitIn this operation, we flip a specific bit that is if the bit is 0 then turn it to 1 else turn it to 0. This operation requires the use of bitwise XOR (^) operator along with the left shift (<<) operator. Basically, as in previous operations, we shift 1 to the specified number of positions and then perform the XOR operation to flip the bit of the given number. Syntax:number ^ (1 << bit_position_to_flip) Example:C
Output
Ans: 5 4. Checking a BitIn this operation, we check if a particular bit is 1 or not using the bitwise AND operator (&) along with the left shift operator (<<). We shift 1 using the left shift operator to the specified position and then perform the bitwise AND operation on that so as to check if that specific bit is 0 or 1. If the bit is 0 then the result would be 0 else the result would be 2^(bit_position). Syntax:number & (1 << bit_position_to_check) Example:C
Output
Ans: 8 Ans: 0 Application of Bitmasking in CFollowing are some main applications of bit masking in the C programming language.
Related Articles |
Reffered: https://www.geeksforgeeks.org
C Language |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 13 |