Advertisement
JOHNYTHEWINNER

Problem 11.Bitwise: Extract Bit #3

Apr 13th, 2015
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.66 KB | None | 0 0
  1. using System;
  2.  
  3. public class ExtractBit
  4. {
  5.     public static void Main()
  6.     {
  7.         int p = 3;  //Here we set position of searched bit
  8.         Console.Write("Please input number: ");  //Here we ask for number
  9.         int number = int.Parse(Console.ReadLine());
  10.  
  11.         int mask = 1 << p;  //Here we set mask with value 1 at position P.
  12.         int foundBit = number & mask; //Here we check choosen bit. We use & to check is it 0 or 1.
  13.  
  14.         if (foundBit == 0) //Below we just print result
  15.         {
  16.             Console.WriteLine("Third bit is 0.");
  17.         }
  18.         else
  19.         {
  20.             Console.WriteLine("Third bit is 1.");
  21.         }
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement