Advertisement
Fundamentalen

CheckABitAtGivenPosition

Mar 14th, 2014
1,136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.56 KB | None | 0 0
  1. using System;
  2.  
  3. class CheckABitAtGivenPosition
  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 moveBit = number >> position;
  13.         int foundBit = moveBit & 1;
  14.  
  15.         bool isOne = foundBit == 1;
  16.  
  17.         Console.WriteLine(Convert.ToString(number, 2).PadLeft(16, '0'));
  18.         Console.WriteLine("Bit of position {0} is '1' = {1}", position, isOne);
  19.     }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement