# How to write bits into a user-defined primitive type?

**URL:** https://discourse.julialang.org/t/how-to-write-bits-into-a-user-defined-primitive-type/8053
**Category:** General Usage
**Created:** [December 29, 2017, 12:31am UTC](https://discourse.julialang.org/t/how-to-write-bits-into-a-user-defined-primitive-type/8053 "2017-12-29T00:31:00Z")
**Posts on this page:** 4
**Page:** 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 29, 2017, 12:31am UTC](https://discourse.julialang.org/t/how-to-write-bits-into-a-user-defined-primitive-type/8053/1 "2017-12-29T00:31:00Z")

</div>

I have defined my own primitive type as below which works.

```julia
import Base: >>, <<
primitive type UInt24 24 end

>>(x::UInt24, y) = Base.lshr_int(x, y)
<<(x::UInt24, y) = Base.shl_int(x, y)

```

I then tried to define a masking operation using `&` the below works pretty well but I can’t figure out how to define my own bits e.g. if I want to set `z = 0xffffff` it turns `z` into UInt32 with `0x00ffffff`. But I want 24 bits of 1’s. How can I acheive that? Or more generally how do I define arbitrary bit patters for my new type. Help appreciated! This will help make a much faster string sorting algorithm in pure Julia!

```julia
x = unsafe_load(Ptr{UInt24}(pointer("abc")))
y = unsafe_load(Ptr{UInt24}(pointer("def")))

Base.and_int(x,y)

```

---

<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 29, 2017, 12:57am UTC](https://discourse.julialang.org/t/how-to-write-bits-into-a-user-defined-primitive-type/8053/2 "2017-12-29T00:57:14Z")

</div>

I think I got a working solution. Just not sure if it’s the most efficient.

```julia
z = UInt(16^5*15 + 16^4*15 + 16^3*15 + 16^2*15 + 16*15+ 15)
unsafe_load(Ptr{UInt24}(pointer_from_objref(z)))

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [December 29, 2017, 7:21am UTC](https://discourse.julialang.org/t/how-to-write-bits-into-a-user-defined-primitive-type/8053/3 "2017-12-29T07:21:49Z")

</div>

This may help:

> [@Convert to and from Int24](https://discourse.julialang.org/t/convert-to-and-from-int24/7670):
>
> I am want to try an Int24 type, purely for storage. It is easy to declare one with primitive type Int24 \<: Signed 24 end but I am not sure how to access its value. I only need conversion to and from Int32, but I don’t think I can use reinterpret because they don’t have the same number of bits. How can I convert to and from Int32? (BTW, I am aware that I destroy alignment and it will degrade performance; it is an experiment)

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [December 29, 2017, 12:32pm UTC](https://discourse.julialang.org/t/how-to-write-bits-into-a-user-defined-primitive-type/8053/4 "2017-12-29T12:32:48Z")

</div>

This topic might also be of interest (this isn’t the first time people have wanted to use a 24-bit primitive type):

> [@Regression between v0.6 and master with reinterpret from #21831](https://discourse.julialang.org/t/regression-between-v0-6-and-master-with-reinterpret-from-21831/4040):
>
> On v0.6.0-rc2, this works: julia\> primitive type Foo 24 end ; reinterpret(Foo, b"123") 1-element Array{Foo,1}: Foo(0x333231) On v0.7.0-DEV.427, it fails: julia\> primitive type Foo 24 end ; reinterpret(Foo, b"123") ERROR: ArgumentError: reinterpret from alignment 1 bytes to alignment 4 bytes not allowed Stacktrace: [1] reinterpret(::Type{Foo}, ::Array{UInt8,1}, ::Tuple{Int64}) at ./array.jl:160 [2] reinterpret(::Type{Foo}, ::Array{UInt8,1}) at ./array.jl:139 This seems to be another regres…
