this post was submitted on 27 Dec 2023
66 points (97.1% liked)
Programming
17352 readers
589 users here now
Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!
Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.
Hope you enjoy the instance!
Rules
Rules
- Follow the programming.dev instance rules
- Keep content related to programming in some way
- If you're posting long videos try to add in some form of tldr for those who don't want to watch videos
Wormhole
Follow the wormhole through a path of communities !webdev@programming.dev
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
I love low-level stuff and this still took me a little while to break down, so I'd like to share some notes on the author's code snippet that might help someone else.
The function
morse_decode
is meant to be called iteratively by another routine, once per morse "character"c
(dot, dash, or null) in a stream, while feeding its own output back into it asstate
. As long as the function returns a negative value, that value represents the next state of the machine, and the morse stream hasn't yet been resolved into an output symbol. When the return value is positive, that represents the decoded letter, and the next call tomorse_decode
should use astate
of 0. If the return value is 0, something has gone wrong with the decoding.state
is just a negated index into the arrayt
, which is actually two arrays squeezed into one. The first 64 bytes are a binary heap of bytes in the formatnnnnnnlr
, each corresponding to one node in the morse code trie.l
andr
are single bits that represent the existence of a left or right child of the current node (i.e. reading a dot or dash in the current state leading to another valid state).nnnnnn
is a 6-bit value that, when shifted appropriately and added to 63, becomes an index into the second part of the array, which is a list of UTF-8/ASCII codes for letters and numbers for the final output.Real MVP.