very basic question here. What does the operator
<< do in the following line of the example
for( unsigned int d = 0; d < ImageDimension; d++ )
requiredMeshSize[d] = meshSize[d] << level;
Level is an integer number;
requiredMeshSize is an integer vector of
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.
Here’s the Wikipedia entry for C’s bit operations:
In the C programming language, operations can be performed on a bit level using bitwise operators.
Bitwise operations are contrasted by byte-level operations which characterize the bitwise operators' logical counterparts, the AND, OR and NOT operators. Instead of performing on individual bits, byte-level operators perform on strings of eight bits (known as bytes) at a time. The reason for this is that a byte is normally the smallest unit of addressable memory (i.e. data with a unique memory ad...
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.
Great!!! Thank you so much for your patience.
I guess I got it!
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.