Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- /*
- * Write a Boolean expression that returns if the bit at position p
- * (counting from 0, starting from the right) in given integer number n has value of 1
- */
- class CheckBitAtGivenPosition
- {
- static void Main()
- {
- int number;
- byte bitPosition;
- int changeNumber;
- int bit;
- bool isOne;
- Console.WriteLine("Please enter the number");
- number = int.Parse(Console.ReadLine());
- Console.WriteLine("The number {0} in binary = {1}",number,Convert.ToString(number,2));
- Console.WriteLine("Please choose position of the bit");
- bitPosition=byte.Parse(Console.ReadLine());
- changeNumber = number >> bitPosition;
- bit = bitPosition & 1;
- isOne = bit == 1;
- if (isOne)
- {
- Console.WriteLine("Is the bit on position {0} = 1? - {1} ",bitPosition,isOne);
- }
- else
- {
- Console.WriteLine("Is the bit on position {0} = 1? - {1} ",bitPosition,isOne);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement