+, -, *,
/, and % (remainder). Integer division truncates toward zero
in all four languages.
| Language | Debug build | Release / production |
|---|---|---|
| Rust | panic on overflow | wrapping (two’s complement) |
| C++ (signed) | undefined behavior | undefined behavior |
| C++ (unsigned) | modular wrap | modular wrap |
| C# | silent wrap (checked context: exception) | silent wrap |
| Python | arbitrary precision; no overflow | arbitrary precision; no overflow |
wrapping_add, saturating_add, checked_add)
when the default panic is not desired.
==, !=,
<, >, <=, >=.
NaN != NaN and
comparisons involving NaN always return false. Rust encodes this in the
type system: f64 implements PartialOrd but not Ord,
preventing floats from being used as sort keys without explicit handling.
==) from reference identity (object.ReferenceEquals /
is). Rust and C++ have only value equality for scalar types; reference
equality requires explicit pointer comparison.
&& (and), || (or), ! (not) operate on
boolean values in all four languages.
&& does not evaluate
its right operand if the left is false; || does not evaluate
its right operand if the left is true. This is guaranteed by the language
standard in all four languages and is commonly used to guard unsafe operations:
and, or, not
as operator alternatives. Unlike other languages, Python’s or and
and return one of their operands, not a strict boolean.
| Operator | Meaning | Example |
|---|---|---|
& |
AND | 0b1100 & 0b1010 == 0b1000 |
| |
OR | 0b1100 | 0b1010 == 0b1110 |
^ |
XOR | 0b1100 ^ 0b1010 == 0b0110 |
! / ~ |
NOT (bitwise complement) | !0u8 == 255 |
<< |
left shift | 1 << 3 == 8 |
>> |
right shift | 16 >> 2 == 4 |
! for bitwise NOT on integers (same token as logical NOT).
C++ and C# use ~. Shifting by more bits than the type width is undefined
behavior in C++ for signed types; Rust panics in debug and wraps in release.
= binds a value to a mutable variable. Compound assignment operators
combine an operation with assignment: +=, -=,
*=, /=, %=, &=,
|=, ^=, <<=, >>=.
Copy. In C++, assignment invokes the copy or move assignment operator.
In C# and Python, assignment copies a reference for reference types; Python integers
and strings are immutable so rebinding is effectively copying.
()), preventing
the classic C bug of writing = where == was intended inside
a condition.
&& and ||
in a way that preserves short-circuit semantics.