EASY
Select 4th Bit
Write a program to select the 4th bit from an integer value x.
Constraints
- Use a bit mask.
- Use the bitwise AND operator.
- Variable
x = 174(0b10101110)
#include <stdio.h>
int main() {
/* binary representation of 174 is
10101110 */
int x = 0b10101110;
/* Select the 4th bit (mask 0...01000)
*/
int bit4 = (x & (1 << 3))>> 3;
printf("The 4th bit of x is %d\n",
bit4);
/* Output: 1 */
return 0;
}