Advertisement
mzografski

CheckBitAtPosition

Mar 15th, 2014
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. /*
  8.  * Write a Boolean expression that returns if the bit at position p
  9.  * (counting from 0, starting from the right) in given integer number n has value of 1.
  10.  */
  11.  
  12. class CheckBitAtPosition
  13. {
  14.     static void Main()
  15.     {
  16.         int haystack;
  17.         int position;
  18.  
  19.         Console.WriteLine("Please, enter unsigned integer:");
  20.         while (!int.TryParse(Console.ReadLine(), out haystack))
  21.         {
  22.             Console.WriteLine("Please, enter correct integer.");
  23.         }
  24.         Console.WriteLine("Please, set the bit position you are interested in:");
  25.         while (!int.TryParse(Console.ReadLine(), out position))
  26.         {
  27.             Console.WriteLine("Please, enter correct integer.");
  28.         }
  29.  
  30.         bool boolResult = (haystack & (1 << position)) != 0;
  31.  
  32.         Console.WriteLine("\n#{0} bit {1}", position, boolResult);
  33.         Console.WriteLine("\nPress any key to exit.");
  34.         Console.ReadKey();
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement