Piece of C++ programming in ITK examples

Hey guys,

very basic question here. What does the operator << do in the following line of the example DeformableRegistration6.cxx?

TransformType::MeshSizeType requiredMeshSize;
for( unsigned int d = 0; d < ImageDimension; d++ )   
   requiredMeshSize[d] = meshSize[d] << level;

Level is an integer number; requiredMeshSize is an integer vector of imageDimension size;

The example is explained at page 758 of the (PDF) ITK Software Guide.

Thanks in advance.

That’s doing bit shifting. So ‘meshSize[d]’ is getting bit shifted ‘level’ times. Essentially it’s double ‘meshSize[d]’ ‘level’ times.

Still, hard to grasp. Is it doing binary operations? Where can I learn such association bitshifting with multiplication.
Thanks anyway! :wink:

Here’s the Wikipedia entry for C’s bit operations:

Half way down they talk about bit shifting.

Here’s a simple example:

unsigned char x=1;

unsigned char y = x << 1;

The binary representation of x is 0000 0001. Doing 1 bit shift to the left gets you 0000 0010. So y==2.

For every bit shift operation, all the binary digits get shifted one position to the left. In effect, you are multiplying by 2, until you run out of bits. Any bit at the top gets lost in the bit shift.

1 Like

Great!!! Thank you so much for your patience.
I guess I got it!
Thanks again.

1 Like

It’s one of those old-school C optimizations. Back in the day shift operations where much faster (1 cycle) than multiple operations (O(# bits), I think).

But these days, who the heck cares? And it makes the code harder to understand if you’re not familiar with these things.