settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Bitwise in C#: Check a Bit at Given Position

+3 votes

Write a Boolean expression that returns if the bit at position p (counting from 0, starting from the right) in given integer number n has value of 1.

Examples:

check bit at given position in C#

asked in C# category by user nikole
edited by user golearnweb

1 Answer

+2 votes
 
Best answer

Here is my solution to it:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


class CheckABitAtGivenPosition
{
    static void Main()
    {
        int n = int.Parse(Console.ReadLine());
        int p = int.Parse(Console.ReadLine());

        int nRightp = n >> p;
        int bit = nRightp & 1;

        bool check = (bit == 1);//by default boolean expressions are set to true
        Console.WriteLine(check);
    }
}
answered by user Jolie Ann
edited by user golearnweb
Tahnk you, JOlie!
...