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 / gokulnaathan

#include <stdio.h>
int main()
{
unsigned int data = 0x000000f0;
int bitpos = 4;
int bitvalue = 1;
unsigned int bit = data;
bit = (bit>>bitpos)&0x00000001;
int invbitvalue = 0x00000001&(~bitvalue);
printf("%x\n",bit);

if(bitvalue ==0)
{
if(bit==0)
printf("%x\n",data);
else
{
data = (data^(invbitvalue<<bitpos));
printf("%x\n",data);
}
}
else
{
if(bit==1)
printf("elseif %x\n",data);
else
{
data = (data|(bitvalue<<bitpos));
printf("else %x\n",data);
}
}

}

Is This Answer Correct ?    1 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What does 3 mean in texting?

605


What is the main difference between calloc () and malloc ()?

568


What is a volatile keyword in c?

634


What is the difference between printf and scanf )?

585


how to write a c program to print list of fruits in alpabetical order?

1785






How do I swap bytes?

626


Explain the difference between structs and unions in c?

572


What are predefined functions in c?

559


What is difference between array and pointer in c?

534


What is indirection? How many levels of pointers can you have?

650


What does the message "automatic aggregate intialization is an ansi feature" mean?

689


How are portions of a program disabled in demo versions?

742


Explain how can you determine the size of an allocated portion of memory?

616


What are the advantages of using macro in c language?

585


What is c++ used for today?

657