I have very simple bitwise working methods that I want to use as follows:
myInt.SetBit(int k, bool set)
so that it changes the bit in the index "k" to the value "set" (0 or 1), I first thought about how to do this as follows:
public static void SetBit(this int A, int k, bool set) {
if (set) A |= 1 << k;
else A &= ~(1 << k);
}
but, of course, this only changes the internal value of the variable "A", and not the original variable, since the integer is not a reference type.
I cannot use 'ref' with 'this', so I don’t know how to turn this into a reference parameter.
I already have similar methods for Int arrays, but they work just fine since the array is a reference type.
I am looking for a way to preserve this convenient syntax for single integers, if any.
source
share