一次屏蔽掉两位
Masking out two bits at the time
一般来说,我很难处理位和字节,但我有一个作业,当时我必须屏蔽掉 n 个位,但我不知道该怎么做.假设我有二进制数 1011010,我想要最高有效的 2 位 (10)。然后我想要接下来的 2 位 (11),然后是接下来的 2 位,依此类推,直到处理完所有位。所以掩码首先是 1100000,然后是 0011000,等等。我知道我可以通过移动 n 个索引并减去 1 来获得最低有效位,但是我该怎么做呢?
我有 int mask = ((1 << NUM_BIT)-1) * (NUM_BIT*2);,其中 NUM_BIT 是位数屏蔽(当前为 2)但这不适用于超过 4 位的字节,而且我不知道如何将 1 位向下移动...
一个解决方案可能是这样的:
int i = 4; // number of bits you want to mask
int mask = ~((1 << 32 - i) - 1); // will return 11110000000000000000000000000000
然后你只需 "move" 右边的位,直到 mask
是 0
。
示例:
while (mask != 0) {
String replace = String.format("%32s", Integer.toBinaryString(mask)).replace(' ', '0'); // just to be more clear
System.out.println(replace);
mask = mask >>> i;
}
输出:
11110000000000000000000000000000
00001111000000000000000000000000
00000000111100000000000000000000
00000000000011110000000000000000
00000000000000001111000000000000
00000000000000000000111100000000
00000000000000000000000011110000
00000000000000000000000000001111
00000000000000000000000000000000
如果你想要两位,只需将 i
替换为 2;
示例代码:
public class TestMain {
public static void main(String[] args) {
int inputInt = Integer.parseInt("1011010", 2);
twoBits(inputInt);
}
public static void twoBits(int inputInt) {
int mask = 3<<30;
while(mask != 0 && (inputInt & mask)==0) {
mask >>>= 2;
}
System.out.println(String.format("%32s", Integer.toBinaryString(inputInt)).replace(' ', '0'));
do {
System.out.println(String.format("%32s", Integer.toBinaryString(inputInt & mask)).replace(' ', '0'));
mask >>>= 2;
}
while(mask != 0 && (inputInt & mask)!=0);
}
}
输出:
00000000000000000000000001011010
00000000000000000000000001000000
00000000000000000000000000010000
00000000000000000000000000001000
00000000000000000000000000000010
一般来说,我很难处理位和字节,但我有一个作业,当时我必须屏蔽掉 n 个位,但我不知道该怎么做.假设我有二进制数 1011010,我想要最高有效的 2 位 (10)。然后我想要接下来的 2 位 (11),然后是接下来的 2 位,依此类推,直到处理完所有位。所以掩码首先是 1100000,然后是 0011000,等等。我知道我可以通过移动 n 个索引并减去 1 来获得最低有效位,但是我该怎么做呢?
我有 int mask = ((1 << NUM_BIT)-1) * (NUM_BIT*2);,其中 NUM_BIT 是位数屏蔽(当前为 2)但这不适用于超过 4 位的字节,而且我不知道如何将 1 位向下移动...
一个解决方案可能是这样的:
int i = 4; // number of bits you want to mask
int mask = ~((1 << 32 - i) - 1); // will return 11110000000000000000000000000000
然后你只需 "move" 右边的位,直到 mask
是 0
。
示例:
while (mask != 0) {
String replace = String.format("%32s", Integer.toBinaryString(mask)).replace(' ', '0'); // just to be more clear
System.out.println(replace);
mask = mask >>> i;
}
输出:
11110000000000000000000000000000
00001111000000000000000000000000
00000000111100000000000000000000
00000000000011110000000000000000
00000000000000001111000000000000
00000000000000000000111100000000
00000000000000000000000011110000
00000000000000000000000000001111
00000000000000000000000000000000
如果你想要两位,只需将 i
替换为 2;
示例代码:
public class TestMain {
public static void main(String[] args) {
int inputInt = Integer.parseInt("1011010", 2);
twoBits(inputInt);
}
public static void twoBits(int inputInt) {
int mask = 3<<30;
while(mask != 0 && (inputInt & mask)==0) {
mask >>>= 2;
}
System.out.println(String.format("%32s", Integer.toBinaryString(inputInt)).replace(' ', '0'));
do {
System.out.println(String.format("%32s", Integer.toBinaryString(inputInt & mask)).replace(' ', '0'));
mask >>>= 2;
}
while(mask != 0 && (inputInt & mask)!=0);
}
}
输出:
00000000000000000000000001011010
00000000000000000000000001000000
00000000000000000000000000010000
00000000000000000000000000001000
00000000000000000000000000000010