stkirov

BitValue

Oct 9th, 2012
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.36 KB | None | 0 0
  1. //Write an expression that extracts from a given integer i the value of a given bit number b. Example: i=5; b=2  value=1.
  2. using System;
  3.  
  4. class Program
  5. {
  6.     static void Main()
  7.     {
  8.         int i = int.Parse(Console.ReadLine());
  9.         int b = int.Parse(Console.ReadLine());
  10.         int mask = 1 << b;
  11.         Console.WriteLine((i&mask)!=0 ? 1 : 0);
  12.     }
  13. }
Advertisement
Add Comment
Please, Sign In to add comment