Timeline for How do you set, clear, and toggle a single bit?
Current License: CC BY-SA 3.0
7 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Sep 27, 2017 at 18:00 | comment | added | chux - Reinstate Monica |
The (size_t)(b)/(8*sizeof *(a)) unnecessarily could narrow b before the division. Only an issue with very large bit arrays. Still an interesting macro.
|
|
Sep 27, 2017 at 17:58 | comment | added | chux - Reinstate Monica |
Minor: the 3rd (size_t) cast seem to be there only to insure some unsigned math with % . Could (unsigned) there.
|
|
Oct 23, 2015 at 17:08 | comment | added | PC Luddite | @R.. This answer is really old, but I'd probably prefer a function to a macro in this case. | |
May 9, 2013 at 14:21 | history | edited | R.. GitHub STOP HELPING ICE | CC BY-SA 3.0 |
added 2 characters in body
|
Jul 13, 2010 at 9:19 | comment | added | R.. GitHub STOP HELPING ICE |
Indeed. =) One variant you might prefer is to separate it into 2 macros, 1 for addressing the array element and the other for shifting the bit into place, ala BITCELL(a,b) |= BITMASK(a,b); (both take a as an argument to determine the size, but the latter would never evaluate a since it appears only in sizeof ).
|
|
Jul 13, 2010 at 8:27 | comment | added | foraidt |
It's good to read but one should be aware of possible side effects. Using BITOP(array, bit++, |=); in a loop will most likely not do what the caller wants.
|
|
Jul 13, 2010 at 6:53 | history | answered | R.. GitHub STOP HELPING ICE | CC BY-SA 2.5 |