Ad

Saturday, March 15, 2008

One's Complement and XOR Operators

Talking about Bit Operators we are left with two of them, which we’ll be discussing in this article.

One’s Complement Operator (~)

It takes and works only on one operand. On taking one’s complement of any variable, the 0s are changed to 1 and vice-versa from the bit structure (binary representation) of that variable. The following example will make it easier to understand:

Suppose we have a short int a

short int a = 16;

its binary representation will be

0000000000010000 (decimal 16)

on taking one’s complement like below

res = ~a;

res will contain

1111111111101111 (decimal 65519)

It can be used as a part of algorithm to encrypt data.

XOR (eXclusive OR) (^)

It is derived from the OR Operator and takes two operands to work on. It compares bits like the OR bitwise operator but exclusively for OR cases.

Following will clarify what it does:

short int a = 46265, its binary form

1011010010111001

another short int b = 46734, binary

1011011010001110

performing XOR operation

a -> 1011010010111001
b -> 1011011010001110
XOR'ed-> 0000001000110111

As you can see, it compares two bits (from variable a and b) and if both are same it gives 0 or 1 in any other case. Thus we can say it does an eXclusive OR comparison between the bit structure of two variables.

now let's look at an example code showing how these two operators are used:


  // Example Program to demonstrate how
  // One's Complement (~) and XOR (^)
  // Opeartors are used.
  #include<stdio.h>

  // prototype
  void showbits(short int);

  // defined
  void showbits(short int dec_num)
  {
    short int loop, bit, and_mask;

    for(loop=15; loop>=0; loop--)
    {
      and_mask=1<<loop;
      bit=dec_num&and_mask;


      if(bit==0) printf("0");
      else printf("1");
    }
  }

  void main()
  {
    // declare three short ints
    // for storing user inputs
    // and results
    short int a,b,res;
    int ch;

    while(ch!=3)
    {
      // show main menu
      printf("\t\tMain Menu\n");
      printf("\t\t---------\n");
      printf("1. Perform One's Complement Operation\n");
      printf("2. Perform XOR Operation\n");
      printf("3. Quit\n");
      scanf("%d",&ch);

      switch(ch)
      {
      case 1:
        // take input
        printf("\n\nEnter a decimal number: ");
        scanf("%d",&a);

        printf("\n\n\tEntered Number: ");
        showbits(a);
        printf(" (decimal %d)",a);

        // perform one's complement
        // operation
        res=~a;

        // show the formatted output
        printf("\n\t~'ed           : ");
        showbits(res);
        printf(" (decimal %d)\n\n",res);

        break;
      case 2:
        printf("\n\nEnter two decimal number: ");
        scanf("%d",&a);
        scanf("%d",&b);

        printf("\n\n\tEntered Number 1: ");
        showbits(a);
        printf(" (decimal %d)",a);
        printf("\n\tEntered Number 2: ");
        showbits(b);
        printf(" (decimal %d)",b);

        // perform XOR on two
        // variables a and b
        res=a^b;

        printf("\n\tXOR'ed          : ");
        showbits(res);
        printf(" (decimal %d)\n\n",res);

        break;
      }
    }
  }

Test Run:

   Main Menu
   ---------
   1. Perform One's Complement Operation
   2. Perform XOR Operation
   3. Quit
   1
   Enter a decimal number: 37
   Entered Number: 0000000000100101 (decimal 37)
   ~'ed          : 1111111111011010 (decimal -38)
   Main Menu
   ---------
   1. Perform One's Complement Operation
   2. Perform XOR Operation
   3. Quit
   2
   Enter two decimal number: 987
   10
   Entered Number 1: 0000001111011011 (decimal 987)
   Entered Number 2: 0000000000001010 (decimal 10)
   XOR'ed          : 0000001111011001 (decimal 985)
 Main Menu
   ---------
   1. Perform One's Complement Operation
   2. Perform XOR Operation
   3. Quit
   3
   Press any key to continue...

Related Articles:

No comments:

Post a Comment

You are free to comment anything, although you can comment as 'Anonymous' it is strongly recommended that you supply your name. Thank You.

Please don't use abusive language.