# How to get the eight bit values of a variable with UInt8?

**URL:** https://discourse.julialang.org/t/how-to-get-the-eight-bit-values-of-a-variable-with-uint8/128022
**Category:** New to Julia
**Tags:** binaryio
**Created:** [April 13, 2025, 12:15pm UTC](https://discourse.julialang.org/t/how-to-get-the-eight-bit-values-of-a-variable-with-uint8/128022 "2025-04-13T12:15:39Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![dk-zen](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@dk-zen](https://discourse.julialang.org/u/dk-zen)
#### Post date: [April 13, 2025, 12:15pm UTC](https://discourse.julialang.org/t/how-to-get-the-eight-bit-values-of-a-variable-with-uint8/128022/1 "2025-04-13T12:15:39Z")

</div>

Now I’m reading a binary file. I have got a 8 bits namely “a”. According to the data description, each bit represents a different meaning, I want to read the value represented by 8 bit bits, how can I do it?

```julia
a = 0x0c # UInt8, little endian
a8 = ? # 
a7 = ?
a6 = ?
a5 = ?
a4 = ?
a3 = ?
a2 = ?
a1 = ?

```

---

<div class="post-metadata">

### Author: ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)
#### Post date: [April 13, 2025, 12:33pm UTC](https://discourse.julialang.org/t/how-to-get-the-eight-bit-values-of-a-variable-with-uint8/128022/2 "2025-04-13T12:33:51Z")

</div>

The simplest is:

```julia
julia> a = 0x0c
julia> digits(a, base=2, pad=8)
8-element Vector{Int64}:
 0
 0
 1
 1
 0
 0
 0
 0

```

---

<div class="post-metadata">

### Author: ![dk-zen](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@dk-zen](https://discourse.julialang.org/u/dk-zen)
#### Post date: [April 13, 2025, 1:52pm UTC](https://discourse.julialang.org/t/how-to-get-the-eight-bit-values-of-a-variable-with-uint8/128022/3 "2025-04-13T13:52:43Z")

</div>

Thanks a lot. An elegant answer.

---

<div class="post-metadata">

### Author: ![abulak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abulak/32/28314_2.png) [@abulak](https://discourse.julialang.org/u/abulak)
#### Post date: [April 13, 2025, 2:30pm UTC](https://discourse.julialang.org/t/how-to-get-the-eight-bit-values-of-a-variable-with-uint8/128022/4 "2025-04-13T14:30:11Z")

</div>

you can also do directly

```julia
bits(a::UInt8) = ntuple(i -> (a >> (i-1)) & one(a), 8)

```

which should be faster as it avoids allocation (3 times faster on my machine).

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [April 13, 2025, 3:05pm UTC](https://discourse.julialang.org/t/how-to-get-the-eight-bit-values-of-a-variable-with-uint8/128022/5 "2025-04-13T15:05:37Z")

</div>

> [@abulak](#):
>
> `bits(a::UInt8) = ntuple(i -> (a >> (i-1)) & one(a), 8)`

It generalizes well, also:

```julia
function bits(a::Integer)
   N = sizeof(a) * 8
   return ntuple(i -> (a >> (i-1)) & one(a), Val(N))
end

```

Example:

```julia
julia> @btime bits(x) setup=(x=rand(Int32))
  4.600 ns (0 allocations: 0 bytes)
(0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0)

```

---

<div class="post-metadata">

### Author: ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)
#### Post date: [April 13, 2025, 3:23pm UTC](https://discourse.julialang.org/t/how-to-get-the-eight-bit-values-of-a-variable-with-uint8/128022/6 "2025-04-13T15:23:30Z")

</div>

There is also the Bits.jl package, which is a bit dusty, but has this `bits` function, which just (lazily) wraps an integer as an array of `Bool`s:

```julia
julia> using Bits; bs = bits(0x0c)
<00001100>

julia> collect(bs)
8-element Vector{Bool}:
 0
 0
 1
 1
 0
 0
 0
 0

julia> bs[4]
true

julia> bs[1:4]
<1100>

```

---

<div class="post-metadata">

### Author: ![abulak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abulak/32/28314_2.png) [@abulak](https://discourse.julialang.org/u/abulak)
#### Post date: [April 13, 2025, 3:45pm UTC](https://discourse.julialang.org/t/how-to-get-the-eight-bit-values-of-a-variable-with-uint8/128022/7 "2025-04-13T15:45:42Z")

</div>

Probably better to use `Base.BitInteger` there at the cost of excluding @rfourquet s BitIntegers.jl 😉 As written this will be not correct for heap allocated integers.

---

<div class="post-metadata">

### Author: ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)
#### Post date: [April 13, 2025, 3:58pm UTC](https://discourse.julialang.org/t/how-to-get-the-eight-bit-values-of-a-variable-with-uint8/128022/8 "2025-04-13T15:58:14Z")

</div>

> [@abulak](#):
>
> at the cost of excluding @rfourquet s [BitIntegers.jl](https://juliaregistries.github.io/General/packages/redirect_to_repo/BitIntegers) 😉

😃 Good occasion to bump [Feature request: add an AbstractBitSigned type · Issue #37752 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/37752)

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [April 13, 2025, 4:03pm UTC](https://discourse.julialang.org/t/how-to-get-the-eight-bit-values-of-a-variable-with-uint8/128022/9 "2025-04-13T16:03:01Z")

</div>

You can usw bit-wise operations directly.

```julia
julia> a = 0x0c
0x0c

julia> bits = a .>> (0:7) .& 0x1
8-element Vector{UInt8}:
 0x00
 0x00
 0x01
 0x01
 0x00
 0x00
 0x00
 0x00

julia> f(a,s) = a >> s & 0x1
f (generic function with 1 method)

julia> f.(a, 0:7)
8-element Vector{UInt8}:
 0x00
 0x00
 0x01
 0x01
 0x00
 0x00
 0x00
 0x00

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [April 13, 2025, 4:19pm UTC](https://discourse.julialang.org/t/how-to-get-the-eight-bit-values-of-a-variable-with-uint8/128022/10 "2025-04-13T16:19:54Z")

</div>

Using a comprehension:

```julia
a = 0x0c

[!iszero(a & <<(one(a), i)) for i in 0:7] 

# result:
8-element Vector{Bool}:
 0
 0
 1
 1
 0
 0
 0
 0

```

---

<div class="post-metadata">

### Author: ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)
#### Post date: [April 13, 2025, 6:01pm UTC](https://discourse.julialang.org/t/how-to-get-the-eight-bit-values-of-a-variable-with-uint8/128022/11 "2025-04-13T18:01:43Z")

</div>

My package Modulo2.jl has the function `zz2vector`:

```julia
julia> zz2vector(0x0c)
8-element ZZ2Vector:
 0
 0
 1
 1
 0
 0
 0
 0

```

It works with all bit sizes, including BitIntegers.jl.

---

<div class="post-metadata">

### Author: ![danielwe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielwe/32/35657_2.png) [@danielwe](https://discourse.julialang.org/u/danielwe)
#### Post date: [April 13, 2025, 8:45pm UTC](https://discourse.julialang.org/t/how-to-get-the-eight-bit-values-of-a-variable-with-uint8/128022/12 "2025-04-13T20:45:20Z")

</div>

If you’re OK with a string representation, there’s also `bitstring`:

```julia-repl
julia> bitstring(0x0c)
"00001100"

```
