# Allowing integers to overflow to 0

**URL:** https://discourse.julialang.org/t/allowing-integers-to-overflow-to-0/17394
**Category:** General Usage
**Tags:** integer-overflow
**Created:** [November 10, 2018, 8:58pm UTC](https://discourse.julialang.org/t/allowing-integers-to-overflow-to-0/17394 "2018-11-10T20:58:54Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![wherrera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wherrera/32/5288_2.png) [@wherrera](https://discourse.julialang.org/u/wherrera)
#### Post date: [November 10, 2018, 8:58pm UTC](https://discourse.julialang.org/t/allowing-integers-to-overflow-to-0/17394/1 "2018-11-10T20:58:54Z")

</div>

I’ve come across a place where allowing a UInt8 to overflow to 0 on adding 1 is the proper behavior.

Is there something like a decorator macro to allow the overflow and not get the overflow error with adding 1 to 255::UInt8 ?

---

<div class="post-metadata">

### Author: ![favba](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/favba/32/2735_2.png) [@favba](https://discourse.julialang.org/u/favba)
#### Post date: [November 10, 2018, 9:03pm UTC](https://discourse.julialang.org/t/allowing-integers-to-overflow-to-0/17394/2 "2018-11-10T21:03:59Z")

</div>

Julia doesn’t trow overflow errors by default:

```julia
julia> UInt8(255) + UInt8(1)
0x00

```

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [November 10, 2018, 10:33pm UTC](https://discourse.julialang.org/t/allowing-integers-to-overflow-to-0/17394/3 "2018-11-10T22:33:09Z")

</div>

I assume you were doing,

```julia
julia> UInt8(UInt8(255) + 1)
ERROR: InexactError: trunc(UInt8, 256)
Stacktrace:
 [1] throw_inexacterror(::Symbol, ::Any, ::Int64) at ./boot.jl:567
 [2] checked_trunc_uint at ./boot.jl:597 [inlined]
 [3] toUInt8 at ./boot.jl:659 [inlined]
 [4] UInt8(::Int64) at ./boot.jl:719
 [5] top-level scope at none:0

```

(Or the equivalent with a typed variable/field array element). You can avoid the overflow check in the **conversion** ( **NOT** the add) by using `%` instead. i.e.

```julia
julia> (UInt8(255) + 1) % UInt8
0x00

```

which could be useful in other cases but the better way to deal with `+ 1` in particular would be to do what @favba posted, i.e. make sure both operand have the right type to begin with.

---

<div class="post-metadata">

### Author: ![wherrera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wherrera/32/5288_2.png) [@wherrera](https://discourse.julialang.org/u/wherrera)
#### Post date: [November 11, 2018, 4:24am UTC](https://discourse.julialang.org/t/allowing-integers-to-overflow-to-0/17394/4 "2018-11-11T04:24:08Z")

</div>

Ah the bug was in adding one not adding UInt8(1).

I did not know you could do modulus with types. Thanks.
