Toggle nth bit in a given integer - num

Answers were Sorted based on User's Feedback



Toggle nth bit in a given integer - num..

Answer / sahil

num=num ^ (1<<(n-1));

Is This Answer Correct ?    51 Yes 10 No

Toggle nth bit in a given integer - num..

Answer / qint

num ^ (1 << n)

Is This Answer Correct ?    31 Yes 18 No

Toggle nth bit in a given integer - num..

Answer / sanyam jain

#include<stdio.h>
int main()
{
int a,b;
printf("Enter the number to be flipped\n");
scanf("%d",&a);
printf("Enter which bit to be flipped: ");
scanf("%d",&b);

a = a^(1<<(b-1));
printf("Resultan number is %d\n",a);
}

Is This Answer Correct ?    6 Yes 3 No

Toggle nth bit in a given integer - num..

Answer / vadivel t

#include<stdio.h>

int main()
{
int no, bit;
printf("ENTER THE NO AND BIT NO, TO TOGGLE: ");
scanf("%d %d", &
no, &bit);
if(no & (0x1 << bit-1))
{
no = no^(0x1 << bit - 1);
}
else
{
no = no | (0x1 << bit - 1);
}
printf("%d \n", no);

_getch();
}

Is This Answer Correct ?    3 Yes 3 No

Toggle nth bit in a given integer - num..

Answer / banavathvishnu

#include<stdio.h>
#include<conio.h>


Togglebit(int k,int bit)
{
printf("after %d bit is toggled = %d",bit,(k^(1<<(bit-
1))));
return 1;

}
int main()
{
int i;
int bitno;
printf("enter the number \n");
scanf("%d",&i);
printf("enter the bit number to toggle \n");
scanf("%d",&bitno);
Togglebit(i,bitno);
getch();


return 1;
}

Is This Answer Correct ?    1 Yes 4 No

Post New Answer

More C Interview Questions

write a program to arrange the contents of a 1D array in ascending order

4 Answers  


define c

6 Answers   HCL, TCS,


if function is declared as static in one source file, if I would like to use the same function in some other source file...is it possible....how ?

2 Answers   NetApp,


What do you mean by keywords in c?

0 Answers  


the portion of a computer program within which the definition of the variable remains unchanged a) mode b) module c) scope d) none

0 Answers  


Is a pointer a kind of array?

0 Answers  


What is a scope resolution operator in c?

0 Answers  


How can I read a directory in a C program?

2 Answers   Bright Outdoor, Wipro,


What is the purpose of void pointer?

0 Answers  


write a program that prints a pascal triangle based on the user input(like how many stages) in an efficient time and optimized code?

3 Answers   Oracle,


Write a program in c to print 1 121 12321 1234321 123454321

11 Answers   ANR, College School Exams Tests, Mu Sigma, Wipro,


what is the output of the following program? #include<stdio.h> void main() { int x=4,y=3,z; z=x-- -y; printf("\n%d %d %d",x,y,z); }

14 Answers  


Categories