mik
mik

Reputation:

What does this line of code do?

Confused as to what this code does

for (L=0; L < levels; L++, N_half>>=1){
    func( y, N_half);
} // end: levels for loop

In particular this " N_half>>=1 "

Thanks

Upvotes: 1

Views: 394

Answers (6)

Ralph M. Rickenbach
Ralph M. Rickenbach

Reputation: 13163

This seems to be the same as

for (L=0; L < levels; L++)
{
  func(y, N_Half); 
  N_Half /= 2;
}

The question has been rephrased since I answered it, such that this is no longer valid, but added for completeness: If nothing else is done within the loop, it is equivalent to:

N_Half >>= levels;

Caveats:

  • if N_Half < 2^levels (0 for several iterations)
  • if N_Half < 0 (first rightshift will depending on the implementation make it a positive number)

Upvotes: 0

Nick Dandoulakis
Nick Dandoulakis

Reputation: 43110

>>= operator shifts number's digits k positions at right

examples:

binary form

N = 101010111 // 2-base  arithmetic system
N >>= 1; // `division` by 2
N: 010101011

decimal form

N = 123456 // 10-base  arithmetic system
N >>= 2; // `division` by 10^2
N: 001234

as usual, the numbers in memory are in binary form and >>=1 is equivalent to division by 2.

Upvotes: 1

Ronny Vindenes
Ronny Vindenes

Reputation: 2361

It right shifts N_half by 1 (i.e. divides it by two) and stores the result back in N_half

Upvotes: 0

Barry Kelly
Barry Kelly

Reputation: 42152

If N_half is a positive or unsigned integer, it halves it.

Upvotes: 0

chaos
chaos

Reputation: 124297

N_half>>=1 performs a 1-place bitwise shift-right on N_half, which (for non-negative numbers) divides it by 2.

>>= is to >> as += is to +.

Upvotes: 5

D.Shawley
D.Shawley

Reputation: 59563

It advances the loop by dividing N_half by two at every iteration. It is equivalent to:

for (L=0; L<levels; ++L, N_half=N_half / 2) {
    ...
}

Upvotes: 9

Related Questions