SHARE
TWEET

BitCheck

a guest Nov 10th, 2013 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Write a boolean expression that returns if the bit at position p (counting from 0)
  2. // in a given integer number v has value of 1. Example: v=5; p=1  false.
  3.  
  4.  
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9.  
  10.  
  11. class BitCheck
  12. {
  13.     static void Main(string[] args)
  14.     {
  15.         Console.Write("Input number: ");
  16.         int v = int.Parse(Console.ReadLine());
  17.         Console.Write("What position of the number in binary do you want: ");
  18.         int p = int.Parse(Console.ReadLine());
  19.         int mask = 1 << p;
  20.         int vAndMask = v & mask;
  21.         int bit = vAndMask >> p;
  22.         Console.WriteLine(bit);
  23.  
  24.     }
  25. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top