Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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

What are the 5 elements of structure?

1084


What are data structures in c and how to use them?

1173


Explain the meaning of keyword 'extern' in a function declaration.

1155


Is c language still used?

985


What is table lookup in c?

1101


What does void main () mean?

1240


praagnovation

2287


Explain how can you avoid including a header more than once?

1093


What is the time and space complexities of merge sort and when is it preferred over quick sort?

1083


How is pointer initialized in c?

1026


Explain is it valid to address one element beyond the end of an array?

1218


Do you know the difference between exit() and _exit() function in c?

1062


When should volatile modifier be used?

1015


When is a null pointer used?

1117


What is volatile variable how do you declare it?

1103