Casting from int to uint and back
Is casting a negative int to a uint and then back to a int guaranteed to give back the original negative value on all platforms, architectures and runtimes? Thanks!
8 Replies
Angius
REPL Result: Failure
Exception: CompilationErrorException
Compile: 190.472ms | Execution: 0.000ms | React with ❌ to remove this embed.
You can't cast it
@ZZZZZZZZZZZZZZZZZZZZZZZZZ that's only an issue for literals pretty sure
uh modix?
you showed the result for a split second?
I saw that too lol
https://sharplab.io/#v2:C4LgTgrgdgNAJiA1AHwAICYAMBYAUBgRjzwEspgACEigXgoFoCB2AbjwjMolooAoPyAShJtcnKuh69OgiKLyoCATmmDRilRDULl09GqA @ZZZZZZZZZZZZZZZZZZZZZZZZZ @ikalou.
SharpLab
C#/VB/F# compiler playground.
in any case, in an
unchecked
context (the default) it's just a reinterpret cast
so the underlying bits remain the samejcotton42
REPL Result: Success
Console Output
Compile: 356.110ms | Execution: 68.280ms | React with ❌ to remove this embed.
Thanks everyone! Yes I tested it on my machine but I wanted to make sure I could rely on it the result wasn't undefined / an implementation detail.