Bitwise Operators are mainly used in low-level programming. Using the bit-wise operators we can set bit, check bit, clear or toggle the bits of an integral type. In the embedded system, a bit-wise operator performs the bit-wise operation on an individual bit of a PORT or Register.
Note: Quiz on bit-wise operators.
Note: Here I assume that bit of register starts with 0th
position, it means the 2nd
position is actually the 3rd bit
.
D7 | D6 | D5 | D4 | D3 | D2 | D1 | D0 |
Setting N-th Bit
Setting an N-th bit means that if the N-th bit is 0, then set it to 1 and if it is 1 then leave it unchanged. In C, bitwise OR operator (|
) used to set a bit of integral data type. As we know that |
(Bitwise OR operator) evaluates a new integral value in which each bit position is 1 only when operand’s (integer type) has a 1 in that position.
In simple words, you can say that “Bitwise OR ” of two bits is always one if any one of them is one.
That means, 0 | 0 = 0 1 | 0 = 1 0 | 1 = 1 1 | 1 = 1
Algorithm to set the bits:
Number | = (1UL << nth Position);
Method1: Set nth- bit in C using the function:
#include <stdio.h> //function to set bit int setBit(unsigned int data,unsigned int pos) { return (data|(1 << pos)); } int main() { unsigned int cData=0x00; unsigned int pos =0; printf("cData = 0x%x\n\n",cData); //Get position from the user printf("Enter the position which you want set = "); scanf("%u",&pos); //Call function to set the bit cData = setBit(cData,pos); //Print the data printf("\n\n%dth Bit Set Now cData will be = 0x%x\n",pos,cData); return 0; }
OutPut:
Explanation:
0 is represented as 0 in binary and has its D2 bit(third bit) 0, so setting it will result in 100 i.e. 4.
Method2: Set nth- bit in C using macro
#include <stdio.h> //Macro to set nth-bit #define SET_BIT(value, pos) (value |= (1U<< pos)) int main() { //value unsigned int value =0; //bit position unsigned int pos = 0; printf("Enter the value = "); scanf("%u",&value); printf("Enter the position you want to Set = "); scanf("%u",&pos); //Calling Macro to set nth-bit printf("After setting nth-bit value will be = %d\n",SET_BIT(value,pos)); return 0; }
OutPut:
Enter the value = 5
Enter the position you want to Set = 0
After setting nth-bit value will be = 5
Explanation:
5 is represented as 101 in binary and has its first bit 1. So after setting the 0th position, it will result in 101 i.e. 5.
Clearing a Bit
Clearing a bit means that if N-th bit is 1, then clear it to 0 and if it is 0 then leave it unchanged. Bitwise AND operator (&
) use to clear a bit of integral data type. “AND” of two bits is always zero if any one of them is zero.
That means, 0 & 0 = 0 1 & 0 = 0 0 & 1 = 0 1 & 1 = 1
Algorithm to clear the bit:
To clear the nth bit, first, you need to invert the string of bits then AND it with the number.
Number &= ~(1UL << nth Position);
Method1: Clear nth- bit in C using the function:
#include <stdio.h> //function to clear nth-bit int clearNthBit(unsigned int data,unsigned int pos) { return (data & (~(1 << pos))); } int main() { unsigned int cData=0xFF; unsigned int pos =0; printf("Initially cData = 0x%x\n\n",cData); printf("Enter the position which you want clear = "); scanf("%u",&pos); //clear the nth bit. cData = clearNthBit(cData,pos); //Print the data printf("\n\n%uth Bit clear Now cData will be = 0x%x\n",pos,cData); return 0; }
OutPut:
Explanation:
Clearing of D1 bit of 0XFF will give 0XFD.
Method2: Clear nth- bit in C using the macro:
#include <stdio.h> //Macro to clear nth-bit #define CLEAR_BIT(value, pos) (value &= (~(1U<< pos))) int main() { unsigned int cData=0xFF; unsigned int pos =0; printf("Initially cData = 0x%x\n\n",cData); printf("Enter the position which you want clear = "); scanf("%u",&pos); //Calling macro to clear nth-bit printf("After clearing nth-bit cData will be = 0x%x\n",CLEAR_BIT(cData,pos)); return 0; }
Output:
Initially cData = 0xff
Enter the position which you want clear = 0
After clearing nth-bit cData will be = 0xfe
Checking a Bit
To check the nth bit, shift the ‘1’ nth position toward the left and then “AND” it with the number.
An algorithm to check the bit
Bit = Number & (1UL << nth)
Method1: Check nth- bit in C using the function
#include <stdio.h> #define SET 1 #define NOT_SET 0 //function to check nth bit int isNthBitSet(unsigned int data,unsigned int pos) { return ((data & (1 << pos))? SET : NOT_SET); } int main() { //Given data unsigned int cData=0xFc; unsigned int pos =0; unsigned int isBitSet = 0; printf("Initially cData = 0x%x\n\n",cData); printf("Enter the position which you want check = "); scanf("%u",&pos); //Check bit set or not isBitSet = isNthBitSet(cData,pos); if(isBitSet) { printf("\nBit is One\n"); } else { printf("\nBit is zero\n"); } return 0; }
OutPut 1:
OutPut 2:
Method2: Check nth- bit in C using macro
#include <stdio.h> //Macro to check nth-bit #define IS_BIT_SET(value, pos) (value & (1U<< pos)) int main() { //Given data unsigned int cData=0xFc; unsigned int pos =0; printf("Initially cData = 0x%x\n\n",cData); printf("Enter the position which you want check = "); scanf("%u",&pos); //Macro to check nth bit if(IS_BIT_SET(cData,pos)) { printf("\n\nBit is One"); } else { printf("\n\nBit is zero"); } return 0; }
Good News for Aticleworld Reader, 10 Days Free Trial is Available for you from one of the most popular Learning Platforms. Don’t Waste It.
Toggling a Bit
Toggling a bit means that if the N-th bit is 1, then change it to 0 and if it is 0 then change it to 1. Bitwise XOR (^
) operator used to toggle the bit of an integral data type. To toggle the nth bit shift the ‘1’ nth position toward the left and “XOR” it.
That means, 0 ^ 0 = 0 1 ^ 0 = 1 0 ^ 1 = 1 1 ^ 1 = 0
An algorithm to toggle the bits
Number ^= (1UL << nth Position);
A simple program to toggle a bit
#include <stdio.h> #include <stdlib.h> int main() { unsigned int cData=0xF8; int pos =0; system("COLOR F"); printf("Initially cData = 0x%x\n\n",cData); printf("Enter the position which you want toggle = "); scanf("%u",&pos); //toggle the nth bit. cData^=1<<pos; //Print the data printf("\n\n%uth Bit Set Now cData will be = 0x%x\n",pos,cData); return 0; }
Output:
OutPut 2:
Recommended Post for You
- Interview questions on bitwise operators in C
- Operator Precedence And Associativity In C.
- Operators in c language
- C format specifiers.
- Structure padding and data alignment in C.
- Can protocol interview Questions.
- 100 C interview Questions.
- 100 embedded C interview Questions.
- File handling in C.
- Pointer in C.
- C format specifiers.
- Little-endian vs Big Endian.
Your opinion matters
Although here I have tried to puts a lot of points regarding the bit operators but I want to know your opinion on the binary operators, so please don’t forget to write a comment in the comment box.