Advertisement
Guest User

test12

a guest
Nov 7th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1. using System;
  2.  
  3. class Program
  4. {
  5.     static void Main()
  6.     {
  7.         int lastBit;
  8.         int result;
  9.         Console.WriteLine("Please input new Bit value 0|1:");
  10.         int newBit = int.Parse(Console.ReadLine());
  11.         while ((newBit != 0) && (newBit != 1))
  12.         {
  13.             Console.WriteLine("Please input new Bit value 0|1:");
  14.             newBit = int.Parse(Console.ReadLine());
  15.         }
  16.         Console.WriteLine("Please input integer value:");
  17.         int number = Math.Abs(int.Parse(Console.ReadLine()));
  18.         Console.WriteLine(Convert.ToString(number, 2).PadLeft(32, '0'));
  19.         Console.WriteLine("Please input position to change:");
  20.         int position = Math.Abs(int.Parse(Console.ReadLine()));
  21.         int mask = (1 << position);
  22.         lastBit = (number & (1 << position)) >> position;
  23.         if (lastBit == newBit)
  24.         {
  25.             Console.WriteLine("Nothing to change!");
  26.         }
  27.         if (lastBit != newBit)
  28.         {
  29.             result = number ^ mask;
  30.             Console.WriteLine(new string('-', 32));
  31.             Console.WriteLine("{0} -> original", Convert.ToString(number, 2).PadLeft(32, '0'));
  32.             Console.WriteLine("{0} -> new", Convert.ToString(result, 2).PadLeft(32, '0'));
  33.         }
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement