views:

320

answers:

7

I have some old code like this:

private int ParseByte(byte theByte)
{
        byte[] bytes = new byte[1];
        bytes[0] = theByte;
        BitArray bits = new BitArray(bytes);

        if (bits[0])
            return 1;
        else
            return 0;
}

It's long and I figured I could trim it down like this:

private int ParseByte(byte theByte)
{
         return theByte >> 7;
}

But, I'm not getting the same values as the first function. The byte either contains 00000000 or 10000000. What am I missing here? Am I using an incorrect operator?

A: 

Perhaps the first function should check for bits[7] ?

Ankur Goel
The first function gets what I want, the second one is not working.
scottm
does the following work: return ((int) theByte == 128) ? 1 : 0 ;
Ankur Goel
A: 

You have an extra zero in your binary numbers (you have 9 digits in each). I'm assuming that's just a typo.

Are you sure you're doing your ordering correctly? Binary is traditionally written right-to-left, not left-to-right like most other numbering systems. If the binary number you showed is property formatted (meaning that 10000000 is really the number 128 and not the number 1) then your first code snippet shouldn't work and the second should. If you're writing it backwards (meaning 10000000 is 1, not 128), then you don't even need to bitshift. Just AND it with 1 (theByte & 1).

In fact, regardless of the approach a bitwise AND (the & operator) seems more appropriate. Given that your first function works and the second does not, I'm assuming you just wrote the number backwards and need to AND it with 1 as described above.

Adam Robinson
scottm
A: 

According to a user on Microsoft's site the BitArray internally stores the bits into Int32s in big endian in bit order. That could cause the problem. For a solution and further info you can visit the link.

merkuro
+4  A: 

The problem is that, in the first function, bits[0] returns the least significant bit, but the second function is returning the most significant bit. To modify the second function to get the least significant bit:

private int ParseByte(byte theByte)
{
    return theByte & 00000001;
}

To modify the first function to return the most significant bit, you should use bits[7] -- not bits[0].

Dustin Campbell
so, the hex editor I used to look at the data displayed it as 10000000. I guess it displayed the lsb first? and I got lucky with my confusion in the BitArray?
scottm
@scott2012: I can't speak for your hex editor, but that's how BitArray works -- the least-significant bit is at index 0. You can verify this with a short bit of code:var bits = new BitArray(new byte[] { 0xf0 }); // 11110000for (int idx = 0; idx <= 7; idx++) Console.WriteLine("{0}: {1}", idx, bits[idx]);
Dustin Campbell
+3  A: 

The equivalent function to the first snipet is:

return theByte & 1 == 1

In the second snipet you were chechink the most significative bit and in the first snipet the least significant.

AlbertEin
A: 

1st The first function does not work as it tries to return a string instead of an int.

But what you might want is this:

    private static int ParseByte(byte theByte)
    {
        return theByte & 1;
    }

However you might also want this:

    private static string ParseByteB(byte theByte)
    {
        return (theByte & 1).ToString();
    }
Martin Brown
Fixed the the typo
scottm
+1  A: 

Do you want to return int or string? Anyway - you can use modulo:

return theByte % 2 == 0 ? "0" : "1"

OK, you edited ... and want to return int

A word to your shifting operation: you would have to use << instead of >>. But this returns (when you cast to byte instead of int) 0 or 128 and not 0 or 1. So you could rewrite your second solution as:

return (byte)(theByte << 7) == 128 ? 1 : 0;

But the other answers contain really better solutions than this.

tanascius