# Efficient bit rotate functions: ror, rol...what is the official way for Julia v1?

**URL:** <https://discourse.julialang.org/t/efficient-bit-rotate-functions-ror-rol-what-is-the-official-way-for-julia-v1/19062>\
**Category:** General Usage\
**Created:** [December 28, 2018, 3:59pm UTC](https://discourse.julialang.org/t/efficient-bit-rotate-functions-ror-rol-what-is-the-official-way-for-julia-v1/19062 "2018-12-28T15:59:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rapasite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rapasite/32/3719_2.png) [@rapasite](https://discourse.julialang.org/u/rapasite)\
**Post date:** [December 28, 2018, 3:59pm UTC](https://discourse.julialang.org/t/efficient-bit-rotate-functions-ror-rol-what-is-the-official-way-for-julia-v1/19062/1 "2018-12-28T15:59:42Z")

</div>

Hi,  
I found this [https://github.com/JuliaLang/julia/issues/11592](https://github.com/JuliaLang/julia/issues/11592) but I can not understand what should I do for a `UInt64` or any other arbitrary `0b....`  
Am I missing something in the doc?

Thanks you

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [December 30, 2018, 9:12pm UTC](https://discourse.julialang.org/t/efficient-bit-rotate-functions-ror-rol-what-is-the-official-way-for-julia-v1/19062/2 "2018-12-30T21:12:37Z")

</div>

Define these:

```julia
ror(x::UInt64, k::Int) = (x >>> (0x3f & k)) | (x << (0x3f & -k))
rol(x::UInt64, k::Int) = (x >>> (0x3f & -k)) | (x << (0x3f & k))

```

Verification:

```julia
julia> code_native(ror, (UInt64, Int); syntax = :intel)
        ...
        ror rax, cl
        ...

julia> code_native(rol, (UInt64, Int); syntax = :intel)
        ...
        rol rax, cl
        ...

```

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [December 31, 2018, 6:03pm UTC](https://discourse.julialang.org/t/efficient-bit-rotate-functions-ror-rol-what-is-the-official-way-for-julia-v1/19062/3 "2018-12-31T18:03:20Z")

</div>

I rather think we should have these in Base for the fixed size integer types. The definitions are non-obvious enough that people shouldn’t have to define them for themselves. If someone wants to make a PR this is a good intro one!

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [March 30, 2024, 6:25am UTC](https://discourse.julialang.org/t/efficient-bit-rotate-functions-ror-rol-what-is-the-official-way-for-julia-v1/19062/4 "2024-03-30T06:25:17Z")

</div>

_someone_💤 has been napping.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [March 30, 2024, 9:25am UTC](https://discourse.julialang.org/t/efficient-bit-rotate-functions-ror-rol-what-is-the-official-way-for-julia-v1/19062/5 "2024-03-30T09:25:17Z")

</div>

This is included since Julia 1.5, in the form of [`bitrotate`](https://docs.julialang.org/en/v1/base/math/#Base.bitrotate):

```julia
help?> bitrotate
search: bitrotate

  bitrotate(x::Base.BitInteger, k::Integer)

  bitrotate(x, k) implements bitwise rotation. It returns the value of x with its bits
  rotated left k times. A negative value of k will rotate to the right instead.

  │ Julia 1.5
  │
  │ This function requires Julia 1.5 or later.

```

It does compile down to efficient instructions, at least on my machine.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [March 31, 2024, 1:30pm UTC](https://discourse.julialang.org/t/efficient-bit-rotate-functions-ror-rol-what-is-the-official-way-for-julia-v1/19062/6 "2024-03-31T13:30:39Z")

</div>

Thanks!
