[Solved] Bitwise C#: Extract Bit from Integer

  

4
Topic starter

Write an expression that extracts from given integer n the value of given bit at index p.

Examples:

extract bit from integer in C#

1 Answer
3

Here's my solution:

using System;
 
class Bitwise
{
    static void Main()
    {
        int number = int.Parse(Console.ReadLine());
        int position = int.Parse(Console.ReadLine());
 
        int numberRightposition = number >> position;
        int bit = numberRightposition & 1;
 
        Console.WriteLine(bit);
    }
}
Share: