Glen654
Glen654

Reputation: 1152

What does this c# bitshifting method do?

This method is pretty much copied from a java program, but I have worries it doesn't work as intended in c# if ID is a byte, what does this do?

public int getBit(int position)
    {
        return (ID >> (position - 1)) & 1;
    }

Upvotes: 0

Views: 108

Answers (2)

Steve
Steve

Reputation: 216293

Extract from the ID the bit at the position passed.
Position should be 1-8
Returns the bit value (0-1)

For example:

ID = 128;  // 10000000
getBit(8); // returns 1

ID = 127;  // 01111111
getBit(8); // returns 0

Upvotes: 1

spender
spender

Reputation: 120450

Returns non-zero if the bit at (position-1) is 1, otherwise returns 0

Upvotes: 1

Related Questions