Advertisement
dimipan80

3.12 Extract Bit From Integer Number At Given Position

Jun 5th, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.94 KB | None | 0 0
  1. // Write an expression that extracts from given integer n the value of given bit at index p.
  2.  
  3. namespace _12.ExtractBitFromIntegerOnGivenPosition
  4. {
  5.     using System;
  6.  
  7.     public class ExtractBitFromIntegerOnGivenPosition
  8.     {
  9.         public static void Main(string[] args)
  10.         {
  11.             checked
  12.             {
  13.                 Console.Write("Enter a whole non-negative number: ");
  14.                 uint num = uint.Parse(Console.ReadLine());
  15.                 int indexP;
  16.                 do
  17.                 {
  18.                     Console.Write("Enter index for Bit position: ");
  19.                 }
  20.                 while (!int.TryParse(Console.ReadLine(), out indexP) || indexP < 0 || indexP > 31);
  21.  
  22.                 uint bitValueOnPositionP = (num >> indexP) & 1;                
  23.  
  24.                 Console.WriteLine("In Given number, on position {0}, Bit Value is: {1} !", indexP, bitValueOnPositionP);
  25.             }
  26.         }
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement