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

Differentiate between full, complete & perfect binary trees.

0 Answers  


Does * p ++ increment p or what it points to?

0 Answers  


void main() { for(; 0 ;) ... { printf("hello"); ... } getch(); }

1 Answers  


count the numbers between 100 and 300, that star with 2 and ends with 2

5 Answers   Mind Tree,


what is the output of the following code? main() { int I; I=0x10+010+10; printf("x=%x",I); } give detailed reason

3 Answers  


What is typedef?

1 Answers  


How can I find out the size of a file, prior to reading it in?

0 Answers  


Function to find the given number is a power of 2 or not?

20 Answers   Motorola, nvidia,


What does char * * argv mean in c?

0 Answers  


Find greatest number out of 10 number without using loop.

5 Answers   TCS,


int x=5; printf("%d%d%d",x,x<<2,x>>2);

2 Answers   TANCET,


write a program without using main function?

2 Answers   TCS,


Categories