Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static void Main()
- {
- Console.WriteLine("Enter a number to change a current bit of its binary code.");
- int n = int.Parse(Console.ReadLine());
- Console.WriteLine("Which bit position you'd like to change?");
- int p = int.Parse(Console.ReadLine());
- Console.WriteLine("What value would you like the bit at pos {0} to hold?\nChoose between 0 and 1", p);
- int v = int.Parse(Console.ReadLine());
- Console.WriteLine("Number in binary: {0}", Convert.ToString(n, 2).PadLeft(16, '0'));
- switch (v)
- {
- case 0:
- {
- int mask = ~(1 << p);
- int result = mask & n;
- Console.WriteLine("Result in decimal: {0}", result);
- Console.WriteLine("Result in binary: {0}", Convert.ToString(result, 2).PadLeft(16, '0'));
- break;
- }
- case 1:
- {
- int mask = 1 << p;
- int result = mask | n;
- Console.WriteLine("Result in decimal: {0}", result);
- Console.WriteLine("Result in binary: {0}", Convert.ToString(result, 2).PadLeft(16, '0'));
- break;
- }
- default:
- {
- Console.WriteLine("Wrong input!");
- break;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment