Advertisement
Teodor92

BitPosition

Oct 6th, 2012
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.59 KB | None | 0 0
  1. /*Write a boolean expression that returns if the bit at
  2.  * position p (counting from 0) in a given
  3.  * integer number v has value of 1. Example: v=5; p=1  false.
  4.  */
  5.  
  6. using System;
  7.  
  8. class BitOnPositon
  9. {
  10.     static void Main()
  11.     {
  12.         int position = 4;
  13.         int bitType = 1;
  14.         int number = 1567;
  15.         bool isGivenBit = false;
  16.         int newNumber = (number >> position) & 1;
  17.         if (newNumber == bitType)
  18.         {
  19.             isGivenBit = true;
  20.         }
  21.         Console.WriteLine("The bit on position {0} is {1} : {2}", position, bitType, isGivenBit);
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement