Why we took Course Computer Organization?
Why we learned ALU?
Lets remember ;)
for (val = 0; val < 100000; val +=5) {
shiftX = val * 8;
myRaise = val * 2;
}
If we were to utilize bit shifting, performance would
increase up to six times. Here’s the refactored code:
for (val = 0; val < 100000; val += 5) {
shiftX = val << 3;
myRaise = val << 1;
}
Instead of multiplying by 8, we used the equivalent to shift
to the left (<<) by 3.
Each shift causes a multiplication by factors
of 2. The variable myRaise demonstrates this capability.
Shifting bits to the right (>>) is the same as dividing by factors
of 2. Of course this makes execution speed faster, but may
make it difficult for your peers to understand at a later date;
therefore it should be commented.
Reference : James McGrown
No comments:
Post a Comment