Java 从一条短裤中获得8条短裤
我有一个数组Java 从一条短裤中获得8条短裤,java,bit-manipulation,bitwise-operators,short,Java,Bit Manipulation,Bitwise Operators,Short,我有一个数组a的缩写如下: a = [ 16748, 26979, 25888, 30561, 115 ] //in decimal a = [ 0100000101101100, 0110100101100011, 0110010100100000, 0111011101100001, 0000000001110011 ] //in binary public s
a
的缩写如下:
a = [ 16748,
26979,
25888,
30561,
115
] //in decimal
a = [ 0100000101101100,
0110100101100011,
0110010100100000,
0111011101100001,
0000000001110011
] //in binary
public static short[] createSequencesOf2Bit(short[] a) {
int length = (16/2) * a.length;
short[] b = new short[length];
for(int i = 0; i < a.length; i++) {
int j = 0;
for(short c = (16 - 2); c >= 0; c -= 2) {
short shift = (short)(a[i] >> c);
b[j] = (short)(shift & 0x3);
j++;
}
}
for(int i = 0; i < b.length; i++) {
System.out.println("b[" + i + "]: " + b[i]);
}
return b;
}
b = [0 0 0 0 1 3 0 3 0 0 0 0 ... 0]
我想获得另一个由表示每个short的每对位组成的short数组b
。
(很难解释,但通过一个例子很容易理解)
因此,对于数组a
我将获得数组b
:
b = [ 01, 00, 00, 01, 01, 10, 11, 00,
01, 10, 10, 01, 01, 10, 00, 11,
01, 10, 01, 01, 00, 10, 00, 00,
01, 11, 01, 11, 01, 10, 00, 01,
00, 00, 00, 00, 01, 11, 00, 11
]
在伪代码中,我想到了这样做:
int lenght = (16/2) * a.length; //16*2 because I had short (16 bit) and I want sequences of 2 bit
short[] b = new short[length]; //I create the new array of short
int j = 0; //counter of b array
foreach n in a { //foreach short in array a
for(int i = 16 - 2; i > 0; i-2) { //shift of 2 positions to right
b[j] = ( (n >> i) & ((2^2)-1) ); //shift and &
j++;
}
}
我试图将这个伪代码(假设它是正确的)翻译成Java:
这是完全错误的。事实上,我应该得到:
b = [ 01, 00, 00, 01, 01, 10, 11, 00,
...
] //in binary
b = [ 1, 0, 0, 1, 1, 2, 3, 0,
...
] //in decimal
有人能帮我吗?
非常感谢
真奇怪。如果我只考虑A中的第一个短,而我打印B,则得到:
public static short[] createSequencesOf2Bit(short[] a) {
int length = (16/2) * a.length;
short[] b = new short[length];
//for(int i = 0; i < a.length; i++) {
int j = 0;
for(short c = (16 - 2); c >= 0; c -= 2) {
short shift = (short)(a[0] >> c);
b[j] = (short)(shift & 0x3);
j++;
}
//}
for(int i = 0; i < b.length; i++) {
System.out.println("b[" + i + "]: " + b[i]);
}
return b;
}
b = [1 0 0 1 1 2 3 0 0 0 0 0 ... 0]
publicstaticshort[]createSequencesOf2Bit(short[]a){
整数长度=(16/2)*a.长度;
短[]b=新短[长度];
//for(int i=0;i=0;c-=2){
短移位=(短)(a[0]>>c);
b[j]=(短)(移位和0x3);
j++;
}
//}
for(int i=0;i
但如果我打印这个:
a = [ 16748,
26979,
25888,
30561,
115
] //in decimal
a = [ 0100000101101100,
0110100101100011,
0110010100100000,
0111011101100001,
0000000001110011
] //in binary
public static short[] createSequencesOf2Bit(short[] a) {
int length = (16/2) * a.length;
short[] b = new short[length];
for(int i = 0; i < a.length; i++) {
int j = 0;
for(short c = (16 - 2); c >= 0; c -= 2) {
short shift = (short)(a[i] >> c);
b[j] = (short)(shift & 0x3);
j++;
}
}
for(int i = 0; i < b.length; i++) {
System.out.println("b[" + i + "]: " + b[i]);
}
return b;
}
b = [0 0 0 0 1 3 0 3 0 0 0 0 ... 0]
publicstaticshort[]createSequencesOf2Bit(short[]a){
整数长度=(16/2)*a.长度;
短[]b=新短[长度];
for(int i=0;i=0;c-=2){
短移位=(短)(a[i]>>c);
b[j]=(短)(移位和0x3);
j++;
}
}
for(int i=0;i
我怀疑主要的问题是您想要的不是&0x11
,而是&0x3
。请记住0x
表示十六进制,因此0x11
给出的是数字17,而不是3。或者您可以编写0b11
,以二进制形式获取11
此外,正如注释中指出的,循环条件应该是
c>=0
,而不是c>0
,我怀疑主要的问题是您想要的不是&0x11
,而是&0x3
。请记住0x
表示十六进制,因此0x11
给出的是数字17,而不是3。或者您可以编写0b11
,以二进制形式获取11
此外,正如注释中指出的,循环条件应该是
c>=0
,而不是c><0
,其次循环条件是错误的:c>0
,应该是`c>=0`。好的,多亏了这两个条件,现在我得到了b=[0 0 0 0 0 0 1 0 3 0 0 0 0…0]
@Step您的问题是因为您为数组中的每个元素重置了j
,所以只能填充数组中的前八个点。@Step:moveint j=0第一个循环之前的代码>第二个循环条件是错误的:c>0
,应该是'c>=0`。好的,多亏了这两个条件,但现在我得到了b=[0 0 0 0 0 0 0 1 0 3 0 0 0 0 0 0…0]
@Step你的问题是因为你为数组中的每个元素重置了j
,所以只能填充数组中的前八个点。@Step:moveint j=0代码>在第一个循环之前。