Skip to content
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

Negative Integers on CBOR playground cbor.me #92

Open
jos2237 opened this issue Jun 12, 2023 · 2 comments
Open

Negative Integers on CBOR playground cbor.me #92

jos2237 opened this issue Jun 12, 2023 · 2 comments

Comments

@jos2237
Copy link

jos2237 commented Jun 12, 2023

The value 0x20 encodes the value -1. The CBOR playground shows the following:
image

I expected the Bytes view to indicate 20 # negative(1) instead.

@cabo
Copy link
Contributor

cabo commented Jun 12, 2023

Thank you for this observation.

20 hex is a negative integer with the argument 0.
The rule for negative integers gives it the value ~0 or -1.

Unfortunately there is no good place to add the explanation that the number in parentheses always is the argument.
So negative(0) really is -1!

@chrysn
Copy link

chrysn commented Jun 12, 2023

That's correct in a sense: negative numbers are encoded with an offset of 1 to avoid having a +0 and a -0. The text to the right of the hex is very much a debug tool, showing the "additional information". It is consistent across all major types (eg. {1:1,2:2} gives A2 # map(2), or false gives F4 # primitive(20)), and always to be interpreted in the context of the major type (for example, the dictionary contains 4 CBOR items, but the additional information is 2 because those 4 CBOR items come in pairs).

One might argue that it'd be helpful to have an interpretation next to it (eg. negative(0) = -1), but then again, that'd be already the full diagnostic format already shown on the left.

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