Advertisement
adriyanbulgary

OperatorsExpressionsAndStatements - Task 13

Jun 13th, 2014
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.04 KB | None | 0 0
  1. using System;
  2. /*
  3.  * Write a Boolean expression that returns if the bit at position p
  4.  * (counting from 0, starting from the right) in given integer number n has value of 1
  5.  */
  6. class CheckBitAtGivenPosition
  7. {
  8.     static void Main()
  9.     {
  10.  
  11.         int number;
  12.         byte bitPosition;
  13.         int changeNumber;
  14.         int bit;
  15.         bool isOne;
  16.         Console.WriteLine("Please enter the number");
  17.         number = int.Parse(Console.ReadLine());
  18.         Console.WriteLine("The number {0} in binary = {1}",number,Convert.ToString(number,2));
  19.         Console.WriteLine("Please choose position of the bit");
  20.         bitPosition=byte.Parse(Console.ReadLine());
  21.         changeNumber = number >> bitPosition;
  22.         bit = bitPosition & 1;
  23.         isOne = bit == 1;
  24.         if (isOne)
  25.         {
  26.             Console.WriteLine("Is the bit on position {0} = 1? -   {1} ",bitPosition,isOne);
  27.         }
  28.         else
  29.         {
  30.             Console.WriteLine("Is the bit on position {0} = 1? -   {1} ",bitPosition,isOne);
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement