سؤال

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

byte a=254;

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

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

Thank you in advance

هل كانت مفيدة؟

المحلول

int getBitFromMSB(byte x,int position){
    return (x >>> (7 - position)) & 1;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top