quick-protobuf icon indicating copy to clipboard operation
quick-protobuf copied to clipboard

negative int32 sign-extended too much?

Open sollyucko opened this issue 3 years ago • 0 comments

-...i32 as u64 results in sign-extending all the way to 64 bits, which I assume is incorrect according to the protobuf spec. Changing it to as u32 as u64 should sign-extend to 32 bits and then zero-extend from there to 64 bits, resulting in what I assume is the correct behavior.

Demo: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=dc9845eb3a846f6658029234ab801478

fn main() {
    println!("{}", (-1i32) as u64);
    println!("{}", (-1i32) as u32 as u64);
}
18446744073709551615
4294967295

sollyucko avatar Jun 27 '22 18:06 sollyucko