Shift Operation

N

Nishu

Harald said:
If you care about the bit pattern, you probably shouldn't be using
signed integers anyway.

Yes, since most of the processors consider 2's complement signed
integers implementation, my interest is in relation to that only. (i =
((unsigned)i) >> 4;) It will_NOT retain the intended bit pattern for
signed arithmetic. Since, as C is flexible for >> operation on signed
integers, C compilers come up >> as arithmetic right shift, maintaining
the sign of the number while shifting and in case you need padding of
0s in MSBs for specific applications, just type CAST it to unsigned !!
Thanks a lot.

-Nishu
 
P

pete

Nishu said:
Yes, since most of the processors consider 2's complement signed
integers implementation, my interest is in relation to that only. (i =
((unsigned)i) >> 4;) It will_NOT retain the intended bit pattern for
signed arithmetic. Since, as C is flexible for >> operation on signed
integers,
C compilers come up >> as arithmetic right shift, maintaining
the sign of the number while shifting and in case you need padding of
0s in MSBs for specific applications, just type CAST it to unsigned !!
Thanks a lot.

What is your reason for not wanting to use the division operator
to acheive an arithmetic result?

What's wrong with (i /= 16)
instead of (i = ((unsigned)i) >> 4)?
 
N

Nishu

What is your reason for not wanting to use the division operator
to acheive an arithmetic result?

What's wrong with (i /= 16)
instead of (i = ((unsigned)i) >> 4)?

Well, It has nothing to do with the end result, which is obviously the
same. It is more specific in relation to optimization.
"Intelligent" (Better) compilers optimize division by the powers of 2
(2, 4, 8, etc) to the simple (and faster) right shift operations using
barrel shifters on fixed point processors (say, DSPs and ARM) but some
poor compilers evoke division "function call" which is not the optimal
way of doing this kind of division. Hence, just to avoid this
possibilty, better to use shift operation and Since C language (middle
level language) support it, it is better to do it there itself. This
obviously can be extended to multiply operations too..like multiple by
7, which is nothing but (8 - 1) and so on..

-Nishu
 
C

CBFalconer

Nishu said:
Well, It has nothing to do with the end result, which is obviously
the same. It is more specific in relation to optimization.
"Intelligent" (Better) compilers optimize division by the powers of
2 (2, 4, 8, etc) to the simple (and faster) right shift operations
using barrel shifters on fixed point processors (say, DSPs and ARM)
but some poor compilers evoke division "function call" which is
not the optimal way of doing this kind of division. Hence, just to
avoid this possibilty, better to use shift operation and Since C
language (middle level language) support it, it is better to do it
there itself. This obviously can be extended to multiply operations
too..like multiple by 7, which is nothing but (8 - 1) and so on..

You should leave it to the compiler optimizer, which is aware of
the number system used and can find these optimizations much better
than you can. Write for clarity. Unless you are writing the
actual optimizer.

--
Some informative links:
< <http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>
 
F

Frederick Gotham

Nishu posted:
Well, It has nothing to do with the end result, which is obviously the
same. It is more specific in relation to optimization.
"Intelligent" (Better) compilers optimize division by the powers of 2
(2, 4, 8, etc) to the simple (and faster) right shift operations using
barrel shifters on fixed point processors (say, DSPs and ARM) but some
poor compilers evoke division "function call" which is not the optimal
way of doing this kind of division. Hence, just to avoid this
possibilty, better to use shift operation and Since C language (middle
level language) support it, it is better to do it there itself. This
obviously can be extended to multiply operations too..like multiple by
7, which is nothing but (8 - 1) and so on..


A lot of people think you should just write:

x /= 2;

, and let the compiler optimise it.

However, these same people still use static data in functions to boost
efficiency, so there's no bonafide rule for where you yourself should
optimise, and where the compiler should optimise on your behalf.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top