Advertisement
Fundamentalen

ExtractBitFromInteger

Mar 14th, 2014
1,168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.75 KB | None | 0 0
  1. using System;
  2.  
  3. class ExtractBitFromInteger
  4. {
  5.     static void Main()
  6.     {
  7.         Console.Write("Enter your number: ");
  8.         int number = int.Parse(Console.ReadLine());
  9.         Console.Write("Check bit of position: ");
  10.         int position = int.Parse(Console.ReadLine());
  11.  
  12.         int fakeBit = 1 << position;
  13.         int foundBit = number & fakeBit;
  14.  
  15.         if (foundBit == 0)
  16.         {
  17.             Console.WriteLine("Bit of position {0} is '0'", position);
  18.             Console.WriteLine(Convert.ToString(number, 2).PadLeft(16, '0'));
  19.         }
  20.         else
  21.         {
  22.             Console.WriteLine("Bit of position {0} is '1'", position);
  23.             Console.WriteLine(Convert.ToString(number, 2).PadLeft(16, '0'));
  24.         }
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement