Timeline for Algorithm to find the sequence of bits that makes up to a given number
Current License: CC BY-SA 4.0
16 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Mar 4, 2022 at 9:00 | history | tweeted | twitter.com/StackCodeReview/status/1499671002254610435 | ||
Mar 4, 2022 at 2:21 | comment | added | Peter Cordes |
Err wait a minute, you aren't serializing the number into base 2 (the sequence of bits), you're turning it into some other kind of sequence? Like making change with a given maximum denomination? Since nBits is an exponent / shift-count, this seems utterly trivial to do on the fly, and not something you'd want to actually store in an array. e.g. number >> nBits of the max size, with number & ((1<<nBits)-1) left over for you to split up into single bits if you want
|
|
Mar 4, 2022 at 2:12 | comment | added | Peter Cordes |
Are you intentionally avoiding using bitwise operations here? C guarantees that integer types use base 2 (aka binary), so you can do stuff like bit = x & 1; and x >>= 1; to get the next bit. It's somewhat interesting to only use arithmetic operations that don't depend on running on a binary computer, e.g. ln *= 2; or ln /= 2; inside a loop (as a strength-reduction of the 2**n you're doing with 1<<n ), and compares, but you are using 1<<n . So just wondering whether this is a missed optimization / simplification, or an intentionally arithmetic/comparison based algorithm.
|
|
Mar 3, 2022 at 15:27 | history | became hot network question | |||
Mar 3, 2022 at 10:34 | answer | added | Martin R | timeline score: 8 | |
Mar 3, 2022 at 9:41 | history | edited | Edenia | CC BY-SA 4.0 |
added 580 characters in body
|
Mar 3, 2022 at 8:49 | comment | added | Toby Speight | I possibly should have read the rest of the comment! ;-) | |
Mar 3, 2022 at 8:47 | comment | added | Edenia | @TobySpeight this is essentially exacted what I stated later in the same comment. I did not change the code, because I don't want to potentially invalidate someone's answer in the process of writing it. | |
Mar 3, 2022 at 8:43 | comment | added | Toby Speight | "it definitely works" - only for small values of "work". My answer shows it has a serious bug that you didn't identify. | |
Mar 3, 2022 at 8:41 | answer | added | Toby Speight | timeline score: 10 | |
Mar 3, 2022 at 8:33 | comment | added | greybeard |
What is a decimal @number , how do you pass it to binary() , and how would you pass a non-decimal one?
|
|
Mar 3, 2022 at 8:32 | comment | added | greybeard | better approach this is exactly where the need for more context, for a detailed specification, for details about intended use arises. As is, all I see is code trying to give the result (in a "unary" representation) to a change making problem with odd parameters. | |
Mar 3, 2022 at 8:13 | comment | added | Edenia |
As for the code, it definitely works. In my use case, I'd use it for much smaller numbers (say up to 1000). If a reviewer can't point out the obvious need for error-checking (such as when the allocation function fails due to an extreme allocation) or even better approach this differently so that there is no need to make such allocations or the need to use unsigned everywhere, then I am definitely not in the right place.
|
|
Mar 3, 2022 at 7:59 | comment | added | Edenia | I can agree with half of what you said, but what is the point of telling more about what the code is intended for? I don't see a reason, at all. I need it for something so specific that the chances of even everyone in the community using it for this are insignificant. Instead, I am just sharing my code, in case someone finds it for useful. I don't require help or anything. Everyone is free to ignore it, in which case I might as well delete it. Or improve it myself. | |
Mar 3, 2022 at 7:43 | comment | added | greybeard |
(You haven't been that active lately.) I was curious to see what will happen if I subjected it to a review CR is about insights into working code (from one of your projects). You may be prompted to tell more about what the code is intended for (How does this [optimise large quantities] of small entities ?), and what makes you think it works. How about binary(30, 987654321) ?
|
|
Mar 3, 2022 at 7:25 | history | asked | Edenia | CC BY-SA 4.0 |