If mth bit set, reset nth bit

 #include <cmath>

// Online C++ compiler to run C++ program online
#include <iostream>
#include <bitset>
using namespace std;

int main() {
    int num;
    cin>>num;
    bitset <32> y(num);
    cout<<y<<endl;
    int n=3,m=0;
    
    int temp=num;
    while(n!=0){
        temp=temp>>1;
        n--;
    }
    cout<<temp<<endl;
    //temp>>1;
    cout<<temp<<endl;
    if(temp%2==1){
        int i=1;
        while(m!=0){
            
            i=i<<1;
            m--;
        }
        //i=i<<1;
        i=~(i);//1's complement, mask
        bitset <32> x(i);
        cout<<x<<endl;
        num=num&i;
        
    }
    cout<<num<<endl;
    bitset <32> z(num);
    cout<<z<<endl;
    
    
    
    

    return 0;
}

Comments

Popular posts from this blog

Sum of Even Numbers till N

Find the Runner-Up Score!

Print All Substrings