Not possible to define primitive types with 8 bits or less?

In Julia v1.0 I tried to create a primitive type that stores 4 bits of data but I get the error

primitive type Cell 4 end

invalid number of bits in primitive type Cell

Stacktrace:
[1] top-level scope at none:0

In my 2048 challenge post I referenced a SO post that mentions the author made a really fast algorithm for playing 2048 using 4 bit chunks nybbles.

If I was to do the same in Julia it seems like that I can’t easily replicate the idea because it’s not allowed to create 4 bits primitive types.

Actually I don’t need to represent individual grid cells. I can represent the whole board as a 64bit type. This sovles the problem!

Yes, this is documented:

Currently, only sizes that are multiples of 8 bits are supported.

Modern CPUs don’t support 4 bit primitive types. Even if they did, you wouldn’t want to use it for your application anyway. You should use a larger word size, to allow for parallelizing operations. The sentence you’re referring to in the SO post says:

My approach encodes the entire board (16 entries) as a single 64-bit integer (where tiles are the nybbles, i.e. 4-bit chunks).

Please read this page to get an overview of what bitboards are. Then read this page and this page (and the links therein) to understand how they can be used in practice.

Also study the C++ code that’s linked in the SO post, since I think most of the relevant code can be ported over to Julia fairly easily (bit operations work similarly in both languages).

Nevertheless, Juila could support them, allowing a different speed/memory trade-off. I suppose it is just a lot of work which has not been a priority.

1 Like