如何从msb开始获取特定位置的位值?

如何从msb开始获取特定位置的位值?

问题描述:

我以前从未在Java中使用过位,因此问题如下: 我有

I never worked with bits in Java before, so the question is the following: I have

byte a=254;

如何从msb位置开始从该字节中获取位?

How to get a bits from this byte, starting from msb position?

If position == 0 it returns 1
If position == 7 it returns 0 

提前谢谢

int getBitFromMSB(byte x,int position){
    return (x >>> (7 - position)) & 1;
}