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 is null in c?
Which is better malloc or calloc?
Why shouldn’t I start variable names with underscores?
What is a null pointer in c?
When do you not use the keyword 'return' when defining a function a) Always b) Never c) When the function returns void d) dfd
What is static and volatile in c?
Can you please explain the difference between malloc() and calloc() function?
What language is windows 1.0 written?
What is use of pointer?
Why doesn't C support function overloading?
what is the function of pragma directive in c?
Explain about the functions strcat() and strcmp()?
What is the default value of local and global variables in c?
Why do we use & in c?
What is break statement?