OneCompiler

all general bit operations

181

#include <iostream>
using namespace std;

// for checking weather it is 0 or 1 on that particular position

bool getbit( int n, int pos)
{
int result;

result = (n & ( 1 << pos));
return result ;
}

// making bit 1 at particualar position is known as SET bit

int setbit( int n, int pos)
{
return (n | ( 1 << pos));
}

// making bit 0 at particular position is clear bit

int clearbit( int n, int pos)
{
int mask= ~(1<<pos);
return ( n & mask) ;
}

// updating bit at particular position 1 or 0 is known as update bit

int updatebit(int n, int pos, int val)
{
int mask= ~(1 << pos);
n = n & mask;

return ( n | (val << pos) );
}

int main()
{

// cout << getbit(5,2);

// cout << setbit( 5, 1);

// cout << clearbit(5,2);

// cout<<updatebit(5,1,1);

return 0;

}