This blog is under construction

Sunday, 22 April 2012

Bitwise logical operators with example

Various bitwise logical operators available in C programming language are as follows.
1. bitwise AND - conjunction
2. bitwise OR  - disjunction
3. bitwise XOR - exclusive disjunction
4. bitwise NOT - negation(one's complement)

Bitwise AND - Conjunction:
If the bits from both the operands are 1, then the resultant bit is 1.  For all others, the resultant bit is 0.
Example:
00001101 & 00111100 = 00001100  

Bitwise OR - Disjunction:
If the bits from both the operands or any one of the operand is 1, then the resultant bit is 1.  For others, the resultant bit is 0
Example:
00001101 | 00111100 = 00111101

Bitwise XOR - Exclusive disjunction:
If the bits from both the operands are 0 or 1, then the resultant bit is 0.  For all others, the resultant bit is 1.
Example:
00001101 ^ 00111100 = 00110001

Bitwise NOT - Negation:
If the input bit is 1, then the resultant bit is 0.  If the input bit is 0, then the resultant bit is 1.
Example:
~00001101 = 11110010


Operator Operation
 &  bitwise AND
 |  bitwise OR
 ^  bitwise XOR
 ~  bit inversion (one's complement)


Example C program on bitwise logical operators:
 
  #include <stdio.h>
  /* converts decimal to binary */
  void decimal2binary(int input, char data[100]) {
        int i = 0, j, k = 0, binary[100];
        while (input > 0) {
                binary[i] = input % 2;
                input = input / 2;
                i++;
        }

        for (j = i - 1; j >= 0; j--) {
               /*
                * sprintf - performs formatted output conversion
                * Here integer to string.  And we keep on appending
                * some integer(0/1) to data for each iteration.  So we
                * need to move our data pointer to write binary[j]
                * in proper position
                * /
                sprintf(data+k, "%d", binary[j]);
                k++;
        }
        data[k] = '\0';
  }

  int main() {
     int input1, input2, ch;
     char data[100];
     do {
        printf("Menu:\n");
        printf("1. Left Shift\n2. Right Shift\n");
        printf("3. Bitwise Inversion\n4. Bitwise AND\n");
        printf("5. Bitwise OR\n6. Bitwise X-OR\n");
        printf("Enter your choice:");
        scanf("%d", &ch);
        switch (ch) {
                case 1:
                        printf("Sample program for left shift operator\n");
                        printf("Enter your input:");
                        scanf("%d", &input1);
                        printf("Enter no of bits to be shifted:");
                        scanf("%d", &input2);
                        input1 = input1 << 2;
                        decimal2binary(input1, data);
                        printf("Output: %s\n", data);
                        break;
                case 2:
                        printf("Sample program for right shift operator\n");
                        printf("Enter your input:");
                        scanf("%d", &input1);
                        printf("Enter no of bits to be shifted:");
                        scanf("%d", &input2);
                        input1 = input1 >> 2;
                        decimal2binary(input1, data);
                        printf("Output: %s\n", data);
                        break;
                case 3:
                        printf("Sample program for bitwise "
                                "inversion operator\n");
                        printf("Enter your input:");
                        scanf("%d", &input1);
                        input1 = ~(input1);
                        printf("%d\n", ~(-13));
                        decimal2binary(input1, data);
                        printf("Output: %s\n", data);
                        break;

                case 4:
                        printf("Sample program for bitwise "
                                 "logical AND operator\n");
                        printf("Enter your input1:");
                        scanf("%d", &input1);
                        printf("Enter your input2:");
                        scanf("%d", &input2);
                        input1 = input1 & input2;
                        decimal2binary(input1, data);
                        printf("Output: %s\n", data);
                        break;

                case 5:
                        printf("Sample program for bitwise "
                                "logical OR operator\n");
                        printf("Enter your input1:");
                        scanf("%d", &input1);
                        printf("Enter your input2:");
                        scanf("%d", &input2);
                        input1 = input1 | input2;
                        decimal2binary(input1, data);
                        printf("Output: %s\n", data);
                        break;
                case 6:
                        printf("Sample program for bitwise "
                                "logical Exclusive OR operator\n");
                        printf("Enter your input1:");
                        scanf("%d", &input1);
                        printf("Enter your input2:");
                        scanf("%d", &input2);
                        input1 = input1 ^ input2;
                        decimal2binary(input1, data);
                        printf("Output: %s\n", data);
                        break;
                default:
                        printf("You have entered wrong option\n");
                        break;

        }
        printf("Do you want to continue(0/1):\n");
        scanf("%d", &ch);
     } while(ch == 1);
        return 0;
  }


  Output:
   jp@jp-VirtualBox:~/$ ./a.out
   Menu:
   1. Left Shift
   2. Right Shift
   3. Bitwise Inversion
   4. Bitwise AND
   5. Bitwise OR
   6. Bitwise X-OR
   Enter your choice:1
   Sample program for left shift operator
   Enter your input:100
   Enter no of bits to be shifted:2
   Output: 110010000
   Do you want to continue(0/1):
   1
   Menu:
   1. Left Shift
   2. Right Shift
   3. Bitwise Inversion
   4. Bitwise AND
   5. Bitwise OR
   6. Bitwise X-OR
   Enter your choice:2
   Sample program for right shift operator
   Enter your input:100
   Enter no of bits to be shifted:2
   Output: 11001
   Do you want to continue(0/1):
   1
   Menu:
   1. Left Shift
   2. Right Shift
   3. Bitwise Inversion
   4. Bitwise AND
   5. Bitwise OR
   6. Bitwise X-OR
   Enter your choice:3
   Sample program for bitwise inversion operator
   Enter your input:-13
   12
   Output: 1100
   Do you want to continue(0/1):
   1
   Menu:
   1. Left Shift
   2. Right Shift
   3. Bitwise Inversion
   4. Bitwise AND
   5. Bitwise OR
   6. Bitwise X-OR
   Enter your choice:4
   Sample program for bitwise logical AND operator
   Enter your input1:12
   Enter your input2:12
   Output: 1100
   Do you want to continue(0/1):
   1
   Menu:
   1. Left Shift
   2. Right Shift
   3. Bitwise Inversion
   4. Bitwise AND
   5. Bitwise OR
   6. Bitwise X-OR
   Enter your choice:5
   Sample program for bitwise logical OR operator
   Enter your input1:11
   Enter your input2:15
   Output: 1111
   Do you want to continue(0/1):
   1
   Menu:
   1. Left Shift
   2. Right Shift
   3. Bitwise Inversion
   4. Bitwise AND
   5. Bitwise OR
   6. Bitwise X-OR
   Enter your choice:6
   Sample program for bitwise logical Exclusive OR operator
   Enter your input1:13
   Enter your input2:6
   Output: 1011
   Do you want to continue(0/1):
   0


No comments:

Post a Comment