# Reverse UnitRange

**URL:** <https://discourse.julialang.org/t/reverse-unitrange/62038>\
**Category:** General Usage\
**Created:** [May 29, 2021, 1:06pm UTC](https://discourse.julialang.org/t/reverse-unitrange/62038 "2021-05-29T13:06:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![minetest2048](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/minetest2048/32/45961_2.png) [@minetest2048](https://discourse.julialang.org/u/minetest2048)\
**Post date:** [May 29, 2021, 1:06pm UTC](https://discourse.julialang.org/t/reverse-unitrange/62038/1 "2021-05-29T13:06:17Z")

</div>

I’m trying to implement SystemVerilog style bit indexing/slicing operator:

```julia
a = 0b11100
b[4:2] == 0b111

```

But I realize that the colon operator doesn’t work when the second index is lower than the first index:

```julia
julia> dump(4:2)
UnitRange{Int64}
  start: Int64 4
  stop: Int64 3

```

The stop index doesn’t go to 2 as expected. Most solutions for reverse range uses something like `4:-1:2` which is not as convenient as `4:2`. Is there a way to make `4:2` work?

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [May 29, 2021, 1:12pm UTC](https://discourse.julialang.org/t/reverse-unitrange/62038/2 "2021-05-29T13:12:14Z")

</div>

My basic understanding is that `4:2` actually works and is purposefully an “empty” range. It’s just that `a:b` is synctactic sugar for `a:1:b`. You could define your own operator like (edited the snippet)

```julia
julia> …(a,b) = (a<b) ? (a:b) : (a:-1:b)
… (generic function with 1 method)

julia> 2…4
2:4

julia> 4…2
4:-1:2

```

I guess. And there is probably a better symbol for that…

---

<div class="post-metadata">

**Author:** ![ctkelley](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ctkelley/32/10684_2.png) [@ctkelley](https://discourse.julialang.org/u/ctkelley)\
**Post date:** [May 29, 2021, 1:23pm UTC](https://discourse.julialang.org/t/reverse-unitrange/62038/3 "2021-05-29T13:23:03Z")

</div>

Looks like @briochemc has it right. Why do I get the `4:3` when I type `4:2` at the prompt?

```julia
julia> 4:2
4:3

julia> collect(4:2)
Int64[]

julia> 4:-1:2
4:-1:2

julia> z=collect(4:-1:2)
3-element Vector{Int64}:
 4
 3
 2

```

---

<div class="post-metadata">

**Author:** ![greg\_plowman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/greg_plowman/32/8100_2.png) [@greg\_plowman](https://discourse.julialang.org/u/greg_plowman)\
**Post date:** [May 30, 2021, 7:52am UTC](https://discourse.julialang.org/t/reverse-unitrange/62038/4 "2021-05-30T07:52:11Z")

</div>

> [@ctkelley](#):
>
> Why do I get the `4:3` when I type `4:2` at the prompt?

I guess `n:n-1` is the “canonical” form for all empty ranges. All empty ranges are converted to this form on construction.

Among other things, it makes sense that a 0-length range starting at `n` ends at `n-1`. Length of `a:b` is consistently defined as length(a:b) = b-a+1

Also we have `4:2 == 4:0`
