Bitwise operators in Dart Programming

Dart ProgrammingServer Side ProgrammingProgramming

Bitwise operators are operators that are used to perform bit-level operations on operands. For example, consider two variables x and y where the values stored in them are 20 and 5 respectively.

The binary representation of both these numbers will look something like this −

x = 10100
y = 00101

We make use of all the bitwise operators in Dart to perform on the values that are shown in the above table (bit values).

In the below table all the bitwise operators that are present in Dart are mentioned.

Consider the table as a reference.

OperatorMeaningExampleDescription
&Binary AND( x & y )Will produce 00100
|Binary OR( x | y )Will produce 10101
^Binary XOR( x ^ y )Will produce 10001
~One's compliment~ xWill produce 01011
<<Left Shiftx << 2Will produce 1010000
>>Right Shifty >> 2Will produce 1

Let's make use of all the above mentioned bitwise operators in a dart program.

Example

Consider the example shown below −

 Live Demo

void main(){
   var x = 20, y = 5;
   print("x & y = ${x & y}");
   print("x | y = ${x | y}");
   print("x ^ y = ${x ^ y}");
   print("~x = ${(~x)}");
   print("x << 2 = ${x << 2}");
   print("y >> 2 = ${y >> 2}");
}

Output

x & y = 4
x | y = 21
x ^ y = 17
~x = -21
x << 2 = 80
y >> 2 = 1
raja
Published on 21-May-2021 12:17:41
Advertisements