# How to define an efficient \`bswap\_int\` for user defined primitive types?

**URL:** <https://discourse.julialang.org/t/how-to-define-an-efficient-bswap-int-for-user-defined-primitive-types/8051>\
**Category:** Performance\
**Created:** [December 28, 2017, 11:05pm UTC](https://discourse.julialang.org/t/how-to-define-an-efficient-bswap-int-for-user-defined-primitive-types/8051 "2017-12-28T23:05:17Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [December 28, 2017, 11:05pm UTC](https://discourse.julialang.org/t/how-to-define-an-efficient-bswap-int-for-user-defined-primitive-types/8051/1 "2017-12-28T23:05:17Z")

</div>

I am trying to define my own primitive type of bit-length `8*n` for some `n`, e.g. below I have `n=3`.  
The functions `lshr_int` and `shl_int` works directly on the newly-defined type but not `bswap_int`. I tried to check its definition using `@which bswap_int(UInt(888))` and I see that it’s an intrinsic function so I can’t look at its implementation for `UInt` and try to adapt it.

I can build my own `bswap_int` using `lshr_int` and `shl_int` and `|` but is there a more efficient way?

```julia
primitive type UInt24 <: Unsigned 24 end
x = unsafe_load(Ptr{UInt24}(pointer("abc")))

# bitshifts work fine
Base.lshr_int(x, 8)
Base.shl_int(x, 8)

# this will crash 
Base.bswap_int(x)

```

The background is that I am trying to build a more efficient string radixsort so being able to load the underlying bits of various length efficiently is key.

---

_[View the full topic](https://discourse.julialang.org/t/how-to-define-an-efficient-bswap-int-for-user-defined-primitive-types/8051)._
