svetlozar_kirkov

Bool Check Bit At Position (Exercise)

Sep 27th, 2014
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.63 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace ConsoleTest
  5. {
  6.     class ConsoleTest
  7.     {
  8.         static void Main()
  9.         {
  10.             int v = int.Parse(Console.ReadLine());
  11.             byte p = byte.Parse(Console.ReadLine());
  12.             Console.WriteLine(Convert.ToString(v, 2).PadLeft(8,'0'));
  13.             bool hasValueOne = false;
  14.             int shifted = v >> p;
  15.             string final = shifted.ToString();
  16.             if (final.Last() == '1')
  17.             {
  18.                 hasValueOne = true;
  19.             }
  20.             Console.WriteLine("Bit at position " + p + @" has value ""1"": " + hasValueOne);
  21.         }
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment