Bitwise operator



                          Bitwise operator

The c# bitwise operators I shall be covering here and are of particular interest to me in my followup post are:
  1. Bitwise AND( & )
  2. Bitwise OR( | )
  3. Bitwise NOT ( ! )
  4. Bitwise XOR (^)
  5. Bitwise Shift operator

Bitwise AND( & ):- And operators are predefined for the integral types and bool. For integral types, & computes the logical bitwise AND of its operands. For bool operands, & computes the logical AND of its operands; that is, the result is true if and only if both its operands are true.
ABA & B
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

 

Bitwise OR( | ):-OR operators are predefined for the integral types and bool. For integral types, | computes the bitwise OR of its operands. For bool operands, | computes the logical OR of its operands; that is, the result is false if and only if both its operands are false.


ABA | B
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

Bitwise NOT(!):-NOT operator are predefined for the integral type and bool.For integral types,! computes the bitwise ! of its operands .For bool operands,! computes the logical ! of its operands.this converts false statement in true and  converts true  statement in false.
ABA & B
TrueFalseTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

Example:-
 int a=2;

if(a!=2) //In this a=2 but by using ! it became false statement

{  

        //statements


}

if(a!=0) //In this a is not equal to 0 but by using ! it became true statement

{  

        //statements



}

Bitwise XOR:-The bitwise XOR operator is indicated by a caret ( ^ ) and, of course, works directly on the binary form of numbers. Bitwise XOR is different from bitwise OR in that it returns 1 only when exactly one bit has a value of 1. Here is the truth table:
 
 ABA^B
FalseFalseTrue
FalseTrueFalse
TrueFalseFalse
TrueTrueTrue

Bitwise Shift operator :-

The bitwise shift operators move the bit values of a binary object. The left operand specifies the value to be shifted. The right operand specifies the number of positions that the bits in the value are to be shifted. The result is not an lvalue. Both operands have the same precedence and are left-to-right associative.


Operator Usage
<< Indicates the bits are to be shifted to the left.
>> Indicates the bits are to be shifted to the right.

Each operand must have an integral or enumeration type. The compiler performs integral promotions on the operands, and then the right operand is converted to type int. The result has the same type as the left operand (after the arithmetic conversions).


OperatorDescriptionExample
&Binary AND Operator copies a bit to the result if it exists in both operands. (A & B) will give 12 which is 0000 1100
|Binary OR Operator copies a bit if it exists in eather operand. (A | B) will give 61 which is 0011 1101
^Binary XOR Operator copies the bit if it is set in one operand but not both. (A ^ B) will give 49 which is 0011 0001
~Binary Ones Complement Operator is unary and has the efect of 'flipping' bits. (~A ) will give -60 which is 1100 0011
<<Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. A << 2 will give 240 which is 1111 0000
>>Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. A >> 2 will give 15 which is 0000 1111

Comments

Popular posts from this blog