lmarkov

Check The Third Bit

Nov 26th, 2012
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.85 KB | None | 0 0
  1. using System;
  2.  
  3. class CheckTheThirdBit
  4. {
  5.     static void Main()
  6.     {
  7.         int number, checkNumber;
  8.  
  9.         Console.Write("Enter a number: ");
  10.         if (int.TryParse(Console.ReadLine(), out number) && number >= int.MinValue && number <= int.MaxValue)
  11.         {
  12.             checkNumber = number & 8;
  13.             bool thirdBitValue = checkNumber != 0;
  14.             if (thirdBitValue)
  15.             {
  16.                 Console.WriteLine("The bit 3 of {0} is 1.\n", number);
  17.                 Main();
  18.             }
  19.             else
  20.             {
  21.                 Console.WriteLine("The bit 3 of {0} is 0\n", number);
  22.                 Main();
  23.             }
  24.         }
  25.         else
  26.         {
  27.             Console.WriteLine("Invalid input! Please enter a number between {0} and {1}", int.MinValue, int.MaxValue);
  28.             Main();
  29.         }
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment