AgentSkillsCN

how-to-do-bitwise-operations-in-dart

深入研究 Dart 的位运算操作,涵盖整数与布尔值的 AND、OR(包括按位与和按位或)、NAND、NOR 以及 XNOR 等运算,并附上实用的代码示例。

SKILL.md
--- frontmatter
name: how-to-do-bitwise-operations-in-dart
description: Explore Dart's bitwise operations for both integers and booleans, including AND, OR (inclusive & exclusive), NAND, NOR, and XNOR, with practical code examples.
metadata:
  url: https://rodydavis.com/posts/dart/bitwise
  last_modified: Tue, 03 Feb 2026 20:04:33 GMT

How to do Bitwise operations in Dart

In Dart it is possible to do Bitwise Operations with int and bool types.

AND 

Checks if the left and right side are both true. Learn more.

code
// int
print(0 & 1); // 0
print(1 & 0); // 0
print(1 & 1); // 1
print(0 & 0); // 0

// bool
print(false & true); // false
print(true & false); // false
print(true & true); // true
print(false & false); // false

OR 

Inclusive 

Checks if either the left or right side are true. Learn more.

code
// int
print(0 | 1); // 1
print(1 | 0); // 1
print(1 | 1); // 1
print(0 | 0); // 0

// bool
print(false | true); // true
print(true | false); // true
print(true | true); // true
print(false | false); // false

Exclusive 

Checks if both the left or right side are true but not both. Learn more.

code
// int
print(0 ^ 1); // 1
print(1 ^ 0); // 1
print(1 ^ 1); // 0
print(0 ^ 0); // 0

// bool
print(false ^ true); // true
print(true ^ false); // true
print(true ^ true); // false
print(false ^ false); // false

NAND 

Negated AND operation.

code
// int
print(~(0 & 1) & 1); // 1
print(~(1 & 0) & 1); // 1
print(~(1 & 1) & 1); // 0
print(~(0 & 0) & 1); // 1

// bool
print(!(false & true)); // true
print(!(true & false)); // true
print(!(true & true)); // false
print(!(false & false)); // true

NOR 

Negated inclusive OR operation.

code
// int
print(~(0 | 1) & 1); // 0
print(~(1 | 0) & 1); // 0
print(~(1 | 1) & 1); // 0
print(~(0 | 0) & 1); // 1

// bool
print(!(false | true)); // false
print(!(true | false)); // false
print(!(true | true)); // false
print(!(false | false)); // true

XNOR 

Negated exclusive OR operation.

code
// int
print(~(0 ^ 1) & 1); // 0
print(~(1 ^ 0) & 1); // 0
print(~(1 ^ 1) & 1); // 1
print(~(0 ^ 0) & 1); // 1

// bool
print(!(false ^ true)); // false
print(!(true ^ false)); // false
print(!(true ^ true)); // true
print(!(false ^ false)); // true