set nth bit of a number

1. read a number x
2. rad the position n
3. To set a particular bit of number. Left shift 1 n times and perform bitwise OR operation with x. Which is num = (1 << n) | x;.

Program

#include<stdio.h>
int main() {
    int n,x,num;
    scanf("%d%d",&x,&n);
    num = (1 << n) | x;
    printf("Number before Setting %d\n",x);
    printf("Number after Setting %d",num);
return 0; 
}

Input:

8 2

Output

Number before Setting 8
Number after Setting 12

No comments:

Post a Comment