cbor / cbor.github.io

cbor.io web site
75 stars 33 forks source link

Negative Integers on CBOR playground cbor.me #92

Open jos2237 opened 1 year ago

jos2237 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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.