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 is the main difference between calloc () and malloc ()?

573


in programming languages a statement or part of a statement that specifies several different execution sequences a) constructs b) distructs c) executes d) none

603


WHICH TYPE OF JOBS WE GET BY WRITING GROUPS .WHEN THE EXAMS CONDUCTED IS THIS EXAMS ARE CONDUCTED EVERY YEAR OR NOT.PLS TELL ME THE ANSWER

1461


Differentiate between calloc and malloc.

760


Hello. How to write a C program to check and display president party like if i type in the console "biden" and hit enter the output shoud be : "biden is democrat" and if i type "trump" and hit enter the output shoud be: "trump is republican"

1582






What are runtime error?

629


How does sizeof know array size?

629


What is the data segment that is followed by c?

613


Explain what could possibly be the problem if a valid function name such as tolower() is being reported by the c compiler as undefined?

583


Explain what is meant by high-order and low-order bytes?

635


How can you increase the size of a statically allocated array?

615


Why is c not oop?

539


What is difference between array and structure in c?

580


What is the modulus operator?

736


How can a process change an environment variable in its caller?

656