Bits Operations in c++

In this tutorial, i show how to use bits operations in c++ to manipulate bits in programming. Please if you do not understand a particular terminology used here, refer to this link for prior knowledge https://firstclicklimited.com/tutorials/index.php/2021/06/13/binary-representation-of-int-c/

int main(){
    /////////Bits Operations////////////////

    int x,i,A=0,B=0;

    //And(&) operations//
    //x & y produces 1 only if x and y are set to 1 otherwise produces 0
    //10110 & 11010 produces 10010 i.e 22 & 26 produces 18
    int y = 22&26;//prints 18
    cout<<"Prints 18: "<<y<<"\n\n";
    //in binary form
    for(i=31;i>=0;i--)
        (y&(1<<i))?cout<<"1":cout<<"0";//compare the bit in position
        //1 shifted by i places using & operator; if it produces 1,
        //then a bit was set in this position. You will see leftshift
        //operator later

    cout<<"\n\n";

    //using the & operator, we can check if a number is even or odd
    y &= 1;
    cout<<"Prints 0 because y is 18: "<<y<<"\n\n";
    y = 23; y &= 1;
    cout<<"Prints 1 because y is 23: "<<y<<"\n\n";

    //Or(|) operations//
    //x | y produces 1 when either x or y or both is set to 1
    //10110 & 11010 produces 11110 i.e 22 | 26 produces 30'
    y = 22|26;//prints 30
    cout<<"Prints 30: "<<y<<"\n\n";
    //in binary form
    for(i=31;i>=0;i--)
        (y&(1<<i))?cout<<"1":cout<<"0";

    cout<<"\n\n";

    //Xor(^) operations//
    //x ^ y produces 1 only if either x or y is set to 1
    //Note 1 ^ 1 amounts to 0, only 1 ^ 0 or 0 ^ 1 produces 1
    //10110 ^ 11010 produces 01100 i.e 22 | 26 produces 12'
    y = 22^26;//prints 12
    cout<<"Prints 12: "<<y<<"\n\n";
    //in binary form
    for(i=31;i>=0;i--)
        (y&(1<<i))?cout<<"1":cout<<"0";

    cout<<"\n\n";

    //Not(~) operations//
    //~y produces a number where all the bits of y have been inverted
    //~y=-y-1
    y = 30;
    cout<<"Prints -31: "<<~y<<"\n";
    y = ~y;
    //in binary form - this will print using Two's complement binary format
    for(i=31;i>=0;i--)
        (y&(1<<i))?cout<<"1":cout<<"0";

    cout<<"\n\n";

    //leftshift(<<) operations//
    //can be used for appending k zero bits to the number
    y = 14;
    for(i=31;i>=0;i--)
        (y&(1<<i))?cout<<"1":cout<<"0";

    cout<<endl;

    y = y << 2;

    for(i=31;i>=0;i--)
        (y&(1<<i))?cout<<"1":cout<<"0";

    cout<<endl;

    cout<<"y should print 56: "<<y<<endl;
    cout<<"\n\n";

    //Another use
    //use leftshift operator to move n positions through the bit
    //for example, to move five places in a 32bit (4 byte) variable (like int in c++) from position 1,
    //use 1<<5 - that will start from the least significant bit which is at
    //position 0 and move leftwards by 5 places. Please refer to the post
    //recommended above for better understanding.
    //You have noticed that i have been printing the binary form of a number
    //using the << operator to move to the next position to confirm if the bit
    //is set to 0 or 1
    cout<<"leftshift operation in action\n";
    for(i=31;i>=0;i--)
        (y&(1<<i))?cout<<"1":cout<<"0";

    cout<<"\n\n";

    //rightshift(>>) operations//
    //do the opposite of leftshift operation with it


    ////Other Applications of Bits Operations in c++////

    //The expression y|1<<2 sets the bit at the 2 bit index to 1
    //index starts at 0 - called the least significant bit
    //which is the index at the extreme right of the bits
    cout<<"y|1<<2 set the bit at index 2 to 1\n";
    y = y|1<<2;//start at index 0 and move two places to the left - 1 indicates index 0
    for(i=31;i>=0;i--)
        (y&(1<<i))?cout<<"1":cout<<"0";

    cout<<"\n\n";

    //the previous operation has changed y from 56 to some other number
    cout<<"y changed from 56 to: "<<y;

    cout<<"\n\n";

    //Set the bit number in the 2 bit index back to 0
    y = y & ~(1<<2);//second bit index to 0
    cout<<"bit number at index 2 back to 0\n";
    for(i=31;i>=0;i--)
        (y&(1<<i))?cout<<"1":cout<<"0";

    cout<<"\n\n";

    //the previous operation has changed y from 60 bac to 56
    cout<<"y changed from 60 back to: "<<y;

    cout<<"\n\n";

    //Set the bit number in the 2 bit index back to 1
    y = y^(1<<2);//this will invert the number at index 2
    cout<<"bit number at index 2 back to 1\n";
    for(i=31;i>=0;i--)
        (y&(1<<i))?cout<<"1":cout<<"0";

    cout<<"\n\n";

    //the previous operation has changed y from 56 back to 60
    cout<<"y changed from 56 back to: "<<y;

    cout<<"\n\n";

    ////////Sets operations using bits manipulation///////

    //Each subset of a set (0,1,2,...,n-1,n) corresponds to an
    //n bit number where 1 indicate which elements are included
    //in the subset
    int s[4] = {1,3,4,8};//subset of set (0,1,2,...,n-1,n)
    for(i=0;i<4;i++){
        A += 1<<s[i];
    }
    cout<<A<<endl;

    //Now confirm that each subset of a set (0,1,2,...,n-1,n)
    //corresponds to an n bit number where 1 indicate which
    //elements are are included in the subset
    cout<<"each subset corresponds to an n bit number\n";
    for(i=0;i<32;i++){
        if(A&(1<<i))
            cout<<i<<" ";
    }
    cout<<endl;

    int a[4] = {3,6,8,9};//subset of set (0,1,2,...,n-1,n)
    for(i=0;i<4;i++){
        B += 1<<a[i];
    }
    cout<<B<<endl;

    cout<<"\n\nA union B start here\n\n";

    int z = A|B;//sum union SUM

    for(i=0;i<32;i++){
        if(z&(1<<i))
            cout<<i<<" ";
    }
    cout<<"\n";
    cout<<__builtin_popcount(z)<<endl;
    cout<<__builtin_clz(z)<<endl;//numbers of 0 at the beginning of the bits
    cout<<__builtin_ctz(z)<<endl;//numbers of 0 at the end of the bits

    for(i=31;i>=0;i--)
        (z&(1<<i))?cout<<"1":cout<<"0";

    cout<<"\n\nA intersect B start here\n\n";
    z = A & B;

    for(i=0;i<32;i++){
        if(z&(1<<i))
            cout<<i<<" ";
    }
    cout<<"\n";
    cout<<__builtin_popcount(z)<<endl;//no of 1s' in the binary number
    cout<<__builtin_clz(z)<<endl;//numbers of 0 at the beginning of the bits
    cout<<__builtin_ctz(z)<<endl;//numbers of 0 at the end of the bits

    for(i=31;i>=0;i--)
        (z&(1<<i))?cout<<"1":cout<<"0";



    cout<<endl;
}
Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

You May Also Like