All Bit Level Operations in C (Bitwise Operations)


 If you was not understand How to use the bitwise operations ?!!

NOW , I can tell you that your problem will solve after read this blog.  


  • Our  Agenda  :

1- Bitwise AND Operator denoted by "&"  .
2- Bitwise OR Operator denoted by "|"  .
3- Bitwise XOR Operator denoted by "^" . 
4- Bitwise Complement or Negation Operator denoted by "~" .
5- 
Bitwise Right Shift & Left Shift denoted by ">>" and "<<" .



  • Bitwise AND Operator '&' : 

AND Operation it's always used to clear bit , why ?! and what is the meaning of bit ?!! .
Bit is the smallest storage unit that allows you to hold on only one of two values : 1 or 0 . 

Why AND Operation used to Clear bit ? 
If you look at this two videos , you will conclude that  :
1- if you assign 0 on bit in register already have 1 , this bit will convert from 1 to 0 . (Clear bit)
2- 
if you assign  1 on bit in register already have 0 , this bit will stay 0 . (No effect on bit )
3- if you assign  1 on bit in register already have 1 , this bit will stay 1 .   (No effect on bit )
4- if you assign  0 on bit in register already have 0 , this bit will stay 0.  (No effect on bit )

This video will help you to imagine the operate of assign bits on register
 (By AND Operator)
 


And look at the second video to be sure that you understand good .  





  • Bitwise OR Operator '|' : 

OR Operation it's always used to set bit why ?! .

Why OR Operation used to Set bit ? 
If you look at this two videos , you will conclude that  :
1- if you assign 0 on bit in register already have 1 , this bit will stay 1 .   (No effect on bit )  
2- 
if you assign  1 on bit in register already have 0 ,this bit will convert from 0 to 1. (Set bit)
3- if you assign  1 on bit in register already have 1 , this bit will stay 1 .   (No effect on bit )
4- if you assign  0 on bit in register already have 0 , this bit will stay 0.  (No effect on bit )

                          This video will help you to imagine the operate of assign bits on register

   (By AND Operator)



                                And look at the second video to be sure that you understand good 




  • Bitwise XOR Operator '^' : 

XOR Operator, it's like OR  but with one difference  what is it ?! .
It's when you assign  1 on bit in register already have 1 , this bit will convert from 1 to 0. (Clear bit)



  • Bitwise Complement or Negation Operator '~' : 
Negation Operator , it's just toggle the bits . 
I mean , 
1- if bit in register have 1 , this bit will convert to 0 .  Toggle bit )
2- if bit in register have 0 ,this bit will convert 1. Toggle bit )
 

  • Bitwise Right shift '>>' and Left shift '<<' : 
Right Shift Operator , it's just Shift the bits from right and set right bits '0' . 

Left Shift Operator , it's just Shift the bits from left and set left bits '0' . 






Post a Comment

Previous Post Next Post