我有 tab[110] 数组,带有随机 1 和 0 整数,所以:1001111001011110... 依此类推,直到数组结束。我正在尝试根据汉明码输出 7 行不同的位。我的循环有效,但仅适用于从位开始的组,数组中的索引为 2,4,8,16。对于第 32 个循环,会削减其中的一半(因此从 64 开始输出,而不是从 32 开始输出),并且完全跳过第 64 组。
int x=0;
int sum=0;
int pointer=0;
boolean w = true;
System.out.println("Grupy Bitow Parzystych");
for (int i=2; i<=7; i++)
{
System.out.println("n");
switch(i)
{
//case 1: pointer=1;
case 2: pointer=2;
break;
case 3: pointer=4;
break;
case 4: pointer=8;
break;
case 5: pointer=16;
break;
case 6: pointer=32;
break;
case 7: pointer=64;
break;
default: System.out.println("DEFAULT SWiTCH");
break;
}
sum=0;
x=0;
for (int p=0; p<tab.length; p++)
{
if (p==0) System.out.println("Grupa bitow: "+pointer);
if (p<=pointer-1) continue;
x++;
if (x == pointer)
{
x = 0;
w = !w;
}
if (p%20==0) System.out.println("");
if (w)
{
iterator = p+1;
System.out.print(tab[p]+"("+iterator+")"+",");
sum++;
}
if (p==tab.length-1) System.out.println("Suma bitow pary "+pointer+": "+sum);
}
}
好的,现在我明白你在做什么了......几个人。首先,将数组扩展到最少 128 个值,以使 64 位大小写正常工作。110 太小了。这是您要使用的主要算法。我冒昧地将变量名pointer
更改为parity
,并删除了一些额外的变量,如iterator
。这应该接近您的需求。检查"一次性"错误等。
switch(i)
{
//case 1: pointer=1;
case 2: parity=2;
break;
case 3: parity=4;
break;
case 4: parity=8;
break;
case 5: parity=16;
break;
case 6: parity=32;
break;
case 7: parity=64;
break;
default: System.out.println("DEFAULT SWiTCH");
break;
}
System.out.println("Grupa bitow: "+parity);
sum=0;
int index = parity - 1;
int blockSize = parity;
int printCount = 0;
while (index + blockSize < tab.length) //don't run past end of bit array
{
for (int j = 0; j < blockSize; j++)
{
if (printCount++ % 20 == 0)
System.out.println("");
System.out.print(tab[index] + "(" + (index + 1) + ")" + ",");
sum += tab[index++];
}
//printing of consecutive bits complete. Now skip the next 2|4|8|16|32|64 bits
index += blockSize;
}
System.out.println("nnSuma bitow pary " + parity + ": " + sum);
}
输出:
Grupy Bitow Parzystych
比托集团: 2
0(2),0(3),1(6),0(7),1(10),0(11),0(14),0(15),1(18),0(19),0(22),0(23),1(26),1(27),0(30),1(31),0(34),0(35),1(38),1(39),0(42),0(43),0(46),0(47),0(50),0(51),0(54),0(55),0(58),0(59),0(62),1(63),1(66),0(67),0(70),1(71),0(74),1(75),0(78),1(79),1(82),0(83),0(86),0(87),1(90),1(91),0(94),1(95),0(98),1(99),1(102),1(103),1(106),0(107),0(110),0(111),0(114),0(115),0(118),0(119),0(122),0(123),0(126),0(127),
苏玛比托帕里2:21
比托集团: 4
0(4),0(5),1(6),0(7),0(12),1(13),0(14),0(15),1(20),1(21),0(22),0(23),1(28),1(29),0(30),1(31),1(36),0(37),1(38),1(39),0(44),1(45),0(46),0(47),0(52),1(53),0(54),0(55),1(60),0(61),0(62),1(63),1(68),0(69),0(70),1(71),1(76),0(77),0(78),1(79),1(84),0(85),0(86),0(87),0(92),1(93),0(94),1(95),1(100),1(101),1(102),1(103),0(108),0(109),0(110),0(111),0(116),0(117),0(118),0(119),0(124),0(125),0(126),0(127),
苏玛比托帕里4:25
比托集团: 8
0(8),0(9),1(10),0(11),0(12),1(13),0(14),0(15),1(24),0(25),1(26),1(27),1(28),1(29),0(30),1(31),1(40),0(41),0(42),0(43),0(44),1(45),0(46),0(47),1(56),0(57),0(58),0(59),1(60),0(61),0(62),1(63),1(72),0(73),0(74),1(75),1(76),0(77),0(78),1(79),0(88),0(89),1(90),1(91),0(92),1(93),0(94),1(95),0(104),0(105),1(106),0(107),0(108),0(109),0(110),0(111),0(120),0(121),0(122),0(123),0(124),0(125),0(126),0(127),
苏玛比托帕里8:22
比托集团: 16
1(16),0(17),1(18),0(19),1(20),1(21),0(22),0(23),1(24),0(25),1(26),1(27),1(28),1(29),0(30),1(31),0(48),0(49),0(50),0(51),0(52),1(53),0(54),0(55),1(56),0(57),0(58),0(59),1(60),0(61),0(62),1(63),1(80),1(81),1(82),0(83),1(84),0(85),0(86),0(87),0(88),0(89),1(90),1(91),0(92),1(93),0(94),1(95),0(112),0(113),0(114),0(115),0(116),0(117),0(118),0(119),0(120),0(121),0(122),0(123),0(124),0(125),0(126),0(127),
苏玛比托帕里16:22
比托集团: 32
0(32),1(33),0(34),0(35),1(36),0(37),1(38),1(39),1(40),0(41),0(42),0(43),0(44),1(45),0(46),0(47),0(48),0(49),0(50),0(51),0(52),1(53),0(54),0(55),1(56),0(57),0(58),0(59),1(60),0(61),0(62),1(63),1(96),0(97),0(98),1(99),1(100),1(101),1(102),1(103),0(104),0(105),1(106),0(107),0(108),0(109),0(110),0(111),0(112),0(113),0(114),0(115),0(116),0(117),0(118),0(119),0(120),0(121),0(122),0(123),0(124),0(125),0(126),0(127),
苏玛比托帕里32:17
比托集团:64
0(64),1(65),1(66),0(67),1(68),0(69),0(70),1(71),1(72),0(73),0(74),1(75),1(76),0(77),0(78),1(79),1(80),1(81),1(82),0(83),1(84),0(85),0(86),0(87),0(88),0(89),1(90),1(91),0(92),1(93),0(94),1(95),1(96),0(97),0(98),1(99),1(100),1(101),1(102),1(103),0(104),0(105),1(106),0(107),0(108),0(109),0(110),0(111),0(112),0(113),0(114),0(115),0(116),0(117),0(118),0(119),0(120),0(121),0(122),0(123),0(124),0(125),0(126),0(127),
苏玛比托帕里64:23
尝试这样的事情:
System.out.println( "Grupy Bitow Parzystych" );
int pointer = 0, sum = 0;
for ( int i = 0; i < 7; i++ ) {
pointer = 1 << i; // 1
sum = 0;
for ( int p = 0; p < tab.length; p++ ) {
if ( p == 0 ) {
System.out.println( "nnGrupa bitow: " + pointer );
}
if ( ( ( p+1 ) & pointer ) == 0 || p == pointer ) { // 2
continue;
}
System.out.format( "%d(%d), ", tab[p], p + 1 ); // 3
sum += tab[p]; // 4
if ( p == tab.length - 1 ) {
System.out.format( "nSuma bitow pary %d: %dn" , pointer , sum );
}
}
}
编号注释:
pointer
的值是 2 的指数,因此可以使用简单的"向左滚动"运算符。(也可以使用"乘以 2"...- 用
pointer
掩蔽索引,用二进制算术 AND 运算符。 -
p
是从零开始的,您的打印索引似乎是从零开始的,因此+1
. tab[p]
的值要么是1
要么是0
,所以把它相加会得到1
值的数量......
编辑:
现在我看到你想检查汉明码了。我编辑了上面的代码。
- 第二个条件是跳过奇偶校验(
pointer
)位...