Dave's Brain

Browse - Programming Tips - Can I use bitwise operators to speed up multiply and divides by 2?

Date: 2015aug14
Language: C/C++

Q.  Can I use bitwise operators to speed up multiply and divides by 2?

A.

Dividing by 2 can be replaced by a right shift

i / 2 -> i>>1 i / 4 -> i>>2
Multiplying by 2 can be replaced by a left shift
i * 2 -> i<<1 i * 4 -> i<<2
Checking for odd or even
i % 2 -> i & 0x01

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.