0

I'm trying to change a for loop's start variable to use an if statement instead. However, they don't seem to be equivalent..

I have a loop that looked like:

int K = 0;
std::uint32_t CheckSum = 0;
const std::uint8_t* BuffPos = static_cast<const std::uint8_t*>(Data);
int Start = Height < 12 ? 1 : 12;

for (std::size_t I = Start; I < Height; ++I)
{
    for (std::size_t J = 0; J < Width; ++J, ++K)
    {
        BuffPos += 3;  //skip RGB and move to alpha pixel.
        CheckSum += *(BuffPos++);  //Checksum = count of alpha pixels.
    }
}

std::cout<<CheckSum;

However, I don't want my loop to start with I = Start. I'd rather do that in an if statement. I tried the following:

int K = 0;
std::uint32_t CheckSum = 0;
const std::uint8_t* BuffPos = static_cast<const std::uint8_t*>(Data);
int Start = Height < 12 ? 1 : 12;

for (std::size_t I = 0; I < Height; ++I)
{
    for (std::size_t J = 0; J < Width; ++J, ++K)
    {
        BuffPos += 3; //Skip RGB and move to alpha pixel.

        if (I >= Start)  //The if statement.
            CheckSum += *(BuffPos++);  //Checksum = count of alpha pixels.
    }
}

std::cout<<CheckSum;

However, the second version with the if statement prints a totally different number than the first for the exact same bitmap.

Is there a reason why it does this? How can I fix it? I can't see why it would be any different :S

3 Answers 3

3

As you noticed, it is no longer the same.

Sure, you segregated the checksum bit, but now you are incrementing BufPos a different number of times. Your loop is now iterating more times, so BuffPos has a different value once you start creating the checksum than it did in the first version.

Look at it this way; the first version starts accumulating the checksum at BuffPos + 3. The second version begins accumulating the checksum from BuffPos + (Start * Width) + 3.

Honestly, the first version is better. I can't tell you which one is correct, but assuming the first version is (iterating from Start) then why introduce a branch at all? It just muddles up the code.

If you want the second version to be the same as the first you'll need to initialize BuffPos to Data + Start * Width or place both statements within the if. Of course, if you do that, you're just doing nothing at all until I == Start, and that should tell you something.

Sign up to request clarification or add additional context in comments.

9 Comments

The branch was so that the first part would calculate a checksum of all pixels and within the branch, I'd calculate a checksum of all except the first 12 rows.
@CantChooseUsernames: then why are you surprised that it now gives a different value? You changed the checksum. Just set BuffPos to point to the correct place to begin with (as I showed).
I was trying to use the two loops to calculate two checksums rather than looping the image with 4 loops (one checksum starting at row 12. Another starting at row 0).
@CantChooseUsernames: Ok, but your example code doesn't do that. We could only answer based on what you show us.
"I can't tell you which one is correct." deserves a -1 but since this is a recreational question, I'll refrain!
|
1

how's this version?

int K = 0;
std::uint32_t CheckSum = 0;
const std::uint8_t* BuffPos = static_cast<const std::uint8_t*>(Data);
int Start = Height < 12 ? 1 : 12;

for (std::size_t I = 0; I < Height; ++I)
{
    if (I < Start)  //The if statement.
        continue; 
    for (std::size_t J = 0; J < Width; ++J, ++K)
    {
        BuffPos += 3; //Skip RGB and move to alpha pixel.
        CheckSum += *(BuffPos++);  //Checksum = count of alpha pixels.
    }
}

std::cout<<CheckSum;

7 Comments

"how's this version?" - pretty useless. I know it's what the OP is asking for, but in this case, the OP wants the wrong solution.
hehe... who am i to deny the OP's desire. did you undownvote? i was mainly wondering what people would do in response. this was a rhetorical answer if there's such a thing. and also a sociological experiment. props for the undownvote - you pass :)
@EdS. i spoke too soon! either somebody else downvoted, or you unundownvoted.
I downvoted, removed it... and then downvoted again. I was a bit conflicted because it's technically what the OP was asking for. However, it's brain-dead code. Why introduce cycles which do absolutely nothing? I would refactor that code in a second, so I don't think it's a good answer.
@EdS. well, there's might still be time to ununundownvote, lol
|
0

Both statements(Buffpos and cheksum) should come under if statement .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.