# Generate vector of binary digits from int

**URL:** <https://discourse.julialang.org/t/generate-vector-of-binary-digits-from-int/96291>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [March 19, 2023, 3:12am UTC](https://discourse.julialang.org/t/generate-vector-of-binary-digits-from-int/96291 "2023-03-19T03:12:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![smartalecH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/smartalech/32/32379_2.png) [@smartalecH](https://discourse.julialang.org/u/smartalecH)\
**Post date:** [March 19, 2023, 3:12am UTC](https://discourse.julialang.org/t/generate-vector-of-binary-digits-from-int/96291/1 "2023-03-19T03:12:03Z")

</div>

Is there an easy way to generate a vector of binary digits from an int?

For example, suppose I want a 4-bit representation of the integer `3` (`0b0011`). Something like:

```julia
julia> bin_to_vec(3,4)
[0, 0, 1, 1]

```

where I can optionally specify how many bits to pad to (in this case 4).

I’m hoping to use this vector as a mask for some additional computation.

Thanks!

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [March 19, 2023, 3:26am UTC](https://discourse.julialang.org/t/generate-vector-of-binary-digits-from-int/96291/2 "2023-03-19T03:26:22Z")

</div>

Not very elegant since it goes fron Int to String back to Int and then Vector, but it works:

```julia
julia> [Int(x)-48 for x in string(3, base = 2, pad = 4)]
4-element Vector{Int64}:
 0
 0
 1
 1

```

For masking operations, though, a `BitVector` might be more appropriate:

```julia
BitVector(x == '0' ? false : true for x in string(3, base = 2, pad = 4))
4-element BitVector:
 0
 0
 1
 1

```

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [March 19, 2023, 5:19am UTC](https://discourse.julialang.org/t/generate-vector-of-binary-digits-from-int/96291/3 "2023-03-19T05:19:42Z")

</div>

You can use `digits`, but note the returned vector is in reversed order (least significant bit first)

```julia
digits(3, base=2, pad=4)

```

---

<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:** [March 19, 2023, 6:46am UTC](https://discourse.julialang.org/t/generate-vector-of-binary-digits-from-int/96291/4 "2023-03-19T06:46:26Z")

</div>

How do you want the output vector to be? A vector of `Int`s, a vector of `Bool`s, or could it be a `BitVector`? How are going to use it?

And is it always a 4-bit number, or could the input/output be other lengths as well?

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [March 19, 2023, 3:17pm UTC](https://discourse.julialang.org/t/generate-vector-of-binary-digits-from-int/96291/5 "2023-03-19T15:17:15Z")

</div>

```julia

bitvec(n)=n>1 ? [bitvec(n÷2);Bool(n%2)] : Bool[1]

tomask(n,p=1)=vcat(fill(false,max(0,p-floor(Int, log(2,n))-1)),bitvec(n))

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [March 19, 2023, 5:02pm UTC](https://discourse.julialang.org/t/generate-vector-of-binary-digits-from-int/96291/6 "2023-03-19T17:02:00Z")

</div>

```julia
julia> bin_to_vec(x,n) = [(x >> i) & 1 for i in n-1:-1:0]
bin_to_vec (generic function with 1 method)

julia> bin_to_vec(3,4)
4-element Vector{Int64}:
 0
 0
 1
 1

```

Is this good? (shifts and ANDs are fast, so should be efficient).

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [March 19, 2023, 6:59pm UTC](https://discourse.julialang.org/t/generate-vector-of-binary-digits-from-int/96291/7 "2023-03-19T18:59:28Z")

</div>

```julia
b2v(n,p)=n>1 ? [b2v(n>>1,p-1);Bool(n&1)] : [fill(false,max(0,p-1)); true]

```

---

<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:** [March 19, 2023, 7:01pm UTC](https://discourse.julialang.org/t/generate-vector-of-binary-digits-from-int/96291/8 "2023-03-19T19:01:37Z")

</div>

The best approach here might be to not do anything. Rather than getting a vector of `Bool` to mask with, you can just use a loop and mask with bitshifts.
