How to use Bitwise operator

Description

Bitwise operators operate on the bits in integer values. There are six bitwise operators,as shown in the following table.

Bitwise operator

There are six bit operators:

Operator Description
& Bitwise AND operator
| Bitwise OR operator
^ Bitwise Exclusive OR (EOR) operator
~ Bitwise NOT operator
>> Bitwise shift right operator
<<Bitwise shift left operator

All of these only operate on integer types.

The ~ operator is a unary operator, and it applies to one operand. The others are binary operators.

AND

The bitwise AND operator, &, combines the corresponding bits of its operands:

if both bits are 1, the resulting bit is 1; otherwise, the resulting bit is 0.

Suppose you declare the following variables:


int x = 13;
int y = 6;
int z = x&y; /* AND the bits of x and y */

z is assigned to 4 (binary 100).


x         0       0       0       0       1       1      0       1 
y         0       0       0       0       0       1      1       0 
x & y     0       0       0       0       0       1      0       0 

# include<stdio.h>
/* w  w w  .j a v a 2 s  .  c o  m*/
main()
{
  char c1 = 4,c2 = 6,c3 = 3;

  c3 = c1 & c2;
  printf("\n Bitwise AND i.e. c1 & c2 = %d",c3);
}

The code above generates the following result.

Suppose c1 = 4, c2 = 6;


The value of c1 & c2 is interpreted:
//ww  w. j a v a 2s.c o m
    0000 0100
  & 0000 0110
   ----------
    0000 0100

OR

The bitwise OR operator, |, results in 1 if either or both of the corresponding bits are 1; otherwise, the result is 0.


int x = 13;
int y = 6;
int z = x|y;                 /* OR the bits of x and y */ 

the result would be as follows:


x       0       0       0       0       1       1      0       1 
y       0       0       0       0       0       1      1       0 
x|y     0       0       0       0       1       1      1       1 

The value stored in z would therefore be 15 (binary 1111).


#include<stdio.h>
/* www  .  java 2s  .c  o  m*/
main()
{
  char c1 = 4,c2 =6 ,c3 = 3;

  c3 = c1 | c2;
  printf("\n Bitwise OR i.e. c1 | c2 = %c",c3);
}

The code above generates the following result.

Suppose c1 = 4, c2 = 6; The value of c1 | c2 is interpreted as follows:


    0000 0100
  | 0000 0110
    -----------
    0000 0110

Exclusive OR

The bitwise EOR operator, ^, produces a 1 if both bits are different, and 0 if they're the same.


int x = 13;
int y = 6;
int z = x^y;                 /*Exclusive OR the bits of x and y */ 

x       0       0       0       0       1       1      0       1 
y       0       0       0       0       0       1      1       0 
x^y     0       0       0       0       1       0      1       1 

z is assigned to the value 11 (binary 1011).


# include<stdio.h>
/*  w  w w  .  j  av a  2  s  .  com*/
main()
{
  char c1 = 4,c2 = 6,c3 = 3;

  c3 = c1 ^ c2;
  printf("\n Bitwise XOR i.e. c1 ^ c2 = %c",c3);
}

Suppose c1 = 4, c2 = 6;

The value of c1 ^ c2 is interpreted as follows:


  0000 0100
^ 0000 0110
----------
  0000 0010

NOT

The unary operator, ~, flips the bits of its operand, so 1 becomes 0, and 0 becomes 1.


int x = 13;
int z = ~x;                            /* Store 1's complement of x */ 

then z will have the value 14. The bits are set as follows:


x       0       0       0       0       1       1      0       1 
~x      1       1       1       1       0       0      1       0 

#include<stdio.h>
/*www.  j  a va  2s .  c om*/
main()
{
  char c1 = 4,c2 = 6,c3 = 3;

  c3 = ~c1;
  printf("\n ones complement of c1 = %c",c3);
}

Suppose c1 = 4, c2 = 6. The value of ~ c1 is interpreted as follows:


~  0000 0100
----------
   1111 1011

Left Shift

The shift operators (<<) shift the bits by the number of positions. You could specify a shift-left operation with the following statements:


int value = 12; 
int shiftcount = 3;                    /* Number of positions to be shifted */ 
int result = value << shiftcount;      

The variable result will contain the value 96. The binary number in value is 00001100. The bits are shifted to the left three positions, and 0s are introduced on the right, so the value of value << shiftcount, as a binary number, will be 01100000.


#include<stdio.h>
/*  w ww . j  a  v  a  2s .c  o m*/
main()
{
  char c1 = 1,c2 = 2,c3 = 3;

  c3 = c1<<2;
  printf("\n left shift by 2 bits c1 << 2 = %c",c3);
}

The bits are shifted left by two places. c1 is 0000 0100 It is shifted 2 bits to the left 0001 00**

While shifting, the high-order (left) bits are discarded. The vacuum on the right side is filled with 0s.

Shift Right

The right shift operator moves the bits to the right. For unsigned values, the bits that are introduced on the left in the vacated positions are filled with zeros.

For example,


unsigned int value = 65372U; 
unsigned int result = value >> 2;      /* Shift right two bits */ 

Suppose you now execute the following statement:


1111    1111    0101    1100 (shift 2 to the right)
0011    1111    1101    0111 

The bits in value is shifted two places to the right, introducing zeros at the left end.

For signed values that are negative, where the leftmost bit will be 1, the result depends on your system. In most cases, the sign bit is propagated, so the bits introduced on the right are 1 bits, but on some systems zeros are introduced in this case too.


#include<stdio.h>
//from   ww w . j ava 2  s  . c om
main()
{
  char c1 = 1,c2 = 2,c3 = 3;

  c3 = c1>>2;
  printf("\n right shift by 2 bits c1 >> 2 = %c",c3);
}

A special feature of the >> and <<: perform super fast binary division and multiplication.


#include <stdio.h>
int main(){/* w  ww  .  j  a  va 2s  . c o m*/

   int x, y = 6;

   x = y >> 1;
  
   printf("%d",x);

   x = y/2;
  
   printf("%d",x);
}

To divide value by 4


#include <stdio.h>
int main(){//from w w w .j  av  a  2 s  . c  om

   int x, y = 8;

   x = y >> 2;
  
   printf("%d",x);

   x = y/4;
  
   printf("%d",x);
}




















Home »
  C Language »
    Language Basics »




C Language Basics
Data Types
Operator
Statement
Array