coasterka

#3CheckBitAtCurrPosition

Mar 14th, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.58 KB | None | 0 0
  1. static void Main()
  2.         {
  3.             Console.WriteLine("Enter a random number to check one of its bits of your choice.");
  4.             int n = int.Parse(Console.ReadLine());
  5.             Console.WriteLine("The bit at which position would you like to check?");
  6.             int p = int.Parse(Console.ReadLine());
  7.             int nRightP = n >> p;
  8.             int bit = nRightP & 1;
  9.             bool bitIsOne = bit == 1;
  10.             Console.WriteLine(Convert.ToString(n, 2).PadLeft(16, '0'));
  11.             Console.WriteLine("Bit at position {0} is = 1 - {1}", p, bitIsOne);
  12.         }
Advertisement
Add Comment
Please, Sign In to add comment