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
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;
x++
returns the original value of x
.
You're then assigning the original value back to the array slot.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.