0

我有随机 1 和 0 整数的 tab[110] 数组,所以: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);
            }
        }
4

2 回答 2

0

OK, now that I see what you're doing...a couple commnents. First, expand your array to 128 values minimum to make the 64 bit case work. 110 is too small. Here's the main algorithm you want to use. I took the liberty to change the variable name pointer to parity, and removed some extra variables like iterator. This should be close to what you need. Check for "one off" errors and such.

        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("\n\nSuma bitow pary " + parity  + ": " + sum);
    }

Output:

Grupy Bitow Parzystych

Grupa bitow: 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),

Suma bitow pary 2: 21

Grupa bitow: 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),

Suma bitow pary 4: 25

Grupa bitow: 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),

Suma bitow pary 8: 22

Grupa bitow: 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),

Suma bitow pary 16: 22

Grupa bitow: 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),

Suma bitow pary 32: 17

Grupa bitow: 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),

Suma bitow pary 64: 23

于 2015-06-14T00:57:16.037 回答
0

尝试这样的事情:

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( "\n\nGrupa 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: %d\n" , pointer , sum );
    }
  }
}

编号评论:

  1. 您的值pointer是 2 的指数,因此可以使用简单的“向左滚动”运算符。(也可以使用“乘以 2”...)
  2. pointer用, 用二进制算术 AND 运算符屏蔽索引。
  3. p是从零开始的,您打印的索引似乎是从一开始的,因此+1.
  4. 的值tab[p]1or 0,因此将其相加将给出1值的数量...

编辑:

现在我看到你想检查汉明码。我编辑了上面的代码。

  1. 第二个条件是跳过奇偶校验 ( pointer) 位......
于 2017-01-20T23:08:16.987 回答