C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - Bitwise Operators

C Programming

In this tutorial we will learn about bitwise operators in C programming language.

Following are the bitwise operators that we can use in C to work with bits.

OperatorDescription
&Bitwise AND
|Bitwise OR
^Bitwise exclusive OR i.e. XOR
<< Shift left
>> Shift right

Don't apply bitwise operator to float and double.

Bitwise AND

Bitwise AND & will give 1 only if both the operands are 1 otherwise, 0.

The truth table for bitwise AND.

ABA & B
000
010
100
111

In the following example we have two integer values 1 and 2 and we will perform bitwise AND operation and display the result.

#include <stdio.h>
int main(void)
{
  //declare integer and assign value
  int
    x = 1,
    y = 2;

  //bitwise AND
  int result = x & y;
  
  printf("%d & %d = %d\n", x, y, result);
  
  return 0;
}

Output

1 & 2 = 0

Calculation of the bitwise AND operation for the above code.

  1 (decimal) = 0000 0001 (binary)
& 2 (decimal) = 0000 0010 (binary)
-----------------------------------
  0 (decimal) = 0000 0000 (binary)

Click here to learn how to convert decimal to binary.

Bitwise OR

Bitwise OR | will give 0 only if both the operands are 0 otherwise, 1.

The truth table for bitwise OR.

ABA | B
000
011
101
111

In the following example we have two integer values 1 and 2 and we will perform bitwise OR operation and display the result.

#include <stdio.h>
int main(void)
{
  //declare integer and assign value
  int
    x = 1,
    y = 2;

  //bitwise OR
  int result = x | y;
  
  printf("%d | %d = %d\n", x, y, result);
  
  return 0;
}

Output

1 | 2 = 3

Calculation of the bitwise OR operation for the above code.

  1 (decimal) = 0000 0001 (binary)
| 2 (decimal) = 0000 0010 (binary)
-----------------------------------
  3 (decimal) = 0000 0011 (binary)

Bitwise XOR

Bitwise XOR ^ will give 1 for odd number of 1s otherwise, 0.

The truth table for bitwise XOR.

ABA ^ B
000
011
101
110

In the following example we have two integer values 2 and 7 and we will perform bitwise XOR operation and display the result.

#include <stdio.h>
int main(void)
{
  //declare integer and assign value
  int
    x = 2,
    y = 7;

  //bitwise XOR
  int result = x ^ y;
  
  printf("%d ^ %d = %d\n", x, y, result);
  
  return 0;
}

Output

2 ^ 7 = 5

Calculation of the bitwise XOR operation for the above code.

  2 (decimal) = 0000 0010 (binary)
^ 7 (decimal) = 0000 0111 (binary)
-----------------------------------
  5 (decimal) = 0000 0101 (binary)

Shift Left

We use shift left << operator to shift the bits left.

In the following example we have an integer which we will left shift 1 position.

#include <stdio.h>
int main(void)
{
  //declare integer and assign value
  int x = 4;

  //shift left
  int result = x << 1;
  
  printf("Shift left x << 1 = %d\n", result);
  
  return 0;
}

Output

Shift left x << 1 = 8

Calculation:

4 (decimal) = 0000 0100 (binary)
----------------------------------
4 << 1 = 0000 1000 (binary)
       = 8 (decimal)

Shift Right

We use shift right >> operator to shift the bits right.

In the following example we have an integer which we will right shift 1 position.

#include <stdio.h>
int main(void)
{
  //declare integer and assign value
  int x = 4;

  //shift right
  int result = x >> 1;
  
  printf("Shift left x >> 1 = %d\n", result);
  
  return 0;
}

Output

Shift right x >> 1 = 2

Calculation:

4 (decimal) = 0000 0100 (binary)
----------------------------------
4 >> 1 = 0000 0010 (binary)
       = 2 (decimal)