Home Segments Index Top Previous Next

315: Mainline

Using C++'s shorthand notations for variable reassignment, you can write the power_of_2 function this way:

int power_of_2 (int n) { 
  int counter, result = 1; 
  for (counter = n; counter; --counter) 
   result = result * 2; 
  return result; 
}