# Piece of C++ programming in ITK examples

**URL:** <https://discourse.slicer.org/t/piece-of-c-programming-in-itk-examples/10648>\
**Category:** Support\
**Created:** [March 11, 2020, 5:44pm UTC](https://discourse.slicer.org/t/piece-of-c-programming-in-itk-examples/10648 "2020-03-11T17:44:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MachadoL](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/machadol/32/3372_2.png) [@MachadoL](https://discourse.slicer.org/u/MachadoL)\
**Post date:** [March 11, 2020, 5:44pm UTC](https://discourse.slicer.org/t/piece-of-c-programming-in-itk-examples/10648/1 "2020-03-11T17:44:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dave3d](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dave3d/32/5003_2.png) [@dave3d](https://discourse.slicer.org/u/dave3d)\
**Post date:** [March 11, 2020, 5:51pm UTC](https://discourse.slicer.org/t/piece-of-c-programming-in-itk-examples/10648/2 "2020-03-11T17:51:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![MachadoL](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/machadol/32/3372_2.png) [@MachadoL](https://discourse.slicer.org/u/MachadoL)\
**Post date:** [March 11, 2020, 6:01pm UTC](https://discourse.slicer.org/t/piece-of-c-programming-in-itk-examples/10648/3 "2020-03-11T18:01:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dave3d](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dave3d/32/5003_2.png) [@dave3d](https://discourse.slicer.org/u/dave3d)\
**Post date:** [March 11, 2020, 6:08pm UTC](https://discourse.slicer.org/t/piece-of-c-programming-in-itk-examples/10648/4 "2020-03-11T18:08:36Z")

</div>

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

> **[Bitwise operations in C](https://en.wikipedia.org/wiki/Bitwise_operations_in_C)**
>
> 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.

---

<div class="post-metadata">

**Author:** ![MachadoL](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/machadol/32/3372_2.png) [@MachadoL](https://discourse.slicer.org/u/MachadoL)\
**Post date:** [March 11, 2020, 7:27pm UTC](https://discourse.slicer.org/t/piece-of-c-programming-in-itk-examples/10648/5 "2020-03-11T19:27:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dave3d](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dave3d/32/5003_2.png) [@dave3d](https://discourse.slicer.org/u/dave3d)\
**Post date:** [March 11, 2020, 7:47pm UTC](https://discourse.slicer.org/t/piece-of-c-programming-in-itk-examples/10648/6 "2020-03-11T19:47:38Z")

</div>

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.
