0

I wan't to be able to increase the value of content in my byte array.

For example if in my array I have got the value 1100001 what is an effective way of incrementing this value so that the contents of the array would show 1100010.

I thought that it would be something like the following but it doesn't seem to work.

ByteArray[i] = ByteArray[i]++;

Thanks in advance

2

3 Answers 3

8
ByteArray[i] = ByteArray[i]++;

This is equivalent to:

byte temp = ByteArray[i];
ByteArray[i]++;
ByteArray[i] = temp;

In other words, you're increasing the value, then replacing it with the old value.

What you want is either

ByteArray[i]++;

or

ByteArray[i] = ByteArray[i] + 1;
Sign up to request clarification or add additional context in comments.

2 Comments

or ByteArray[i] += 1; as long as we're including everything
Thanks for the help and quick response
2

x++ returns the original value of x.
You're then assigning the original value back to the array slot.

2 Comments

this is right, but OP seems to be a beginner. please explain it more
And it doesn't answer author's question, nor solve his problem.
1

You should read into increment operators, there is ++x and x++. Both increment, but ++x returns x+1, while x++ returns x.

This in your case, you set ByteArray[i] to ByteArray[i], the increment is impatiently overwritten.

Use ByteArray[i]++; instead.

And whenever you use the return of such an increment, think about which variant you use.

1 Comment

Thanks for the help and quick response

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.