How to get a bit value on particular position starting from msb?

邮差的信 提交于 2019-12-01 06:30:32

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/15630476/how-to-get-a-bit-value-on-particular-position-starting-from-msb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!