Skip to content

######why are negative numbers of type int32 encoded using 64-bit varints? Instead of using 32-bit varint encoding #10521

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

Closed
luoluoyuyu opened this issue Sep 8, 2022 · 3 comments
Assignees

Comments

@luoluoyuyu
Copy link

luoluoyuyu commented Sep 8, 2022

Why are negative numbers of type int32 encoded using 64-bit varints? Instead of using 32-bit varint encoding

@luoluoyuyu luoluoyuyu added the untriaged auto added to all issues by default when created. label Sep 8, 2022
@deannagarcia
Copy link
Member

Can you add a bit more detail here? What language/architecture are you using? Can you provide an example/something reproducible so we can run it?

@deannagarcia deannagarcia self-assigned this Sep 8, 2022
@deannagarcia deannagarcia removed the untriaged auto added to all issues by default when created. label Sep 8, 2022
@boscosiu
Copy link
Contributor

boscosiu commented Sep 9, 2022

Why are negative numbers of type int32 encoded using 64-bit varints? Instead of using 32-bit varint encoding

The designers wanted the 32-bit varints to be forward compatible with their 64-bit counterparts.

From https://developers.google.com/protocol-buffers/docs/proto3#updating

int32, uint32, int64, uint64, and bool are all compatible – this means you can change a field from one of these types to another without breaking forwards- or backwards-compatibility. If a number is parsed from the wire which doesn't fit in the corresponding type, you will get the same effect as if you had cast the number to that type in C++ (for example, if a 64-bit number is read as an int32, it will be truncated to 32 bits).

and

sint32 and sint64 are compatible with each other but are not compatible with the other integer types.

@luoluoyuyu
Copy link
Author

thank

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants