# Does Julia has bitwise circular shift?

**URL:** <https://discourse.julialang.org/t/does-julia-has-bitwise-circular-shift/44220>\
**Category:** General Usage\
**Created:** [August 3, 2020, 10:07pm UTC](https://discourse.julialang.org/t/does-julia-has-bitwise-circular-shift/44220 "2020-08-03T22:07:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lucas\_Queiroz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas_queiroz/32/12086_2.png) [@Lucas\_Queiroz](https://discourse.julialang.org/u/Lucas_Queiroz)\
**Post date:** [August 3, 2020, 10:07pm UTC](https://discourse.julialang.org/t/does-julia-has-bitwise-circular-shift/44220/1 "2020-08-03T22:07:16Z")

</div>

[This documentation page](https://docs.julialang.org/en/v1/manual/mathematical-operations/) lists the logical and arithmetic shift, as well as the bitwise NOT, AND, OR and XOR. However, the circular shift is absent.

Does Julia has a bitwise circular shift? If yes, where can I find it’s documentation? If not, is it possible to implement it with performance similar to the other bitwise operations?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [August 3, 2020, 10:21pm UTC](https://discourse.julialang.org/t/does-julia-has-bitwise-circular-shift/44220/2 "2020-08-03T22:21:52Z")

</div>

I don’t think it exists, but for `Int64` this should work and be about 3x slower than other bitshifts (since it uses 3 of them). You could easily enough write methods for other types of `Integer`, but I don’t know of a way in Julia to automatically get the number of bits in an integer.

```julia
function circshift(x, n)
    return (x<<n) | (x>>(muladd(8, sizeof(x), -n)
end

```

EDIT: now should work for any `Integer` subtype

---

<div class="post-metadata">

**Author:** ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)\
**Post date:** [August 3, 2020, 10:30pm UTC](https://discourse.julialang.org/t/does-julia-has-bitwise-circular-shift/44220/3 "2020-08-03T22:30:02Z")

</div>

> [@Oscar\_Smith](#):
>
> but I don’t know of a way in Julia to automatically get the number of bits in an integer

`sizeof(x)` or `sizeof(typeof(x))` gives the amount of bytes

---

<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:** [August 3, 2020, 11:21pm UTC](https://discourse.julialang.org/t/does-julia-has-bitwise-circular-shift/44220/4 "2020-08-03T23:21:39Z")

</div>

Hot off the press: `bitrotate`

[https://docs.julialang.org/en/v1.5/base/math/#Base.bitrotate](https://docs.julialang.org/en/v1.5/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.

  julia> bitrotate(UInt8(114), 2)
  0xc9

  julia> bitstring(bitrotate(0b01110010, 2))
  "11001001"

  julia> bitstring(bitrotate(0b01110010, -2))
  "10011100"

  julia> bitstring(bitrotate(0b01110010, 8))
  "01110010"

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [August 3, 2020, 11:27pm UTC](https://discourse.julialang.org/t/does-julia-has-bitwise-circular-shift/44220/5 "2020-08-03T23:27:13Z")

</div>

```julia
(x << ((sizeof(T) << 3 - 1) & k)) | (x >>> ((sizeof(T) << 3 - 1) & -k))

```

is the body of the method. The biggest difference is that this actually can rotate in both directions as opposed to mine which was unidirectional.

---

<div class="post-metadata">

**Author:** ![Lucas\_Queiroz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucas_queiroz/32/12086_2.png) [@Lucas\_Queiroz](https://discourse.julialang.org/u/Lucas_Queiroz)\
**Post date:** [August 4, 2020, 4:26am UTC](https://discourse.julialang.org/t/does-julia-has-bitwise-circular-shift/44220/6 "2020-08-04T04:26:12Z")

</div>

It came right when I need 😀, it’s timing couldn’t be better!

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 4, 2020, 7:28am UTC](https://discourse.julialang.org/t/does-julia-has-bitwise-circular-shift/44220/7 "2020-08-04T07:28:09Z")

</div>

> [@Oscar\_Smith](#):
>
> I don’t think it exists, but for `Int64` this should work and be about 3x slower than other bitshifts (since it uses 3 of them).

Actually, it appears to be just as fast as an ordinary bitshift. It seems that the compiler is smart enough to optimize it into a single bitrotate operation (from [Circular shift - Wikipedia](https://en.wikipedia.org/wiki/Circular_shift)):

> Unfortunately, many programming languages, including [C](https://en.wikipedia.org/wiki/C_(programming_language)), do not have operators or standard functions for circular shifting, even though virtually all [processors](https://en.wikipedia.org/wiki/Processors) have [bitwise operation](https://en.wikipedia.org/wiki/Bitwise_operation) instructions for it […] some constructs in standard ANSI C code may be optimized by a compiler to the “rotate” assembly language instruction
