What is simplest way to assign a bit mask to integer value? For example I want integer with first, third and forth bits = 1, other = 0.
Certainly I am looking for code, not for single value! And certainly there lot of possibilities, but I try to find simplest and most descriptive looking
I think the best way to think (!) is to just index bits from 0, and then apply "to set the n:th bit, bitwise-OR with the value (1 << n)":
int first_third_and_fourth = (1 << 0) | (1 << 2) | (1 << 3);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With