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

what is the difference between <stdio.h> and "stdio.h"

14 Answers   Invendis, Kanbay, Mastek, MathWorks,


What is the size of empty structure in c?

0 Answers  


What are # preprocessor operator in c?

0 Answers  


what value is returned to operating system after program execution?

0 Answers  


What are the basic data types associated with c?

0 Answers  






#include<stdio.h> #include<conio.h> void main() { char ch='\356'; printf("%d",ch); } o/p=-18 why?plz.explain

2 Answers  


give an example of type casting by a simple c program

2 Answers   TCS,


What are the advantages of using new operator as compared to the function malloc ()?

0 Answers   NIIT,


44.what is the difference between strcpy() and memcpy() function? 45.what is output of the following statetment? 46.Printf(“%x”, -1<<4); ? 47.will the program compile? int i; scanf(“%d”,i); printf(“%d”,i); 48.write a string copy function routine? 49.swap two integer variables without using a third temporary variable? 50.how do you redirect stdout value from a program to a file? 51.write a program that finds the factorial of a number using recursion?

6 Answers   Amdocs,


how to do in place reversal of a linked list(singly or doubly)?

3 Answers  


c language interview questions & answer

0 Answers  


implement OR gate without using any bitwise operator.

1 Answers   Alcatel, Wipro,


Categories