1+1 and 1+100, theoretically, is there any execution speed difference?

I mean, How does the computer do computing? 1+100 is executing like 1+1+1+1...repeatling 100 times?


how about 2*2 and 2*100? or 1*1 and 1*100?
Well, since the computer knows only bits it combines those bits in that fashion that it results int a addition or multiplication and so on
Well, since the computer knows only bits it combines those bits in that fashion that it results int a addition or multiplication and so on


so you mean, 1+100 and 1+1 will not be any different?
basically yes. it depends on how many bits a microprozessor can actually process at once. Currently there're processors from 8 - 64 bits
Strictly speaking, though, there is no general answer. A CPU that does addition by incrementing a number n times is not unthinkable. It all depends on how the logic is implemented. For a proper answer, you need to check your CPU's documentation.

In the particular case of modern x86 implementations, ADD reg32,reg32 takes 1 clock regardless of the contents of src and dst.
If you're dealing with bignum classes, then most likely the + and += operators utilize the ++ operator in a loop. Although some are more mathematically beutiful and do it in a somewhat more efficient way. There are a lot of the other looped ++ bignum classes out there, though, because there are a lot of beginners trying to do it the simpler way. So, in the case of a bignum class, do like all teachers do and get to know your class! ;)

It may also be noteworthy to consider linked list classes, such as the std::list class, where with iterators, you can only use ++it or it++, there is no it + x or it += x operator for list iterators like there are for vector iterators. This leads to iterating being less effecient the further you have to iterate.
Last edited on
Modern computers operate on things other than bits (1s and 0s). They have circuits that allow them to operate on integers and real numbers. They essentially have circuitry to add any two numbers.
Last edited on
abellia wrote:
I do not understand what a computer is or how it works.
abellia wrote:
Modern computers operate on things other than bits (1s and 0s).

!!! are you operating an analog computer, perchance?

abellia wrote:
They essentially have circuitry to add any two numbers.

this is a scary misconception
Last edited on
helios wrote:
...... A CPU that does addition by incrementing a number n times....


That really sounds illogical to me though..
I believe they use bitwise operators.

Example:
1
2
int a = 3; // a=0000011
a = a<<1; // a=0000110 

So this actually multiplied a (3) by 2 to give us 6. This also works for addition and subtraction using "bitwise and", etc.

EDIT: So to be clear - no, it doesn't take any more processing power to do 1+1 vs 1+100 or 10000+10000, etc.
Last edited on
closed account (S6k9GNh0)
http://en.wikipedia.org/wiki/Central_processing_unit
Topic archived. No new replies allowed.