I have an unsigned char and I need to check bits 1 and 2 to find the status. What is the best way to dete开发者_JAVA百科rmine the last 2 bits?
I am attempting to perform an OR, but my results aren't correct. Any help would be appreciated. Thanks.
Example:
10101000 = off
10101001 = on
10101010 = error
10101011 = n/a
if(data_byte_data[0] | 0xfe)
//01
else if(data_byte_data[0] | 0xfd)
//10;
else if(data_byte_data[0] | 0xfc)
//11
else if(data_byte_data[0] | 0xff)
//00
I would do something like:
v = data_byte_data[0] & 0x03;
switch (v)
{
case 0: ...
case 1: ...
case 2: ...
case 3: ...
}
switch(data_byte_dat[0] & 3) {
case 0: puts("off"); break;
case 1: puts("on"); break;
case 2: puts(""error"); break;
case 3: puts("N/A");
}
switch(data_byte_data[0] & 0x0003)
{
case 0:
// 00
break;
case 1:
// 01
break;
case 2:
// 10
break;
case 3:
// 11
break;
}
switch ( val & 3 ) {
case 0: // 00
case 1: // 01
case 2: // 10
case 3: // 11
}
精彩评论