Dart Bitwise and Shift Operators
The Bitwise operators are the operators that perform different operations bit by bit on the value of the two operands.
List of Bitwise Operators in Dart
Sr. |
Operators |
Description |
1. |
& ( Binary AND ) |
It returns 1 if and only if both
bits are 1, and returns 0 if either one of them is 0. |
2. |
| ( Binary OR ) |
It returns 1 if any of the bits is
1, and returns 0 if and only if both bits are 0. |
3. |
^ ( Binary XOR ) |
It returns 1 if both bits are
different, and returns 0 if both bits are same. |
4. |
~ ( 1’s Complement
) |
It returns the reverse of the bit.
If the bit is 0 then the complement will be 1. |
5. |
<< ( Shift left ) |
The value of the left operand shifts
to the left by the number of bits present in the right of the operator. |
6. |
>> ( Shift right ) |
The value of the right operand shifts
to the right by the number of bits present in the left of the operator. |
Program
void main( )
{
print( " Bitwise Operators \n " ) ;
var a = 50 ;
var b = 40 ;
var c = 0 ;
// Bitwise AND Operator
var res1 = a & b ;
print( " a & b = " ) ;
print( res1 ) ;
// Bitwise OR Operator
var res2 = a | b ;
print( " \n a | b = " ) ;
print( res2 ) ;
// Bitwise XOR
var res3 = a ^ b ;
print( " \n a ^ b = " ) ;
print( res3 ) ;
// Complement Operator
var res4 = ~a ;
print( " \n ~a = " ) ;
print( res4 ) ;
// Binary left shift Operator
var res5 = a << 2 ;
print( " \n c << 1 = " ) ;
print( res5 ) ;
// Binary right shift Operator
var res6 = a >> 2 ;
print( " \n c >> 1 = " ) ;
print( res6 ) ;
}
Output
Bitwise Operators
a & b =
32
a | b =
58
a ^ b =
26
~a =
4294967245
c << 1 =
200
c >> 1 =
12