Advertisement
VyaraG

ExtractBitFromInt

Nov 28th, 2014
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.58 KB | None | 0 0
  1. using System;
  2.  
  3. //Write an expression that extracts from given integer n the value of given bit at index p.
  4.  
  5. class ExtractBitFromInt
  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.  
  15.         if ((mask & number) == 0)
  16.         {
  17.             Console.WriteLine("0");
  18.         }
  19.         else
  20.         {
  21.             Console.WriteLine("1");
  22.         }
  23.      
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement