Heavier your skills |
Bitwise Operators Interview question in C
Q1) Check the sign of an Integer ?!
Note: In Binary no. , we have MSB (Most Significant Bit) responsible of the sign of number .
If (MSB) bit is set , this mean that number is Negative .
If (MSB) bit is set , this mean that number is Negative .
Q2) Detect if two integers have opposite signs ?
The two integers are opposite signs , If the MSB bit is different . We will use the XOR bitwise operator to check the sign of integer .
Note : In XOR operator the same input produces LOW output , if different input produces HIGH output .
XOR TABLE |
Q3) Write a program to check an integer is a power of 2 ?
we will write algorithm to check the integer power of 2 . by using flag ,if the flag is TRUE the number will be power of 2 .
Q4) How to set a particular bit in C ?
we know that Bitwise OR operator used to set bit by ONE, if one of them bits is 1 .
So will write an algorithm to set particular bit using OR and SHEFT LEFT .
Data |= (1<<position no.)
Q5) How to clear a particular bit in C ?
we know that Bitwise AND operator used to clear bit , if one of them bits is 0 .
So will write an algorithm to set particular bit using AND and SHEFT LEFT .
Data & = ~(1 << position no.)
Q6) How to check if a particular bit is set in C?
To check the bit position is set or not, SHEFT LEFT the "1" by n position then "AND " with number .
So will write an algorithm to check the bit set or not . particular bit using and .
Bit = Data & (1 << position no.)
Q7) How to Toggle a particular bit in C?
we know that XOR used to toggle the bit .
So we write an algorithm to toggle bit .
Data ^= (1 << position no.).
Q8) Write an efficient c program to revers bits of a number ?
Post a Comment