-
Notifications
You must be signed in to change notification settings - Fork 2.1k
bcd: initial import of binary coded decimal en-/decoder #7311
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
Conversation
static void test_bcd_to_byte__greater_0x99(void) | ||
{ | ||
TEST_ASSERT_EQUAL_INT(100, bcd_to_byte(0xa0)); | ||
TEST_ASSERT_EQUAL_INT(100, bcd_to_byte(0xa0)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
duplicate test for 100
, and shouldn't this be the opposite version of test_bcd_from_byte__greater_99
- meaning both should test same values?!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the duplicate line, but they don't have the same value, since they are out of range => garbled "input", which is the whole point of this test.
(in fact the output of |
interesting encoding, but which driver is (seriously) using this - and why 😕 ❓ |
ah oversaw your reference ... |
Among others (like the DS1307) the x86 RTC |
The mensa-cards at FU also store the current account balance as a BCD, IIRC. |
As to why according to my quick research mainly a) legacy reasons (see x86 RTC) and simplified ASCII representation. Since the DS1307 is mainly used by the Arduino community I guess the latter applies for this device. |
(not only ASCII, but also e.g. 7-segment displays). |
that sounds reasonable, indeed! Wasn't aware that it might still have a good use-case ... mostly read articles stating it is legacy stuff and "not supported by modern CPUs" bla bla ... |
*/ | ||
static inline uint8_t bcd_from_byte(uint8_t byte) | ||
{ | ||
return byte + (6 * (byte / 10)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though this is (more) elegant, it's not as intuitive as ((byte / 10) << 4) | (byte % 10)
, which makes the conversion IMHO quite explicit. I don't suggest to change this, but it might be worth a comment (or example)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not only is it more elegant, but also more efficient ;-), because you only have one costly division op (counting mod as a division).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise ACK
please squash, too |
Squashed |
This imports a simple BCD en-/decoder. I require that for a driver I'm currently porting.