3

I am learning c# and I want to find out whether the 3rd bit in an integer is a 1 or 0.

So how I am approaching this is to convert the int to a binary. Then convert the string to an array. convert the array of string to an array of ints and slice on the 3rd bit and check value.

I cannot quite get this to happen. This is where I am at. I am using this example from SO to convert to array

using System;
using System.Text;

class Expression
{
    static void Main()
    {
        int number = 3;
        string binValue = Convert.ToString(number, 2);

        char[] array = binValue.ToCharArray();
        array<int> list = new List<int>();
        for (int i = 0; i < array.Length; i++)
        {
            list.add(value);
        }

        int[] binArr = list.ToArray();
        binArr[2] == 1? "Yes" : "No";
    }
}
Community
  • 1
  • 1
sayth
  • 6,696
  • 12
  • 58
  • 100
  • 1
    There is a far easier way to do this: by using a bit mask. Check here for example: http://stackoverflow.com/questions/3261451/using-a-bitmask-in-c-sharp. Or here: http://stackoverflow.com/questions/14479981/how-do-i-check-if-bitmask-contains-bit – Ronald Wildenberg Jul 14 '14 at 12:31

2 Answers2

3

That's entirely the wrong way to do it; just perform binary arithmetic:

bool bit3IsSet = (number & 4) != 0;

where the 4 is bit 3; you could also use:

int bitNumber = 3; // etc
bool bitIsSet = (number & (1 << (bitNumber-1))) != 0;

in the general-case.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • That's much easier. Oh well I learnt a lot going the wrong way anyway. – sayth Jul 14 '14 at 12:36
  • I am getting True for any number value. using System; using System.Text; class Expression { static void Main() { int number = 100; string binValue = Convert.ToString(number, 2); System.Console.WriteLine(binValue); bool bit3IsSet = (number & 4) != 0; System.Console.WriteLine(bit3IsSet); } } #Output C:\Users\Sayth\Documents\Scripts>Expression 1100100 True – sayth Jul 14 '14 at 12:45
  • @sayth and bit 3 is set for 100 ;p Try 96 or 104 – Marc Gravell Jul 14 '14 at 12:51
  • I was counting the wrong way left to right not right too left. – sayth Jul 14 '14 at 12:57
  • 1
    @sayth ah, I see; traditionally, you usually count bit numbers right to left, i.e. starting with the LSB. You can do it the other way, but it will be harder; first you'd need to find the highest set bit, etc. – Marc Gravell Jul 14 '14 at 13:12
  • Incidentally, the [current plan](http://roslyn.codeplex.com/wikipage?title=Language%20Feature%20Status) is for Roslyn to support binary literals. This would allow you to replace `bool bit3IsSet = (number & 4) != 0;` with `bool bit3IsSet = (number & 0b00000100) != 0;`. Whether that is actually more readable depends on how it's being used. – Brian Jul 14 '14 at 13:47
1

You do not need an array conversion: use String.Substring() Function (re: http://msdn.microsoft.com/en-us/library/system.string.substring%28v=vs.110%29.aspx ) to check the value of third bit (in your case: binValue.Substring(2,1); in a short form it could be written like the following:

bool _bit3 = (Convert.ToString(number, 2).Substring(2,1)=="1")? true:false;
Alexander Bell
  • 7,842
  • 3
  • 26
  • 42