#include<stdio.h>
void main()
{ int x,y; x=200; y=140; printf("\nBefore swapping x=%d,y=%d",x,y); x=x^y; y=x^y; x=x^y; printf("\nAfter swapping x=%d y=%d",x,y); }
Search This Blog
Showing posts with label exor. Show all posts
Showing posts with label exor. Show all posts
Program to Swap Numbers with Bitwise (EXOR) Operators
Here is a c program to swap two numbers. This method of swapping does not use a third (temporary) variable. Instead, swapping is done by bitwise operation. We perform bitwise EXOR operations on the two numbers. The program is as follows:
Program to Understand Bitwise Operators
This is a C program that shows working of bitwise operators. The program takes two values, a and b. And displays the different bitwise operations' results. The program performs bitwise AND, bitwise OR, bitwise XOR, bitwise inversion (NOT), right bit shift and left bit shift operations.
#include<stdio.h>
main ()
{
unsigned int a = 60; /* 60 = 0011 1100*/
unsigned int b = 13; /* 13 = 0000 1101 */
printf ("a=60 ( 0011 1100)\nb=13 ( 0000 1101)\n");
int c = 0;
c = a&b ; /* 12 = 0000 1100 */
printf ("a&b=%d\n",c );
c = a | b ; /* 61 = 0011 1101 */
printf ("a|b=%d\n",c );
c = a ^ b ; /* 49 = 0011 0001 */
printf ("a^b=%d\n", c );
c = ~ a; /*-61 = 1100 0011 */
printf ("~a=%d\n" , c );
c = a << 2 ; /* 240 = 1111 0000 */
printf ("a<<2=%d\n" , c );
c = a >> 2 ; /*15 = 0000 1111*/
printf ("a>>2=%d\n" , c );
}
Subscribe to:
Posts (Atom)