How can I use the + = operator in C to demonstrate that the same array index is used to refer to an array?

I recently studied Peter Van Der Linden's Programmers Programming Program and came across this use for the + = operator:

"If you have a complex array reference and want to show that the same index is used for both links, then:

node[i >> 3] += ~(0x01 << (i & 0x7)); 

is the way to go. "

As far as I tried, I can not understand this code. I hope someone here can explain what is actually happening, and why can it be used to demonstrate that the same index is being used?

+6
source share
2 answers

My interpretation of the quote is such that

 node[COMPLICATED_EXPRESSION] += STUFF; 

preferable

 node[COMPLICATED_EXPRESSION] = node[COMPLICATED_EXPRESSION] + STUFF; 

as it’s easier to see at a glance what this intention is.

Moreover, if STUFF also difficult, as this makes the general expression even more difficult to parse at a glance.

In the book, Van der Linden explains where the code that he shows came from:

We took this sample statement directly from some code in the operating system. Only data names have been changed to protect those responsible.

+6
source

I have not read this book, so I can only get away from your quote. I suspect he means the following:

Instead of writing:

 array[complicated_expression] = array[complicated_expression] + something_else 

(note the two references to the same array index)

You can write:

 array[complicated_expression] += something_else 

This makes it clear that the compound expression is the same in "both links."

An alternative way to do this is to use a temporary variable:

 int index = complicated_expression; array[index] = array[index] + something_else 

But this is not so concise. (This, however, is more general, as you can use it for cases when you perform an operation that does not have an X= operator.)

+4
source

Source: https://habr.com/ru/post/907339/


All Articles