Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- public class ExtractBit
- {
- public static void Main()
- {
- int p = 3; //Here we set position of searched bit
- Console.Write("Please input number: "); //Here we ask for number
- int number = int.Parse(Console.ReadLine());
- int mask = 1 << p; //Here we set mask with value 1 at position P.
- int foundBit = number & mask; //Here we check choosen bit. We use & to check is it 0 or 1.
- if (foundBit == 0) //Below we just print result
- {
- Console.WriteLine("Third bit is 0.");
- }
- else
- {
- Console.WriteLine("Third bit is 1.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement