Alex Rivera | Logout

Count leading zeroes in an Int32

Asked 2012-05-03T20:59:23.780
17

How do I count the leading zeroes in an Int32? So what I want to do is write a function which returns 30 if my input is 2, because in binary I have 000...0000000000010.

Edit
Report

1 Answer

1

Some complicated answers going on here. How about this?

private int LeadingZeroes(int value)
{
    return (32 - (Convert.ToString(value, 2).Length));
}

Though now I'm guessing there might be some issues with negative numbers and whatnot with this type of solution.

answered 2012-05-03T21:08:08.130

Your Answer