bos*_*kop 3 java hex bit-manipulation
移...
我要做点什么,这会扭曲我的想法.
我得到一个十六进制值为String(例如:"AFFE")并且必须决定,如果设置了字节1的第5位.
public boolean isBitSet(String hexValue) {
//enter your code here
return "no idea".equals("no idea")
}
Run Code Online (Sandbox Code Playgroud)
任何提示?
问候,
Boskop
最简单的方法是转换String为int,并使用位算术:
public boolean isBitSet(String hexValue, int bitNumber) {
int val = Integer.valueOf(hexValue, 16);
return (val & (1 << bitNumber)) != 0;
} ^ ^--- int value with only the target bit set to one
|--------- bit-wise "AND"
Run Code Online (Sandbox Code Playgroud)