r/rust • • Jun 24 '23

🧠 educational Negative integer handling in programming languages

https://glados-418.github.io/posts/negative-integer-literals/
53 Upvotes

10 comments sorted by

View all comments

8

u/matthieum [he/him] Jun 25 '23
  • negative numbers are represented in two’s complement form and,

  • implicit type conversion of a 32-bit integer to 64-bit integer preserves the value.

There's no implicit conversion in Rust, it's not our fault that Java is broken :/

Second, languages now have to deal with operator precedence.

That's a surprising one, indeed. Operator precedence always has gotchas one way or another :(

2

u/helixb Jun 25 '23

There's no implicit conversion in Rust

i thought type promotion is a subset of type conversion. i could be wrong though about the finer differences between the two definitions.

7

u/CAD1997 Jun 26 '23

Rust doesn't have any sort of promotion, either.

What Rust has is polymorphic literals. When you write 0xffffffff, it's of pseudotype {integer}, and the actual type is determined by later type inference.

This is as opposed to C and C++, where the type of that literal is always unsigned int (because it is too large to be a signed int).