All submissions

Set or Clear a Specific Bit in a Register

Code

#include <stdio.h>







int setorclear(int num,int pos,int con){
     int result;
    if(con==1){
    result=num | (1 << pos);
    }
    else{
        result=num & ~(1<<pos);
    }
return result;
}

int main(){
    int num;
    int pos;
    int con;
    
    //printf("enter a number or An 8-bit integer (0-255) ");
    scanf("%d",&num);
     //printf("enter the bit position to modify that position ");
    scanf("%d",&pos);
     //printf("enter a 1 for operation set and 0 for clear");
    scanf("%d",&con);
    
    printf("%d", setorclear(num,pos,con));

}

Solving Approach

 

 

 

Loading...

Input

10 3 1

Expected Output

10