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

Study the Following Points: a.One Cannot Take the address of a Bit Field b.bit fields cannot be arrayed c.Bit-Fields are machine Dependant d.Bit-fields cannot be declared as static 1. Which of the Following Statements are true w.r.t Bit- Fields A)a,b&c B)Only a & b C)Only c D)All

3 Answers   Accenture,


Is void a keyword in c?

0 Answers  


What is a 'null pointer assignment' error? Explain what are bus errors, memory faults, and core dumps?

0 Answers  


What is switch in c?

0 Answers  


what is c programming?

3 Answers   TCS,


Does c have an equivalent to pascals with statement?

0 Answers  


Is a house a shell structure?

0 Answers  


Draw a flowchart to produce a printed list of all the students over the age of 20 in a class .The input records contains the name and age of students. Assume a sentinel value of 99 for the age field of the trailer record

0 Answers   Wipro,


How can I find the modification date of a file?

0 Answers   Celstream,


Write a program that accepts a string where multiple spaces are given in between the words. Print the string ignoring the multiple spaces. Example: Input: “ We.....Are....Student “ Note: one .=1 Space Output: "We Are Student"

6 Answers   IBM,


what will be the output of" printf("%d%d",scanf("%d% d",&a&b));"

4 Answers  


wat are the two methods for swapping two numbers without using temp variable??

2 Answers  


Categories