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
How do you generate random numbers in C?
How many levels of pointers can you have?
Is using exit() the same as using return?
What is the difference between mpi and openmp?
How many keywords (reserve words) are in c?
How do you override a defined macro?
What are the 5 elements of structure?
What is merge sort in c?
Is return a keyword in c?
Why can’t constant values be used to define an array’s initial size?
What is memcpy() function?
What is the scope of global variable in c?
What is a wrapper function in c?
Explain what’s a signal? Explain what do I use signals for?
What is return in c programming?