Skip to main content

BitWise Operators Explained in Short

Bitwise Operators :   Bitwise Operators do not operate on decimal value of the Number. They first convert the numbers into binary value & then they operate on it. Bitwise opeartors are not used with floating type variables.
  1. Bitwise AND :-   &
  2. Bitwise OR    :-   |
  3. Bitwise XOR :-   ^
  4. Bitwise NOT :-   ~
  5. LEFT SHIFT :- <<
  6. RIGHT SHIFT :- >>
  • Bitwise AND  &
  • Example :
  • N1 = 10 , N2 = 15
  • N1 & N2
  • In Binary  :
  •                                                  0 0 1 0 1 0 
  •                                                  0 0 1 1 1 1 
  •                                               ------------------
  •                                                  0 0 1 0 1 0 
  •  
  • So N1 & N2 == 15 (in decimal) 


  • Bitwise OR  |
  • Example :
  • N1 = 10 , N2 = 15
  • N1 | N2
  • In Binary  :
  •                                                  0 0 1 0 1 0 
  •                                                  0 0 1 1 1 1 
  •                                               ------------------
  •                                                  0 0 1 1 1 1
  •  
  • So N1 | N2 == 10 (in decimal)  

  • Bitwise  XOR   ^
  • Example :
  • N1 = 10 , N2 = 15
  • N1 ^ N2
  • In Binary  :
  •                                                  0 0 1 0 1 0 
  •                                                  0 0 1 1 1 1 
  •                                               ------------------
  •                                                  0 0 0 1 0 1
  •  
  • So N1 ^ N2 == 5 (in decimal)  
  • Bitwise NOT - Numbers are
  • Example :
  • N1 = 10
  • ~N1
  • In Binary  :
  •                                                1 ...  0 0 1 0 1 0
  •                                               ------------------
  •                                                1 ...  1 1 0 1 0 1
  •                                                1 ...  0 0 1 0 1 0 (Take Complement Leaving Sign Bit)
  •                                                1 ...  0 0 1 0 1 1 (Add 1)
  •  
  • So ~N1 == -11 (in decimal)  
  •  
  • Another Example :
  • N2 = 15
  • ~N2
  • In Binary  :
  •                                                1 ...  0 0 1 1 1 1
  •                                               ------------------
  •                                                1 ...  1 1 0 0 0 0
  •                                                1 ...  0 0 1 1 1 1 (Take Complement Leaving Sign Bit)
  •                                                1 ...  0 1 0 0 0 0 (Add 1)
  •  
  • So ~N1 == -16 (in decimal)  
  •  
  •  
  • Bitwise LEFT SHIFT <<
  • Example :
  • N1 = 10
  • N1<< 2
  • In Binary  :
  •                                                  0 0 1 0 1 0  (Left Shift shifts the bits to the left & fills 0)
  •                                               -----------------
  •                                                  1 0 1 0 0 0
  •  
  • So N1 <<  2 == 40 (in decimal) 
  •  
  •  
  • Bitwise RIGHT SHIFT >>
  • Example :
  • N1 = 10
  • N1>> 2
  • In Binary  :
  •                                                  0 0 1 0 1 0  (Left Shift shifts the bits to the left & fills 0)
  •                                               -----------------
  •                                                  0 0 0 0 1 0  
  •  
  • So N1 >>  2 == 2 (in decimal) 

Comments

Popular posts from this blog

Insertion Sort in C

#include<stdio.h> int main() {     int arr[]={8,5,7,4,3,0};     int n = sizeof(arr)/sizeof(int);     int key=0,i=0,j=0;     for(i=1;i<n;i++){         key=arr[i];         j=i-1;         while(j>=0 && arr[j]>key){             arr[j+1]=arr[j];             j--;         }         arr[j+1]=key;     }      for(i=0;i<n;i++){         printf("%d\t",arr[i]);     }     printf("\n");     return 0; }

Activity Selection Problem in Short

Activity Selection Problem is used extensively. Suppose a person is given n activities with their start & finish time. Select the maximum number of activities that can be performed by a single person, assuming that a person can only work on a single activity at a time. Greedy Approach:  set n equal to the max finish time  initialize set of selected activity with a 1    set k=1 loop m=2 to n    if start time of current activity is greater than the finish time of last activity            add the current activity to selected activity           set k=m