Hi, if we take for example 32bits: 1111 1000 0000 1000 0001 1111 1100 0000, how does it become (24bits) 1011 1110 0011 0110 1111 0110 after RLE is applied?

Why/how does 1011 represent the 5 1's at the start of the 32bit number? I'm trying to understand the concept and was trying to figure out how to do it by hand before attempting to write a text compression program.

Recommended Answers

All 6 Replies

Why/how does 1011 represent the 5 1's at the start of the 32bit number? I'm trying to understand the concept and was trying to figure out how to do it by hand before attempting to write a text compression program.

I've never heard of run-length encoding, but 101 is 5 in binary, so maybe the first three bits are a binary number that specifies the number of repetitions of the digit specified in the fourth bit? Or something like that?

While RLE is pretty straightforward, the encoding of the result can vary. So it's difficult to answer your question without knowing what kind of encoding is being performed to achieve that result. Where did you get the example?

The right most digit is the 1 or 0 that is being compressed, and the remaining 3 digits to the left code how many of those 1's or 0's there are.

1011 = 101 1's - i.e., 5 ones.
1110 = 111 0's - i.e., 7 zeros.

While RLE is pretty straightforward, the encoding of the result can vary. So it's difficult to answer your question without knowing what kind of encoding is being performed to achieve that result. Where did you get the example?

I got the example from my notes.

The right most digit is the 1 or 0 that is being compressed, and the remaining 3 digits to the left code how many of those 1's or 0's there are.

I see, so the first 3 are binary numbers of how many times they appear.

I tried to make a simple program that takes in an input that looks something like this: 'aabbc' and the output being: 'a2b2c1', but right now it's outputting a2b2c2. I tried it on paper and it seemed fine, but I can't figure out why it's outputting a2b2c2.

    string textData;
    readFile >> textData;
    string myArray[5];
    vector<string> vec;
    vector<int> numCount;

    for (int i = 0; i < 5; i++) {
      myArray[i] = textData[i];
   }

    int count = 1;
    for (int i = 0; i < 5; i++) {


      if (myArray[i] == myArray[i+1]) {
        count++;
      }
      if (myArray[i] != myArray[i+1]){
          vec.push_back(myArray[i]);
          numCount.push_back(count);
          count = 1;
      }
    }

I think you need to change line 15 to

if ( i < 4 && myArray[i] == myArray[i+1])

The way you have it now the last time you go through the loop you are going past the end of the array and it will cause undefined behavior.

Hmm, still getting the same output after adding 'i<4' to it. I tried changing the input to 'abbbc' and the output ended up being 'a1b1c1' for some odd reason. Shouldn't count increment when the next iteration begins? (when i=1, b == b so shouldn't count increment to 2 and revert back to 1 as it reaches c?)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.