Dart Operators: Bitwise and shift

Dart Operators: Bitwise and shift

ยท

2 min read

  • Bitwise operators are used to perform bit-level operations on operands which means they performed on single bits.
Operator SymbolOperator NameOperator Description
&Bitwise ANDPerforms bitwise and operation on two operands.
Bitwise ORPerforms bitwise or operation on two operands.
^Bitwise XORPerforms bitwise XOR operation on two operands.
~Bitwise NOTPerforms bitwise NOT operation on two operands.
<<Left ShiftShifts a in binary representation to b bits to left and inserting 0 from right.
\>>Right ShiftShifts a in binary representation to b bits to left and inserting 0 from left.
void main()
{
    int a = 5;
    int b = 7;

    // Performing Bitwise AND on a and b
    var c = a & b;
    print(c); //5

    // Performing Bitwise OR on a and b
    var d = a | b;
    print(d); //7

    // Performing Bitwise XOR on a and b
    var e = a ^ b;
    print(e); //2

    // Performing Bitwise NOT on a
    var f = ~a;
    print(f); //-6

    // Performing left shift on a
    var g = a << b;
    print(g); //640

    // Performing right shift on a
    var h = a >> b;
    print(h); //0
}

Did you find this article valuable?

Support Vinit Mepani (Flutter Developer) by becoming a sponsor. Any amount is appreciated!

ย