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


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

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!

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


WIP: faster string sort

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

z = UInt(16^5*15 + 16^4*15 + 16^3*15 + 16^2*15 + 16*15+ 15)


This may help:


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