What is the advantage of using >> operator over / operator? It was extensively used in the code I am maintaining.
For eg,
int width = previousWidth >> 2;
What is the advantage of using >> operator over / operator? It was extensively
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
When you want to shift a value by a certain number of bits, it’s considerably simpler to understand. For example:
That’s clearly shifting by different numbers of bits. Would you really want to express that in terms of division instead?
Now of course when what you really want is division, you should use the division operator for the sake of readability. Some people may use bitshifting for the sake of performance, but as ever, readability is more important than micro-optimization for most code. So in your case, if what’s actually desired is for
widthto bepreviousWidthdivided by 4, the code should *absolutely reflect that:I’d only use bitshifting for this after proving that the performance difference was significant.