Advertisement
dimipan80

3.BooleanTrueIfBitAtPositionPHasValue1

Apr 5th, 2014
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.82 KB | None | 0 0
  1. class CheckABitAtGivenPosition
  2. {
  3.     static void Main ()
  4.     {
  5.         Console.Write("Please, enter a whole integer non-negative number, N = ");
  6.         string numberStr = Console.ReadLine();
  7.         int numberN = int.Parse(numberStr);
  8.         Console.Write("Enter other whole non-negative number in the range [0 ... 31], P = ");
  9.         numberStr = Console.ReadLine();
  10.         int numberP = int.Parse(numberStr);
  11.         int bitMask = 1 << numberP;
  12.         int valueOfBitOnP = numberN & bitMask;
  13.         bool pBitHasValue1;
  14.         if (valueOfBitOnP == 0)
  15.         {
  16.             pBitHasValue1 = false;
  17.         }
  18.         else
  19.         {
  20.             pBitHasValue1 = true;
  21.         }
  22.         Console.WriteLine("The Bit at position P, in that number, has Value 1: " + pBitHasValue1);
  23.         Console.ReadLine();
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement