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

Option to specify whether decimal integer fields are signed or unsigned #17

Open
dc42 opened this issue Oct 3, 2022 · 10 comments
Open

Comments

@dc42
Copy link

dc42 commented Oct 3, 2022

This is a great tool! I would like the option to specify whether a decimal value should be interpreted and displayed as a signed or an unsigned value. Currently it appears that a 16-bit decimal field is always interpreted as unsigned, whereas a 32-bit field is interpreted as signed. For example, this data:

258d2d2200000001000000000100000000ffffffffffff000000000000000000000000000000a7f38
hhhhhhhh hhhhhhhh hhhhhhhhhh hhhhhhhh 16d 32d 

displays:

258d2d22 00000001 0000000001 00000000 65535 00000000-1 00000000 00000000 0000000000 0000a7f3 08

@zuckschwerdt
Copy link
Member

zuckschwerdt commented Oct 3, 2022

Thanks. Yes we need better output formats someday. The decimal part relies on JS interpretation of rules to apply, the limit here is 52 bit for an integer then.
But getting a 2's complement for 32d really must be a bug. I'll take a look.

@zuckschwerdt
Copy link
Member

The code to rework is ("it's unsafe to output more than 32 bits in one call" -- we mostly knew this would crash and burn)

bitbench/src/bitstring.js

Lines 106 to 122 in 457c32f

// note: it's unsafe to output more than 32 bits in one call
function formatBitsChar(bits, base = 16) {
var num = 0
var pad = Math.ceil(bits.length / Math.log2(base))
while (bits.length)
num = (num << 1) + bits.shift()
if (base == 256) { // special case ascii
var inv = num >= 128
num %= 128
var ctrl = (num < 32 || num == 127)
if (ctrl)
num = (num + 64) % 128
var cls = (ctrl ? 'ctrl' : '') + (inv ? ' inv' : '')
return `<span class="${cls}">${ctrl ? '^' : ''}${String.fromCharCode(num)}</span>`
}
return num.toString(base).padStart(pad, '0')
}

@zuckschwerdt
Copy link
Member

I'll check if this can be fixed using BigInt and then introduce s as sign-decimal format specifier.

@zuckschwerdt
Copy link
Member

Numbers bigger than 31 bit are now fixed with 4df41e6 using multiplication by 2 instead of left-shift which is signed modulo 32.
Also numbers bigger than the maximum integer safe 52 bits use BigInt.

@zuckschwerdt
Copy link
Member

A signed decimal format (2's complement, only up to 32 bit width) has been added as "s" with 464fa1e
Example in this BitBench.

@zuckschwerdt
Copy link
Member

We could want signed for larger field width, and for e.g. hex.
Larger width might be possible if we apply the 2's complement ourselfs -- but probably needed step up from 32 bit is 64 bit which then also needs BigInt.
Other formats (hex) should be possible but will be a mess in the code.
Let's wait until someone really requires this :)

@dc42
Copy link
Author

dc42 commented Oct 3, 2022

Wow, that was quick! Looks good to me. Might be worth mentioning in the docs that if you want signed little endian interpretation, the < character must come between the s and the number of bits. So s<32d works but <s32d doesn't.

I don't have any need to display signed hex values, and I think that would be an unusual thing to do. OTOH, interpreting 32-bit or 64-bit strings as IEEE floating point values may be needed some day.

@zuckschwerdt
Copy link
Member

Oh, sorry, I thought about making signed a general modifier, e.g. $ but it's a format specifier right now, you'll want <32s

I'll see about e.g. 32f and 64f (also <32f and <64f).

@dc42
Copy link
Author

dc42 commented Oct 3, 2022

Ah OK, 's' replaces 'd' instead of being a modifier. That works!

@zuckschwerdt
Copy link
Member

Float is now supported, an example in this BitBench.

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

2 participants