#include <stdio.h>
int main() {
int num = 10;
int mask = 7;
// Turn on the 3rd and 1st bits
num = num | (1 << 2);
num = num | (1 << 0);
// Turn off the 2nd bit
num = num & ~(1 << 1);// 1101
printf("%d turn off\n",num);
// Check if the 3rd bit is set
int is_set = num & (1 << 2);//imp//if we print we get the value of bit
printf("%d check\n",is_set);
if (is_set) {
printf("The 3rd bit is set %d\n", is_set);//imp
} else {
printf("The 3rd bit is not set\n");
}
// Mask out the lower 3 bits and print the result
int result = num & mask;
printf("The masked number is: %d\n", result);
return 0;
}
#if 0
a = 5 (0101 in binary)
(1 << 2) = 4 (0100 in binary)
a & (1 << 2) = 0101 & 0100 = 0100 (4 in decimal)
here a & (1 << 2) is used to check if its set or not
#endif
To embed this program on your website, copy the following code and paste it into your website's HTML: