# Why boolean is 4 bytes instead of 1 byte in struct

**URL:** <https://discourse.julialang.org/t/why-boolean-is-4-bytes-instead-of-1-byte-in-struct/82613>\
**Category:** General Usage\
**Created:** [June 12, 2022, 12:48am UTC](https://discourse.julialang.org/t/why-boolean-is-4-bytes-instead-of-1-byte-in-struct/82613 "2022-06-12T00:48:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jk091](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jk091/32/37086_2.png) [@jk091](https://discourse.julialang.org/u/jk091)\
**Post date:** [June 12, 2022, 12:48am UTC](https://discourse.julialang.org/t/why-boolean-is-4-bytes-instead-of-1-byte-in-struct/82613/1 "2022-06-12T00:48:04Z")

</div>

Hi all,

I’m having trouble understanding why boolean is taking 4 bytes instead of 1 byte in structs.  
Here is an example:

```julia
struct F8
    p::Float32
    t::Float32
end
get_F8() = F8(0.0f0, 0.0f0)
println(sizeof(get_F8())) # 8 bytes, correct

struct F12
    p::Float32
    t::Float32
    b::Bool # why 4 bytes?
end

get_F12() = F12(0.0f0, 0.0f0, true)
println(sizeof(get_F12())) # 12 bytes, incorrect was expecting 9 bytes

struct F1
    b::Bool # 1 byte
end
get_F1() = F1(true)
println(sizeof(get_F1())) # 1 byte, correct

```

Thanks! 😃

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [June 12, 2022, 12:51am UTC](https://discourse.julialang.org/t/why-boolean-is-4-bytes-instead-of-1-byte-in-struct/82613/2 "2022-06-12T00:51:17Z")

</div>

The `Bool` is 1 byte, but there are 3 bytes of padding. Julia (and most other languages) pad structs to prevent memory alignment issues.

---

<div class="post-metadata">

**Author:** ![jk091](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jk091/32/37086_2.png) [@jk091](https://discourse.julialang.org/u/jk091)\
**Post date:** [June 12, 2022, 12:52am UTC](https://discourse.julialang.org/t/why-boolean-is-4-bytes-instead-of-1-byte-in-struct/82613/3 "2022-06-12T00:52:21Z")

</div>

> [@jk091](#):
>
> Julia (and most other languages) pad structs to prevent memory alignment issues

I did not know that, thanks!

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [June 12, 2022, 7:21am UTC](https://discourse.julialang.org/t/why-boolean-is-4-bytes-instead-of-1-byte-in-struct/82613/4 "2022-06-12T07:21:05Z")

</div>

Note that it’s worth keeping padding in mind when ordering your fields:

```julia
julia> sizeof((1,true,1,true))
32

julia> sizeof((1,1,true,true))
24

```

We can avoid excessive padding by grouping the `Bool`s together.
