C

Examples Of Bitwise Operators In C

Bitwise operators carry out operations at the bit level. A bitwise & (AND) operator on two numbers x and y, for example, would convert these numbers to their binary equivalents before performing the logical AND operation on them.

1. The bitwise AND (AND) operator

The resultant bit in the Bitwise & operation is 1 if the corresponding bits in both operands are 1. If any of the operand’s corresponding bit is 0, the output bit is 0.
As an example: The output of (101 & 111) is 101 because the first bit in both operands is 1 and thus the first output bit is 1.
Because the second bit in the first operand is 0, the output bit is 0.
Because the third bit in both operands is 1, the third output bit is 1.
Let’s look at another example and write a C program to test the output:

12  = 00001100 (In Binary)
18  = 00010010 (In Binary)

Bitwise AND Operation of numbers 12 and 18 is:
  00001100
& 00010010
  ________
  00000000  = 0 (In decimal)

C Program:

#include <stdio.h>
int main ()
{
  int num1 = 12, num2 = 18;

  //Bitwise & Operation: num1 & num2
  printf("Output of num1 & num2: %d", (num1 & num2));

  return 0;
}

Output:

Output of num1 & num2: 0

2. The bitwise (OR) operator

The resultant bit in the Bitwise | (OR) operation is 0 if the corresponding bits in both operands are 0. If any of the operands’ corresponding bits is 1, the output bit is 1.
As an example: The result of (101 & 111) is 111 because:
Because the first bit in both operands is 1, the first output bit is 1.
Because the second operand’s second bit is 1, the output bit is 1.
Because the third bit in both operands is 1, the third output bit is 1.
Let’s look at another example of an OR operator and write a C program for it:

12  = 00001100 (In Binary)
18  = 00010010 (In Binary)

Bitwise OR Operation of numbers 12 and 18 is:
  00001100
| 00010010
  ________
  00011110  = 30 (In decimal)

C Program:

#include <stdio.h>
int main ()
{
  int num1 = 12, num2 = 18;

  //Bitwise | (OR) Operation: num1 | num2
  printf("Output of num1 | num2: %d", (num1 | num2));
 
  return 0;
}

Output:

Output of num1 | num2: 30

3. The bitwise (XOR) operator

Exclusive OR is another name for the bitwise (XOR) operator. Bitwise XOR returns 1 if the corresponding bits of two operands are opposite, and 0 if the corresponding bits are the same.
As an example: The output of (101 & 111) is 010 because the first bit in both operands is the same, so the first output bit is 0.
Because the second bit in both operands differs, the output bit is 1.
Because the third bit in both operands is the same, the third output bit is 0.
Let’s look at another operator example and write a C program for it:

12  = 00001100 (In Binary)
18  = 00010010 (In Binary)

Bitwise XOR Operation of numbers 12 and 18 is:
  00001100
^ 00010010
  ________
  00011110  = 30 (In decimal)

C Program:

#include <stdio.h>
int main ()
{
  int num1 = 12, num2 = 18;

  //Bitwise ^ (XOR) Operation: num1 ^ num2
  printf("Output of num1 ^ num2: %d", (num1 ^ num2));

  return 0;
}

Output:

Output of num1 ^ num2: 30

4. (Complement) bitwise operator

A unary operator is bitwise complement. It converts 1 bits to 0 bits and 0 bits to 1 bits. It is represented by the (tilde) symbol.
Let’s look at how to find a number’s bitwise complement:
32 = 00100000 (In hexadecimal)
Number 32’s bitwise complement(): According to the complement rules, 1s become 0s and 0s become 1s.
~ 00100000 \s \s11011111 = 223 (In decimal) (In decimal)
The compiler, however, prints the 2’s complement of this number.

2's complement of 223 = 1's complement of 223 + 1
= 1's of (11011111) + 1
= 00100000 + 1
= 00100001
= -33 (2's complement are represented by negative sign)

C Program:

#include <stdio.h>
int main ()
{

  int num = 32;

  //printing bitwise complement of 32
  printf("Bitwise complement of 32 is: %d",(~num));

  return 0;
}

Output:

Bitwise complement of 32 is: -33

5. Operators for bitwise shift

There are two kinds of bitwise shift operators:

  • Right Shift Operator >>
  • Left Shift Operator >>

Left Shift: The left shift operator is represented by the symbol. It shifts all bits to the left by a specified number of bits, for example, num2 shifts the bits to the left by two positions. The bit positions vacated by the left shift operator are filled with zeros.

27     = 00011011 (In binary)
27<<2  = 01101100 (In binary) [Left shift by two bits]
       = 108 (In decimal)

Right Shift: The right shift operator is represented by the >> symbol. It shifts all bits to the right by the specified number of bits. For example, num>>2 will shift the bits two positions to the right. The bit positions vacated by the right shift operator are filled with zeros.

27    = 00011011 (In binary)
27>>2 = 00000110 (In binary) [Right shift by two bits]
      = 6 (In decimal)

C Program:

#include <stdio.h>
int main ()
{
  int number = 27;

  printf("number is: %d\n", number);

  //Bitwise left shift <<, shifting the bits to the left
  //by two positions
  printf("number << 2: %d\n", (number<<2));

  //Bitwise right shift >>, shifting the bits to the right
  //by two positions
  printf("number >> 2: %d", (number>>2));

  return 0;
}

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button