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

**URL:** <https://discourse.julialang.org/t/not-possible-to-define-primitive-types-with-8-bits-or-less/16378>\
**Category:** Internals & Design\
**Created:** [October 16, 2018, 1:14am UTC](https://discourse.julialang.org/t/not-possible-to-define-primitive-types-with-8-bits-or-less/16378 "2018-10-16T01:14:47Z")\
**Posts on this page:** 5\
**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:** [October 16, 2018, 1:14am UTC](https://discourse.julialang.org/t/not-possible-to-define-primitive-types-with-8-bits-or-less/16378/1 "2018-10-16T01:14:47Z")

</div>

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](https://discourse.julialang.org/t/the-2048-challenge-fastest-ways-to-simulate-games-of-2048-in-julia/16331) 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.

---

<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:** [October 16, 2018, 1:49am UTC](https://discourse.julialang.org/t/not-possible-to-define-primitive-types-with-8-bits-or-less/16378/2 "2018-10-16T01:49:14Z")

</div>

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

---

<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:** [October 16, 2018, 6:50am UTC](https://discourse.julialang.org/t/not-possible-to-define-primitive-types-with-8-bits-or-less/16378/3 "2018-10-16T06:50:01Z")

</div>

Yes, this is [documented](https://docs.julialang.org/en/stable/manual/types/#Primitive-Types-1):

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

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [October 16, 2018, 12:11pm UTC](https://discourse.julialang.org/t/not-possible-to-define-primitive-types-with-8-bits-or-less/16378/4 "2018-10-16T12:11:18Z")

</div>

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](https://en.wikipedia.org/wiki/Bitboard) to get an overview of what bitboards are. Then read [this page](https://www.chessprogramming.org/Bitboards) and [this page](http://pages.cs.wisc.edu/~psilord/blog/data/chess-pages/index.html) (and the links therein) to understand how they can be used in practice.

Also study [the C++ code](https://github.com/nneonneo/2048-ai/blob/master/2048.cpp) 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).

---

<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:** [October 16, 2018, 12:45pm UTC](https://discourse.julialang.org/t/not-possible-to-define-primitive-types-with-8-bits-or-less/16378/5 "2018-10-16T12:45:40Z")

</div>

> [@bennedich](#):
>
> Modern CPUs don’t support 4 bit primitive types.

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.
