Advertisement
mzografski

BitwizeExtract3rd

Mar 15th, 2014
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. /*
  8.  * Using bitwise operators, write an expression for finding the value of the bit #3
  9.  * of a given unsigned integer.
  10.  * The bits are counted from right to left, starting from bit #0.
  11.  * The result of the expression should be either 1 or 0.
  12.  */
  13.  
  14. class ExtractBitAtPos3
  15. {
  16.     static void Main()
  17.     {
  18.         byte mask = 1 << 3;
  19.         uint haystack;
  20.         Console.WriteLine("Please, enter unsigned integer:");
  21.         while(!uint.TryParse(Console.ReadLine(), out haystack))
  22.         {
  23.             Console.WriteLine("Please, enter correct integer.");
  24.         }
  25.         uint result = mask & haystack;
  26.         Console.WriteLine("\n#3 bit is {0}",result>>3);
  27.         Console.WriteLine("\nPress any key to exit.");
  28.         Console.ReadKey();
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement