Advertisement
Guest User

BitCheck

a guest
Nov 10th, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.67 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement