Advertisement
VyaraG

ExtractBit3

Nov 28th, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.61 KB | None | 0 0
  1. using System;
  2.  
  3. //Using bitwise operators, write an expression for finding the value of the bit #3 of a given unsigned integer. The bits are counted from right to left, starting from bit #0. The result of the expression should be either 1 or 0.
  4.  
  5. class ExtractBit3
  6. {
  7.     static void Main()
  8.     {
  9.         Console.Write("Enter a positive number: ");
  10.         uint n = uint.Parse(Console.ReadLine());
  11.         uint mask = 1 << 3;
  12.         if ((n & mask) == 0)
  13.         {
  14.             Console.WriteLine("0");
  15.         }
  16.         else
  17.         {
  18.             Console.WriteLine("1");
  19.         }
  20.  
  21.        
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement