Advertisement
Asinka

WitchBit-stay-OfPositionP

Oct 6th, 2012
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.88 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4. class WitchBitStayOfPositionP
  5. {
  6.     static void Main()
  7.     {
  8.         Console.Write("Enter Number here: n = ");
  9.         int n = Convert.ToInt32(Console.ReadLine());
  10.         Console.Write("Enter Position here: p = ");
  11.         int p = Convert.ToInt32(Console.ReadLine());
  12.  
  13.     /* Counting random number, witch in binary representation has a bit 1 of position p and
  14.     the other bits  are 0 . Example: p = 5 position(binary) = 100000 and position(decimal) = 32 */
  15.         int position = Convert.ToInt32(Math.Pow(2, p));
  16.  
  17.     /* Example: n = 35 (100011); p = 5; position = 32 (100000) , 1&1 == 1 => bit 1 */
  18.     if ((n & position) == 0)
  19.         {
  20.             Console.WriteLine("The bit, witch stay ot position P from number N is: \n0");
  21.         }
  22.         else
  23.         {
  24.             Console.WriteLine("The bit, witch stay ot position P from number N is: \n1");
  25.         }
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement