Reputation: 4401
This is actually fairly tricky to Google.
How do you SET (bitwise or) the top two bits of a 32 bit int?
I am getting compiler warnings from everything I try.
Upvotes: 1
Views: 791
Reputation: 27874
integerVariable |= 0xC0000000;
Use 0xC0000000u
for an unsigned integer variable.
Showing the entire 32-bit integer in hex notation is clearer to me than the bit shifts in Mehrdad's answer. They probably compile to the same thing, though, so use whichever looks clearer to you.
Upvotes: 1
Reputation: 422152
Try this:
integerVariable |= 3 << 30;
It may be more clear to use (1 << 31) | (1 << 30) instead of (3 << 30), or you could add a comment about the behavior. In any case, the compiler should be able to optimize the expression to a single value, which is equal to int.MinValue >> 1
== int.MinValue / 2
.
If it's a uint
:
uintVar |= 3u << 30;
Upvotes: 6