mayap

ExtractBitFromInteger

Apr 11th, 2014
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ExtractBitFromInteger
  8. {
  9.     class ExtractBitFromInteger
  10.     {
  11.         static void Main()
  12.         {
  13.             int num, p;
  14.             Console.WriteLine("Enter number: ");
  15.             num = int.Parse(Console.ReadLine());
  16.             Console.WriteLine("Enter position of bit: ");
  17.             p = int.Parse(Console.ReadLine());
  18.  
  19.             int numRightP = num >> p;
  20.             int bit = numRightP & 1;
  21.  
  22.             Console.WriteLine("The bit of number " + num + " which is in" + " position " + p + " is " + bit);
  23.  
  24.         }
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment