Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- class ExtractBitFromInt
- {
- static void Main()
- {
- int haystack;
- int position;
- int m = 1;
- Console.WriteLine("Please, enter unsigned integer:");
- while (!int.TryParse(Console.ReadLine(), out haystack))
- {
- Console.WriteLine("Please, enter correct integer.");
- }
- Console.WriteLine("Please, set the bit position you are interested in:");
- while (!int.TryParse(Console.ReadLine(), out position))
- {
- Console.WriteLine("Please, enter correct integer.");
- }
- int mask = m << position;
- int result = mask & haystack;
- Console.WriteLine("\n#{0} bit is {1}", position, result >> position);
- Console.WriteLine("\nPress any key to exit.");
- Console.ReadKey();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement