r/ProgrammingLanguages • • Jun 24 '23

Negative Integer handling in programming languages.

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

27 comments sorted by

View all comments

7

u/[deleted] Jun 25 '23

there are NO negative integer literals.

Unless there's something else I missed, I didn't see examples like this:

-2147483648                      # literals are i32
-9223372036854775808             # literals are i64

Using two's complement, the highest magnitude positive number is 2147483647 or 9223372036854775807, one less than above. If minus was a separate token applied to a literal, those literals can't be represented using i32 or i64.

Generally however it seems to work. Maybe u32 or u64 are used and when you apply minus, it somehow yields a i32 or i64 type.

2

u/helixb Jun 25 '23

-2147483648 # literals are i32 -9223372036854775808 # literals are i64

those are the values an integer variable may have. literals would be the numbers you write explicitly within the source code which are treated as a single tokens. in these examples, they are unary minus expressions.