We are given integer n, value v (v=0 or 1) and a position p. Write a sequence of operators that modifies n to hold the value v at the position p from the binary representation of n. Example:
- n = 5 (00000101), p=3, v=1 -> 13 (00001101)
- n = 5 (00000101), p=2, v=0 -> 1 (00000001)
This is my code:
int n1 = 35;
int p1 = 3;
int v = 1;
n1 = n1 + (v << p1);
System.out.println(n1);
It works when the v=1 but when the v=0 it doesn't.