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 |
What are the disadvantages of a shell structure?
Difference between malloc() and calloc() function?
What is the difference between single charater constant and string constant?
how the compiler treats any volatile variable?Explain with example.
What does sizeof function do?
Why enum is used in c?
how to find out the reverse number of a digit if it is input through the keyboard?
write a c code "if you give a any decimal number then print that number in english alphabet"? ex: i/p: 552 o/p: five hundred fifty two ...
What is structure and union in c?
How can I display a percentage-done indication that updates itself in place, or show one of those twirling baton progress indicators?
What is scope and lifetime of a variable in c?
Difference between C and Embedded C?