C
C#2y ago
breakfast

❔ Practical use for bitwise operators, specifically |= in context.

Not bitwise application. This is a simple boolean or equals Howdy folks. Currently working in a new codebase with a lot of legacy code and 0 of the folks that wrote it still around. To preface I ran into this bit of code and am failing to truly understand what it's doing.
bool myFlag = myString == string.Empty;
myFlag |= GetCategoryCount(currentType) == 1;

// Attempting to simplify this. Singleton where we're returning the index count from a Category type.
bool myFlag = myString == string.Empty;
myFlag |= GetCategoryCount(currentType) == 1;

// Attempting to simplify this. Singleton where we're returning the index count from a Category type.
Now I do understand the basics of bit-level operators and how they function at the operator level (bit column comparisons resulting in a new binary representation ). I've just never ran into them in production, and honestly might still be too "green" with them to understand what they're doing exactly. Happy to give more context, but as it stands I'm uncertain of the dev was trying to achieve here.
7 Replies
breakfast
breakfastOP2y ago
This actually looks to be boolean overriding? May delete after some tests here
Thinker
Thinker2y ago
does the same as
bool myFlag = myString == string.Empty | GetCategoryCount(currentType) == 1;
bool myFlag = myString == string.Empty | GetCategoryCount(currentType) == 1;
canton7
canton72y ago
It's not bitwise - it's a boolean 'or equals'
Thinker
Thinker2y ago
yeah, it's like || except it always evaluated both sides
canton7
canton72y ago
There's no ||=, so they probably reached for |=, even though it's a bit less efficient in this case as it will always evaluate GetCategoryCount(currentType) == 1 regardless of whether myFlag is true or false
breakfast
breakfastOP2y ago
Thank you so much. I know this is a rather simple one. But it looked a bit strange to me.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server