View Single Post
Old 08-05-2003, 04:16 PM   #14 (permalink)
npa
Code Monkey
 
Join Date: Jul 2003
Location: canada
Posts: 82
npa is on a distinguished road
"i want a faster way to divide when my numbers are powers of 2":
Code:
int a = x / (power of 2) // is equiv to
int a = x >> sqroot(power of 2)

//so:
int a = 10 / 4;
	// becomes
	a = 10 >> 2;

int b = 5412 / 16
	// becomes
	b = 54112 >> 4

same goes for multiplying except use "<<".

the ">>" is a faster operation than "/" (in c++ at least)

"i want a faster way to get the 'mod' of a number when it is a power of 2":
Code:
int a = x % (power of 2)
	//becomes
	a = x & (power of 2) - 1

int a = 56 % 8
	// becomes
	a = 56 & 7
	
// "&" is a faster operation than "%"
__________________
direct entry file specification.
npa is offline   Reply With Quote