write a c program to change only the 3rd bit of the
particular number such that other bits are not affected..
if bitnum=10(say.. it can be any no..

Answer Posted / om

int change_third_bit_only(int n, int bitToBeChanged)
{
int k1=1<<(bitToBeChanged-1) ;
//This is same as// int k1=pow(2, bitToBeChanged-1);
int k2=~k1;
if((n & k1) == 0)
//This means bitToBeChanged th bit in number n is 0
n=n | k1; // here changing it to 1
else //otherwise the bitToBeChanged th bit in number n is 1
n=n & k2; // here changing it to 0

return n;
}

Is This Answer Correct ?    4 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Where in memory are my variables stored?

837


List some applications of c programming language?

732


How do you print only part of a string?

781


What is the difference between the local variable and global variable in c?

721


can we have joblib in a proc ?

1922


What is the difference between c &c++?

839


The number of measuring units from an arbitarary starting point in a record,area,or control block to some other point a) recording pointer b) offset c) branching d) none

973


What is c preprocessor mean?

1031


Which is the memory area not included in C program? give the reason

1710


What is the use of structure padding in c?

794


What is switch in c?

841


What is dynamic memory allocation?

1045


What is the most efficient way to store flag values?

897


What is array within structure?

824


What are examples of structures?

785