We are given the number n, the value v (v = 0 or 1) and the position p. write a sequence of operations that changes the value of n, so the bit on the position p has the value of v. Example: n=35, p=5, v=0 -> n=3. Another example: n=35, p=2, v=1 -> n=39.
I am unable to find a way to only use that bit on the position p.
If i do n >> p with a number such as 35 in bits I will have 100011 >> 5 = 00001
I don't know how to get the value of v here. Mathematically even if I think about it above the value of n becomes 1 not 3 after this operation. I am totally confused as I can not explain the problem to myself.
Console.Write("Enter n: ");
int n = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter p: ");
int p = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter v: ");
int v = Convert.ToInt32(Console.ReadLine());
int mask = n >> 5;
Console.WriteLine(mask);