Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ModifyBitsAtPosition
- {
- class Program
- {
- static void Main()
- {
- Console.WriteLine("n=");
- int n = int.Parse(Console.ReadLine());
- Console.WriteLine("p=");
- int p = int.Parse(Console.ReadLine());
- Console.WriteLine("v=");
- int v = int.Parse(Console.ReadLine());
- int nRightP = n >> p;
- int bit = nRightP & 1;
- int mask;
- int result;
- if (bit == 0 & v == 0)
- {
- Console.WriteLine(n);
- }
- else if (bit == 0 && v == 1)
- {
- mask = 1 << p;
- result = n | mask;
- Console.WriteLine(result);
- }
- else if (bit == 1 && v == 0)
- {
- mask = ~(1 << p);
- result = n & mask;
- Console.WriteLine(result);
- }
- else if (bit == 1 && v == 1)
- {
- Console.WriteLine(n);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment