Search⌘ K
AI Features

Bitwise and Shift Operators

Explore how to apply bitwise and shift operators in Dart to process integers at the binary level. Understand binary representations, evaluate logical operators like AND, OR, XOR, and learn how shifts affect values. Discover the reasoning behind negative results from the unary complement operator and how Dart handles signed integers using two's complement.

Bitwise and shift operators perform operations on the individual bits of integer types. Computers store numbers in binary form (using 1s and 0s). We usually see our operands and results displayed in decimal format, but these operators process the underlying binary data.

Types of bitwise and shift operators

Below is a list of the bitwise operators supported by Dart.

Operator

Name

Use

&

Bitwise AND

Evaluates to 1 if the corresponding bit in both operands is 1, otherwise evaluates to 0.

|

Bitwise OR

Evaluates to 1 if the corresponding bit in at least one operand is 1, otherwise evaluates to 0.

^

Bitwise XOR

Evaluates to 1 if the corresponding bit in exactly one operand is 1, otherwise evaluates to 0.

~

Unary Bitwise Complement

Inverts the bits. Bits that are 0 become 1, and bits that are 1 become 0.

Below is a list of the shift operators supported by Dart.

<<

Shift Left

Shifts all the bits of its operand to the left by the specified amount.

>>

Shift Right

Shifts all the bits of its operand to the right by the specified amount.

Viewing binary representation

Because Dart prints integers in decimal by default, it can be difficult to visualize how bitwise operators work. We can use the toRadixString method and pass it a base of 2 to see the actual binary representation of our ...