Write a program using bitwise operators to invert even bits of
a given number.
Answers were Sorted based on User's Feedback
#include<stdio.h>
int main()
{
int n,n2;
printf("enter the no. < 15 "); // here i am considering the case of 4 bits. (1111) binary = (15) decimal
scanf("%d",&n);
n2=n^10;
/*
10 = 1010 in binary form, to invert its even bits , we will
use bit wise XOR (^) operator
1010 has 1 at its even places, so it will invert the even bits of n.
if there is any further problem mail me at
buntyhariom@gmail.com
www.campusmaniac.com
*/
printf("\n%d",n2);
return 0;
}
| Is This Answer Correct ? | 10 Yes | 2 No |
Answer / ayaskanta
Use Xor operation. Xor ing with 1 coverts a 0 to 1 and a 1
to zero...
| Is This Answer Correct ? | 3 Yes | 3 No |
Why the use of alloca() is discouraged?
1. What will be the output of the following programs. a) #include <stdio.h> Main() { Int x=4; While(x==1) { X=x-1; Printf(“%d”,x); --x; } }
what does ‘Bus Error’ mean?
program to find out date after adding 31 days to a date in the month of febraury also consider the leap year
What are c preprocessors?
Write a code to generate a series where the next element is the sum of last k terms.
Explain how do you list files in a directory?
write an algorithm to get a sentence and reverse it in the following format: input : I am here opuput: Here Am I note: first letter of every word is capiatlised
enum colors {BLACK,BLUE,GREEN} main() { printf("%d..%d..%d",BLACK,BLUE,GREEN); return(1); }
How can I get back to the interactive keyboard if stdin is redirected?
How to Throw some light on the splay trees?
what would be the output of the following program? main() { int k = 123; char *ptr; ptr = &k; printf("%d",*ptr); }