-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Assignment of hexadecimal and integer types is not interchangeable. #108269
Comments
Currently the way to do this is const VALUE: i32 = 0x80000000_u32 as i32; |
I'm aware of that solution, but still, I can hard agree with the conversion. I mean, the binary representation of |
No,
works just fine, I suspect the confusion is because |
That was brought up in an issue I linked earlier (#53628). |
I totally agree if In my view, this is a matter of what context is chosen for interpretation here. In a signed integer, a 32-bit signed integer to be more specific, is in the context of two's complement representation, and the representation:
Expands to
In the context of 32-bit two's complement, the left-most bit is the sign bit denotes this number is negative, hence should be at least interpreted to a negative number. As of this particular value, which does not have its two's complement, the value of My suggestion is we could have developed a method like |
I disagree strongly, because then 256i8 won't mean the numerical value of what someone might think it does. I also do not want to special case things between hexadecimal (I think it was a mistake that Rust "{:x}" formats signed integers as unsigned in the first place, I didn't know that until now, also I personally use hexadecimal for numerical values anyway sometimes). It takes little code and zero cost to cast between signed and unsigned. |
Well, the help suggests using I put some deeper thoughts about the lint itself in #99195 (comment) |
In code:
It will tell me that
i32::MIN
is0X80000000
, but if I try to assign ani32
with0X80000000
, I would get an error saysliteral out of range
.I suggest to add methods to support assignment of integer types with exact bits, like I can assign
i32
with the exact 32 bits in0X80000000
.The text was updated successfully, but these errors were encountered: