Luce
Luce

Reputation: 62

One Operation in C++

I have been recently reading about space complexities of algorithms.

And I wondering how does C++ define "one operation"

So, 2+3 = 5 is treated as one operation or is it:

2+3 = (010+ 011)b2 = (101)b2 =5

And thus leading to 3 operations.

This question arises from the curiosity about bit shifting since it is more basic than the addition of multiple bits. And having read that complexity of bit-shifting depends on the language used. I wanted to ask how does C++ define "one operation".

Upvotes: 1

Views: 122

Answers (1)

AIWalker
AIWalker

Reputation: 184

Different compilers or compiler options / optimisations can affect how such statements are handled, leading to there being no consistent definition of 'one operation' in c++. If you wanted to know how a particular piece of code is executed, you could put it into compilerexplorer/godbolt and setting the compiler & settings and looking at its assembly output.

That said, the 'number of operations' in this respect is not the point of consideration in algorithmic complexity for either time or space. They are defined it is in regards to the input of a function - space complexity represents the amount of memory taken to perform the operation and time represents how long the function takes to execute. 3 + 5 would be represented as O(1) space (and time, but that's not what you were asking about) complexity because the amount of memory taken to perform it is constant (2 or 3 registers usually, 2 inputs and an output, which depending on other considerations may be in the same or a different register).

Upvotes: 1

Related Questions