Advertisement
kuruku

CheckBit

Apr 16th, 2014
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.72 KB | None | 0 0
  1. using System;
  2.  
  3. // Write a Boolean expression that checks if the bit on position p in the
  4. //integer v has the value 1. Example v=5, p=1 -> false.
  5.  
  6. class CheckBit
  7. {
  8.     static void Main()
  9.     {
  10.         Console.Write("n = ");
  11.         int n = int.Parse(Console.ReadLine());
  12.         Console.Write("p = ");
  13.         int p = int.Parse(Console.ReadLine());
  14.         int mask = (int)Math.Pow(2, p);
  15.         string binaryMask = Convert.ToString(mask, 2);   //binary of mask
  16.         string binaryN = Convert.ToString(n, 2);   //binary of n
  17.         bool valueOne = true;
  18.         int position = mask & n;
  19.         if (position == 0)
  20.         {
  21.             valueOne = false;
  22.         }
  23.         Console.WriteLine(valueOne);
  24.  
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement