Advertisement
valent1n

Untitled

Nov 9th, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.70 KB | None | 0 0
  1. using System;
  2.  
  3. class CheckIfGivenBitHasValue1
  4. {
  5.     static void Main()
  6.     {
  7.         /* Write a boolean expression that returns if the bit
  8.          * at position p (counting from 0) in a given integer number v has value of 1.
  9.          * Example: v=5; p=1  false.
  10.          */
  11.  
  12.         Console.Write("v= ");
  13.         int v = int.Parse(Console.ReadLine());
  14.         Console.Write("p= ");
  15.         int p = int.Parse(Console.ReadLine());
  16.         Console.WriteLine();
  17.  
  18.         int mask = v >> p; // move p-bit at position 0
  19.         bool checkValue = !(mask % 2 == 0);
  20.  
  21.         Console.WriteLine("Does the {1} bit of {0} have value of 1?\n---> {2}",v,p,checkValue);
  22.         Console.WriteLine();
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement