Bitwise Operators Example Program in Dart


This Dart code is demonstrating the use of bitwise operators to perform bitwise operations on two variables. Here's what's happening in this code:

  • var a, b;: This line declares two variables a and b.
  • a = 2; //0010: This line initializes variable a with the value 2, which is 0010 in binary.
  • b = 25; //0011: This line initializes variable b with the value 25, which is 0011 in binary.
  • print('Bitwise & : ${a&b}'); : This line performs the bitwise AND operation between a and b using the & operator, which returns 0000 in binary (0 in decimal). The result is printed to the console.
  • print('Bitwise | : ${a|b}'); : This line performs the bitwise OR operation between a and b using the | operator, which returns 0011 in binary (3 in decimal). The result is printed to the console.
  • print('Bitwise ^ : ${a^b}'); : This line performs the bitwise XOR operation between a and b using the ^ operator, which returns 0011 in binary (3 in decimal). The result is printed to the console.
  • print('Bitwise Not : ${~b}'); : This line performs the bitwise NOT operation on b using the ~ operator, which returns -26 in decimal. In binary, this is equivalent to the one's complement of b, where all the bits are flipped (i.e., 1100). The result is printed to the console.

Source Code

void main() {
   var a,b;
   a=2; //0010
   b=25; //0011
   print('Bitwise & : ${a&b}');
   print('Bitwise | : ${a|b}');
   print('Bitwise ^ : ${a^b}');
   print('Bitwise Not : ${~b}');
}  
To download raw file Click Here

Output

Bitwise & : 0
Bitwise | : 27
Bitwise ^ : 27
Bitwise Not : -26