استخراج بیت ان ام از یک بایت
پنجشنبه, ۲۰ مرداد ۱۴۰۱، ۱۰:۰۹ ب.ظ
/**
* استخراج بیت ان ام از یک بایت
*/
public static boolean returnNthBit(byte a, int Nth) {
boolean result;
switch (Nth) {
case 0:
result = (a & 0b00000001) == 0b00000001;
break;
case 1:
result = (a & 0b00000010) == 0b00000010;
break;
case 2:
result = (a & 0b00000100) == 0b00000100;
break;
case 3:
result = (a & 0b00001000) == 0b00001000;
break;
case 4:
result = (a & 0b00010000) == 0b00010000;
break;
case 5:
result = (a & 0b00100000) == 0b00100000;
break;
case 6:
result = (a & 0b01000000) == 0b01000000;
break;
case 7:
result = (a & 0b10000000) == 0b10000000;
break;
default:
System.out.println("returnNthBit: wrong Nth. (N: " + Nth + " of max 7) (byte: " + a + ") (byteShow: "
+ Integer.toBinaryString(a) + ") (hexShow: " + String.format("0x%08X", a) + ")");
result = false;
}
return result;
}
public static int returnNthBitAsInt(byte a, int Nth) {
if (returnNthBit(a, Nth)) {
return 1;
} else {
return 0;
}
۰۱/۰۵/۲۰