Bitwise Operators


Unlike many other languages, C/C++ supports a full complement of bitwise operators. Since C was designed to take the place of assembly language for most programming tasks, it needed to able to support many operations that can be done in assembler, including operations on bits. Bitwise operation refers to testing, setting, or shifting the actual bits in a byte or word, which correspond to the char and int data types and variants. You cannot use bitwise operation on the float, double, long double, void, bool, or other, more complex types.

Bitwise operations most often find applications in device drivers - such as modem programs, disk file routines, and printer routines - because the bitwise operations can be used to mask off certain bits, such as parity.

Bitwise operators

Operator Meaning
& AND
| OR
^ Exclusive OR(XOR)
~ One's complement(NOT)
>> Shift right
<< Shift left

Example of bitwise operator

Program
//Bitwise operator
#include <stdio.h>

int main() {
  int a = 15,b = 30;
  printf("Output = %d",a&b);
  return 0;
}

15 = 01111(binary form)
30 = 11110(binary form)
 
  01111
& 11110
............
  01110 = 14