User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 423,460 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,814 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums

C++ Performance Tips

Join Date: May 2005
Location: India
Posts: 65
Reputation: vicky_dev is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
vicky_dev's Avatar
vicky_dev vicky_dev is offline Offline
Junior Poster in Training

A General Performance Tip - Arithmetic involving powers of 2

  #15  
Dec 15th, 2006
Multiplication, division and modulus operations on powers of 2 can be made much faster using bitwise operators.

1. Modulo
Finding the modulus is particularly expensive. This operation can be performed mush faster using the & operator, because
a % n == a & (n-1) where n is power of 2.

2. Division
Division by a power of 2 can done faster using the >> operator.
a / n == a >> m, where n = 2^m

3. Multiplication
This is similar to division, except, use left shift << operator
a * n == a <<m , n = 2^m
Reply With Quote  
All times are GMT -4. The time now is 3:03 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC