Bitwise Operations

As Embedded C is an extension of C Language. It inherits all the fundamental operators of C. In C there are 6 bitwise operators. These operators Operate on Binary Level on the data can be seen in the below table :

Bitwise Operations Embedded C Interview Questions :

Q1: Write a program to select the 4th bit from an integer value 'x' using a bit mask and the bitwise AND operator.
Ans:

#include 
int main() { int x = 0b10101110; /* binary representation of 174 */ /* Select the 4th bit using a bit mask and the bitwise AND operator */ int bit4 = (x & (1 << 3)) >> 3; printf("The 4th bit of x is %d\n", bit4); /* should print 1 */ return 0; }

Q2: Write a program to clears the 2nd bit from an integer value 'x' using a bit mask and the bitwise AND operator.
Ans:

#include 
int main() { int x = 0b11011101; /* binary representation of 221 */ /* Clear the 2nd bit using a bit mask and the bitwise AND operator */ x &= ~(1 << 1); printf("x after clearing the 2nd bit: %d\n", x); /* should print 219 (0b11011011) */ return 0; }

Q3: Write a program to set the 3rd bit of an integer value 'x' using a bit mask and the bitwise OR operator.
Ans:

#include 
int main() { int x = 0b01101001; /* binary representation of 105 */ /* Set the 3rd bit using a bit mask and the bitwise OR operator */ x |= (1 << 2); printf("x after setting the 3rd bit: %d\n", x); /* should print 109 (0b01101101) */ return 0; }

Q4: Write a program to set the nth bit of a given integer to 1.
Ans:

#include 
int main() { int num, n; printf("Enter a number: "); scanf("%d", &num); printf("Enter the bit position to set: "); scanf("%d", &n); num |= (1 << n); printf("The new number is: %d", num); return 0; }

Q5: Write a program to toggle the nth bit of a given integer.
Ans:

#include 
int main() { int num, n; printf("Enter a number: "); scanf("%d", &num); printf("Enter the bit position to toggle: "); scanf("%d", &n); num ^= (1 << n); printf("The new number is: %d", num); return 0; }

Q6: Write a program to count the number of set bits in a given integer.
Ans: 

#include 
int main() { int num, count = 0; printf("Enter a number: "); scanf("%d", &num); while (num != 0) { if (num & 1) { count++; } num >>= 1; } printf("The number of set bits is: %d", count); return 0; }

Q7: Write a program to count the number of zeroes in a given integer using bitwise operator.
Ans:

#include 
int main() { int num, count = 0; printf("Enter a number: "); scanf("%d", &num); while (num != 0) { if ((num & 1) == 0) { count++; } num >>= 1; } printf("The number of zero bits is: %d", count); return 0; }

Q8: Write a program to multiply an integer value 'x' by 2 using left shift operator.
Ans: 

#include 
int main() { int x = 10; /* Multiply x by 2 using left shift */ x = x << 1; printf("x after left shift: %d\n", x); /* should print 20 */ return 0; }

Q9: Write a program to check if the nth bit of a given integer is set or not.
Ans: 

#include 
int main() { int num, n; printf("Enter a number: "); scanf("%d", &num); printf("Enter the bit position to check: "); scanf("%d", &n); if ((num >> n) & 1) { printf("The bit is set.\n"); } else { printf("The bit is not set.\n"); } return 0; }

Q10: Write a program to divide an integer value 'x' by 2 using right shift operator.
Ans: 

#include 
int main() { int x = 10; /* Divide x by 2 using right shift */ x = x >> 1; printf("x after right shift: %d\n", x); /* should print 5 */ return 0; }

Q11: Write a program to swap two values of integer variables using XOR operator. 
Ans:

#include 
int main() { int x = 10; int y = 20; /* Swap x and y using XOR */ x = x ^ y; y = x ^ y; x = x ^ y; printf("x after XOR swap: %d\n", x); /* should print 20 */ printf("y after XOR swap: %d\n", y); /* should print 10 */ return 0; }