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..

Answers were Sorted based on User's Feedback



write a c program to change only the 3rd bit of the particular number such that other bits are not ..

Answer / binu

YOU CAN SET OR CLEAR THE THIRD BIT without changing other bits

#define BIT3 (0X01<<3)
void main()
{
int a;
//set the third bit of a
a|=BIT3;
//clear the third bit
a&=~BIT3;

Is This Answer Correct ?    11 Yes 1 No

write a c program to change only the 3rd bit of the particular number such that other bits are not ..

Answer / ajay karanam

int main()
{
int number=0;
int bitTobeChanged=0;
int endResult=0;

printf("Enter the number\n");
scanf("%d",&number);

printf("Enter the bit to be changed\n");
scanf("%d",&bitTobeChanged);

endResult=number|(0x1<<(bitTobeChanged-1));

printf("End Result = %d\n",endResult);

return 0;
}

Is This Answer Correct ?    18 Yes 10 No

write a c program to change only the 3rd bit of the particular number such that other bits are not ..

Answer / vasanth

hi Ajay... that is a great work.. well done.. but ur program
works only to change 0 to 1 of any bit but not 1 to 0..
anyhow good job...

Is This Answer Correct ?    8 Yes 1 No

write a c program to change only the 3rd bit of the particular number such that other bits are not ..

Answer / om

void toggle_kth_bit_IMPROVED(int *n, int bitToBeChanged)
{
*n=*n^(1<<(bitToBeChanged-1));
}

Is This Answer Correct ?    7 Yes 1 No

write a c program to change only the 3rd bit of the particular number such that other bits are not ..

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

write a c program to change only the 3rd bit of the particular number such that other bits are not ..

Answer / vadivel t

assume int holds 4bytes...

For ex:

#include <stdio.h>
int main()
{
int i = 16;
if(i & 0x04)
{
/*3rd bit is set to 1- so reset it to 0 - other bits will
not be disturbed*/
i = i & 0xFFFFFFFFB;
printf("IN IF \n");
}
else
{
/*3rd bit is set to 0- So set it to 1 - other bits will
not be disturbed*/
i = i | 0x00000004;
}
printf("%d", i);
_getch();
return 0;
}

Is This Answer Correct ?    4 Yes 1 No

write a c program to change only the 3rd bit of the particular number such that other bits are not ..

Answer / ravikumar

14

Is This Answer Correct ?    8 Yes 6 No

write a c program to change only the 3rd bit of the particular number such that other bits are not ..

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

write a c program to change only the 3rd bit of the particular number such that other bits are not ..

Answer / kishore batta

#include<stdio.h>
int main()
{
int number=0,set_bit=0,result=0;
printf("Enter the number:");
scanf("%x",&number);
printf("which bit to be set?:");
scanf("%x",&set_bit);
result=number|(0x1<<(set_bit-1));
printf("After setting the bit, the result is:%x
",result);
return 0;
}
OUTPUT:
Enter the number:1
which bit to be set?:4
After setting the bit, the result is:9

Is This Answer Correct ?    0 Yes 1 No

write a c program to change only the 3rd bit of the particular number such that other bits are not ..

Answer / vignesh1988i

it's not possible to change the bit individually by the user
.. the user doen't have permission..... it should not be
allowed for security purposes... then the data can be easily
hacked....



thank u

Is This Answer Correct ?    1 Yes 26 No

Post New Answer

More C Interview Questions

write a method for an array in which it can display the largest n next largest value.

1 Answers   Value Labs,


Go through this linked list concept.While traversing through the singly linked list sometimes the following code snippet "while(head != NULL)" is used and other times "while(head->link != NULL)"is used(Here head is the pointer pointing to the first node,node has two parts data part and link part).What is the difference between head != NULL and Head->link != NULL and in which situation are they used?

1 Answers   Oracle,


how to print a statement in c without use of console statement ,with the help of if statement it should print

2 Answers   Satyam,


How do you define a function?

0 Answers  


Why c is known as a mother language?

0 Answers  






Why do we need a structure?

0 Answers  


why we need function pointers?

3 Answers  


where are auto variables stored? What are the characteristics of an auto variable?

0 Answers  


struct ptr { int a; char b; int *p; }abc; what is d sizeof structure without using "sizeof" operator??

9 Answers   Verifone,


Print all the palindrome numbers.If a number is not palindrome make it one by attaching the reverse to it. eg:123 output:123321 (or) 12321

7 Answers   HCL,


Write a program to use switch statement.

0 Answers   Agilent, Integreon, ZS Associates,


main use of recursive function a) processing speed high b) reduce program length/reduce repeated statements c) if you do not, use iterative methods like, for, while or do-while d) all the above

0 Answers  


Categories