Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class Program
- {
- static void Main()
- {
- int lastBit;
- int result;
- Console.WriteLine("Please input new Bit value 0|1:");
- int newBit = int.Parse(Console.ReadLine());
- while ((newBit != 0) && (newBit != 1))
- {
- Console.WriteLine("Please input new Bit value 0|1:");
- newBit = int.Parse(Console.ReadLine());
- }
- Console.WriteLine("Please input integer value:");
- int number = Math.Abs(int.Parse(Console.ReadLine()));
- Console.WriteLine(Convert.ToString(number, 2).PadLeft(32, '0'));
- Console.WriteLine("Please input position to change:");
- int position = Math.Abs(int.Parse(Console.ReadLine()));
- int mask = (1 << position);
- lastBit = (number & (1 << position)) >> position;
- if (lastBit == newBit)
- {
- Console.WriteLine("Nothing to change!");
- }
- if (lastBit != newBit)
- {
- result = number ^ mask;
- Console.WriteLine(new string('-', 32));
- Console.WriteLine("{0} -> original", Convert.ToString(number, 2).PadLeft(32, '0'));
- Console.WriteLine("{0} -> new", Convert.ToString(result, 2).PadLeft(32, '0'));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement