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
What are the 5 elements of structure?
What are data structures in c and how to use them?
Explain the meaning of keyword 'extern' in a function declaration.
Is c language still used?
What is table lookup in c?
What does void main () mean?
praagnovation
Explain how can you avoid including a header more than once?
What is the time and space complexities of merge sort and when is it preferred over quick sort?
How is pointer initialized in c?
Explain is it valid to address one element beyond the end of an array?
Do you know the difference between exit() and _exit() function in c?
When should volatile modifier be used?
When is a null pointer used?
What is volatile variable how do you declare it?