Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Write a boolean expression that returns if the bit at position p (counting from 0) in a given integer number v has value of 1. Example: v=5; p=1 false.
- */
- using System;
- class BitCheck
- {
- static void Main()
- {
- int numberV, bitPositionP,numberMask, numberToCheck;
- bool bitCheckResult;
- Console.WriteLine("Enter a number:");
- if (int.TryParse(Console.ReadLine(), out numberV) && numberV >= int.MinValue && numberV <= int.MaxValue)
- {
- Console.WriteLine("Enter bit position which you want to check for 1 (count from 0): ");
- if(int.TryParse(Console.ReadLine(), out bitPositionP) && bitPositionP >= int.MinValue && bitPositionP <= int.MaxValue)
- {
- numberMask = 1 << bitPositionP;
- numberToCheck = numberV & numberMask;
- if (numberToCheck != 0)
- {
- bitCheckResult = true;
- }
- else
- {
- bitCheckResult = false;
- }
- Console.WriteLine("Bit position {0} for the number {1} is 1? -> {2}\n",bitPositionP, numberV, bitCheckResult);
- Main();
- }
- }
- else
- {
- Console.WriteLine("Invalid input!\n");
- Main();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment