Advertisement
VyaraG

CheckABitAtGivenPosition

Nov 28th, 2014
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.55 KB | None | 0 0
  1. using System;
  2.  
  3. //Write a Boolean expression that returns if the bit at position p (counting from 0, starting from the right) in given //integer number n has value of 1.
  4.  
  5. class CheckABitAtGivenPosition
  6. {
  7.     static void Main()
  8.     {
  9.         Console.Write("Enter a number: ");
  10.         int number = int.Parse(Console.ReadLine());
  11.         Console.Write("Enter a position: ");
  12.         int position = int.Parse(Console.ReadLine());
  13.         int mask = 1 << position;
  14.         bool isOne = ((mask & number) == 0 ? false : true);
  15.         Console.WriteLine(isOne);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement