# Intbv arithmetic operations return types

**URL:** <https://discourse.myhdl.org/t/intbv-arithmetic-operations-return-types/132>\
**Category:** Uncategorized\
**Created:** [October 24, 2016, 4:00pm UTC](https://discourse.myhdl.org/t/intbv-arithmetic-operations-return-types/132 "2016-10-24T16:00:25Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jck](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.myhdl.org/jck/32/107_2.png) [@jck](https://discourse.myhdl.org/u/jck)\
**Post date:** [October 24, 2016, 4:00pm UTC](https://discourse.myhdl.org/t/intbv-arithmetic-operations-return-types/132/1 "2016-10-24T16:00:25Z")

</div>

Is there any particular reason why adding or subtracting `intbv`s return `int`s, but anding, shifting etc return `intvb`s?

---

<div class="post-metadata">

**Author:** ![josyb](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.myhdl.org/josyb/32/5_2.png) [@josyb](https://discourse.myhdl.org/u/josyb)\
**Post date:** [October 24, 2016, 6:45pm UTC](https://discourse.myhdl.org/t/intbv-arithmetic-operations-return-types/132/2 "2016-10-24T18:45:42Z")

</div>

The reason could be that adding and subtracting (and alike) are _arithmetic_ operations, where anding, shifting e.a. are _bitwise logical_ operations.  
But in all cases the result of the operation is used in an assignment and thus the actual integer value will be used. Making the promotion of the integer result of the _bitwise logical_ operations to `intbv` superfluous.

---

<div class="post-metadata">

**Author:** ![cfelton](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.myhdl.org/cfelton/32/3_2.png) [@cfelton](https://discourse.myhdl.org/u/cfelton)\
**Post date:** [October 25, 2016, 12:06pm UTC](https://discourse.myhdl.org/t/intbv-arithmetic-operations-return-types/132/3 "2016-10-25T12:06:33Z")

</div>

There is an old thread, on the old mailing-list, discussing this topic. I had tested returning `intbv` in all cases, it had a performance impact - at that time it was decided not to use `intbv` in all cases. I don’t recall the arguments for and / or against, just that in the past it was proposed to use `intbv` as all returns and it was, at that point in time, decided against.

---

<div class="post-metadata">

**Author:** ![cfelton](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.myhdl.org/cfelton/32/3_2.png) [@cfelton](https://discourse.myhdl.org/u/cfelton)\
**Post date:** [October 25, 2016, 12:08pm UTC](https://discourse.myhdl.org/t/intbv-arithmetic-operations-return-types/132/4 "2016-10-25T12:08:30Z")

</div>

[http://old.myhdl.org/doku.php/user:cfelton:projects:small\_profile](http://old.myhdl.org/doku.php/user:cfelton:projects:small_profile) ???

---

<div class="post-metadata">

**Author:** ![cfelton](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.myhdl.org/cfelton/32/3_2.png) [@cfelton](https://discourse.myhdl.org/u/cfelton)\
**Post date:** [October 25, 2016, 12:09pm UTC](https://discourse.myhdl.org/t/intbv-arithmetic-operations-return-types/132/5 "2016-10-25T12:09:43Z")

</div>

Here is the thread (I believe) and the rational:  
[https://sourceforge.net/p/myhdl/mailman/message/22137406/](https://sourceforge.net/p/myhdl/mailman/message/22137406/)

(gmane is down?)

---

<div class="post-metadata">

**Author:** ![josyb](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.myhdl.org/josyb/32/5_2.png) [@josyb](https://discourse.myhdl.org/u/josyb)\
**Post date:** [October 25, 2016, 2:26pm UTC](https://discourse.myhdl.org/t/intbv-arithmetic-operations-return-types/132/6 "2016-10-25T14:26:45Z")

</div>

@cfelton Thanks for enlightening me (us?), I didn’t think of slicing of/indexing the result. But then we should never make a distinction between arithmetic and logical operators?

---

<div class="post-metadata">

**Author:** ![josyb](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.myhdl.org/josyb/32/5_2.png) [@josyb](https://discourse.myhdl.org/u/josyb)\
**Post date:** [October 30, 2016, 9:15am UTC](https://discourse.myhdl.org/t/intbv-arithmetic-operations-return-types/132/7 "2016-10-30T09:15:09Z")

</div>

@cfelton I ran into the case this morning …

```auto
movingxpattern[i].next = ((xcounter.Q << SHIFT_PIX) + ycounter.Q + i)[WIDTH_PIXEL:]

```

throws an exception: AttributeError: ‘Subscript’ object has no attribute ‘vhd’, obviously because I’m trying to slice an integer. The work-around:

```auto
movingxpattern[i].next = ((xcounter.Q << SHIFT_PIX) + ycounter.Q + i) & (2 ** WIDTH_PIXEL - 1)

```

is fine.

---

<div class="post-metadata">

**Author:** ![jck](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.myhdl.org/jck/32/107_2.png) [@jck](https://discourse.myhdl.org/u/jck)\
**Post date:** [November 1, 2016, 3:28pm UTC](https://discourse.myhdl.org/t/intbv-arithmetic-operations-return-types/132/8 "2016-11-01T15:28:12Z")

</div>

That’s good to know. It seems like an arbitrary choice made for performance reasons. Perhaps we should document it?
