0

I have tab[110] array with random 1 and 0 ints, so: 1001111001011110... and so on untill end of array. I am trying to output 7 different rows of bits according to Hamming Code. My loop works but only for groups starting from bits, which index in array are 2,4,8,16. For 32th loop cuts half of them (so start output from 64, not from 32) and group 64th is skipped completely.

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);
            }
        }
ScriptyChris
  • 639
  • 4
  • 16
  • 48

2 Answers2

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

swingMan
  • 732
  • 1
  • 6
  • 17
  • iterator is defined as "int iterator=0;". You didn't get me good enough. I want to output bits but including skip each X bits (when x is 1,2,4,8,16,32 or 64), so i want to start in example from 32th bit and output 32 bits, then skip 32 bits and again output 32 bits. That works for gourp of bits from 1-16, but as i said when it comes to 32 group and then 64, it doesn't output it properly. In your output above 32th group of bits should skip bits from 65 untill 97 (so 32 bits), and then output from 98 till end of array[110]. In 64th group it's good. – ScriptyChris Jun 14 '15 at 08:29
  • I hope you understood me know - its not only about counting those bits, but also output and skip repetitively certain number of them (this number refers to group of bits i want to output). – ScriptyChris Jun 14 '15 at 08:30
0

Try something like this:

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 );
    }
  }
}

The numbered comments:

  1. The values of your pointer are exponents of 2, so a simple "roll to the left" operator could be used. (Also a "multiply by 2" could be used...)
  2. Masking the index with pointer, with binary arithmetic AND operator.
  3. p is zero based, your printed indexes seem to be one based, hence the +1.
  4. The value of tab[p] is either 1 or 0, so summing it up would give the number of 1 values...

Edit:

Now i see you want to check Hamming code. I edited the code above.

  1. The second condition is for skipping the parity (pointer) bits...
Usagi Miyamoto
  • 6,196
  • 1
  • 19
  • 33