08-17-2023 09:10 AM
Hi Natasha,
@Natasha1994 wrote:
Will it also work in a reverse order ? To change 0 to 1 ?
Sure - as soon as you use the correct mask and the correct boolean operation (as mentioned above)…
08-17-2023 10:03 AM
C# reference, but the logic is the same
08-17-2023 10:51 AM
08-17-2023 11:25 AM - edited 08-17-2023 11:26 AM
Here's a more general version where you can freely select all the bits you like to toggle:
08-17-2023 11:38 AM
Because I am TERRIBLE at bitwise operations, I usually translate the numbers into Booleans so I can manipulate them "directly". It is more important to me that the results are accurate than me trying to be efficient. I know most people prefer to manipulate the numbers directly using bitwise operations. I just have a mental block about them.
And anecdotally, I've never seen an instance where Booleans were used that the results were incorrect; on the other hand, I've found plenty of instances where bitwise operations were performed incorrectly leading to erroneous results.
08-17-2023 11:53 AM - edited 08-17-2023 11:58 AM
@billko wrote:
And anecdotally, I've never seen an instance where Booleans were used that the results were incorrect; on the other hand, I've found plenty of instances where bitwise operations were performed incorrectly leading to erroneous results.
Disagree! I've seen plenty of cases where boolean operation were incorrect (especially here in the forum, not with my own code!, of course!), OTOH, bitwise operations on unsigned integers are identical to boolean operations. If they are incorrect, the underlying boolean operation was already incorrect. 😄
Substituting boolean operations for bitwise, gives you a dramatic penalty (8x more memory per bit!), and adding the conversion overhead adds to the expense.
08-17-2023 04:12 PM - edited 08-17-2023 04:13 PM
@altenbach wrote:
@billko wrote:
And anecdotally, I've never seen an instance where Booleans were used that the results were incorrect; on the other hand, I've found plenty of instances where bitwise operations were performed incorrectly leading to erroneous results.
Disagree! I've seen plenty of cases where boolean operation were incorrect (especially here in the forum, not with my own code!, of course!), OTOH, bitwise operations on unsigned integers are identical to boolean operations. If they are incorrect, the underlying boolean operation was already incorrect. 😄
Substituting boolean operations for bitwise, gives you a dramatic penalty (8x more memory per bit!), and adding the conversion overhead adds to the expense.
I think of it as calculating which switch to throw vs just looking at the switches and throwing the right one. And unless I am going to do this 1000x in a FOR loop, I don't really care if it takes a split second longer or multiplies my memory usage by 8x per bit.
To me, it's much easier to visualize which bit to flip than to calculate it. And I've seen bad mask calculations a lot more than bit miswirings.
08-18-2023 08:57 AM
@billko wrote:
Because I am TERRIBLE at bitwise operations, I usually translate the numbers into Booleans so I can manipulate them "directly". It is more important to me that the results are accurate than me trying to be efficient. I know most people prefer to manipulate the numbers directly using bitwise operations. I just have a mental block about them.
And anecdotally, I've never seen an instance where Booleans were used that the results were incorrect; on the other hand, I've found plenty of instances where bitwise operations were performed incorrectly leading to erroneous results.
I do that in a develop/test stage many times, or just use a binary display of a number.
08-18-2023 09:03 AM
@Yamaeda wrote:
@billko wrote:
Because I am TERRIBLE at bitwise operations, I usually translate the numbers into Booleans so I can manipulate them "directly". It is more important to me that the results are accurate than me trying to be efficient. I know most people prefer to manipulate the numbers directly using bitwise operations. I just have a mental block about them.
And anecdotally, I've never seen an instance where Booleans were used that the results were incorrect; on the other hand, I've found plenty of instances where bitwise operations were performed incorrectly leading to erroneous results.
I do that in a develop/test stage many times, or just use a binary display of a number.
I never thought of that!!! I could do an explicit Boolean conversion to check my blind spot bitwise operation for validity! Then I could have the best of both worlds.
08-18-2023 11:13 AM
@billko wrote:
@Yamaeda wrote:I do that in a develop/test stage many times, or just use a binary display of a number.
I never thought of that!!! I could do an explicit Boolean conversion to check my blind spot bitwise operation for validity! Then I could have the best of both worlds.
I also like the binary display when dealing with bit-wise logic. It works well for 8 or 16-bit integers. Once going for a 32-bit or 64-bit, it becomes unwieldy and I switch to hex display.