Add Big Endian for getbits() and setbits() #537
Closed
dzid26 wants to merge 3 commits into
Closed
Conversation
re-solidified getbits setbits - proto vargs hints BE_VA_METHOD (2) now - spaces got replaced with tabs
Contributor
|
Thanks, very useful. |
This was referenced Jun 26, 2026
Contributor
|
Thanks, this is superseded by #538 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Changes:
get(),set(), the negative length arg ingetbits()andsetbits()sets Big Endian for reading and writing bits.get(), zero length ingetbits()will return nilThe need for this came about when reading signals in CAN messages in my little project where I chose Berry for scripting car automations.
There are some interesting variations how bit numbering within a byte can be interpreted. People refer to them as in/consistent, lsb0/msb0 and normal/inverted. It gets pretty confusing especially when thinking of data spanning multiple bytes. lsb0/msb0 is meaningless to me if bits can be inverted...
Let's just say that for working with a single data at the time that is in a code and not sent over a medium, only one numbering makes sense for Big Endian - msb0, consistent or normal.
Now why would anyone use different numberings?
One reason is that at low level the hardware may have bits ordered differently and perhaps we want to stream uncompressed bits over a medium, it is often better to start with MSB. Just like in numbering in English language. And unlike numbering in German which is objectively worse.
Einundzwanzig.Second reason is visualization of mixed endianness data. If have bytes incrementing down, the little endian starts in right top corner and big endian ends in left top corner. People want to have just one table header, so they fiddle with bits numbering
This is the for CANbus - the signal definition file (DBC) allows to define mixed signals in one byte array and so it uses inverted bits numbering for big endian to make visualisation indexing constant.

In Kvaser software the table is indexed using Little Endian interpretation, but we can insert Big Endian signal into it
It's MSB is on 7th bit in 0th byte and that's exactly how its offset is defined in DBC file.
If I put 0 as an offset in DBC, I would get this abomination:

So that's pretty terrible numbering.
We don't care about making drawings mixed endianness signals in a table. - Setbit/getbits functions always works with one signal at the time. And we don't interface with transmission mediums directly. - So we do the only reasonable thing and define signal with normal numbering, so 0 offset will give us fully filled byte. Example - first little endian:
And big endian:
All we have to do to visualize Big Endian is redraw bit indexes header in MS Paint to match Big Endian normal aka consistent bit order:

Side note, for converting CANbus DBC inverted offset to this normal big endian offset, all I have to do is
off + 7 - 2*(off % 8).Side note 2, I kinda want to add getibits and setibits helpers for arbitrary length signed integers...