Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- //Using bitwise operators, write an expression for finding the value of the bit #3 of a given unsigned integer.
- //The bits are counted from right to left, starting from bit #0. The result of the expression should be either 1 or 0. Examples:
- class PrintBitInGivenPosition
- {
- static void Main()
- {
- Console.Write("n = ");
- int n = int.Parse(Console.ReadLine());
- Console.WriteLine("p = 3");
- int p = 3;
- int mask = (int)Math.Pow(2, p);
- string binaryMask = Convert.ToString(mask, 2); //binary of n
- string binaryN = Convert.ToString(n, 2); //binary of n
- int position = mask & n;
- if (position == 0)
- {
- Console.WriteLine("Bit of position {0} = 0", p);
- }
- else
- {
- Console.WriteLine("Bit of position {0} = 1", p);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement