# Converting bitstring to number

**URL:** https://discourse.julialang.org/t/converting-bitstring-to-number/12199
**Category:** General Usage
**Tags:** bitstring
**Created:** [July 6, 2018, 12:47am UTC](https://discourse.julialang.org/t/converting-bitstring-to-number/12199 "2018-07-06T00:47:57Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)
#### Post date: [July 6, 2018, 12:47am UTC](https://discourse.julialang.org/t/converting-bitstring-to-number/12199/1 "2018-07-06T00:47:57Z")

</div>

I know that if I have a litteral bitstring, I can convert it to an integer with

```julia
Int(0b110101)

```

Now, however, I would like to convert a bit string, either given as a tuple of 0 and 1, or as an actual string, to the corresponding integer value. I figured in the tuple case, I could do something like

```julia
julia> bits2int(b::Vararg{Integer,N}) where N = sub2ind(ntuple(_->2,Val(N)),reverse(b.+1)...) - 1;

julia> Int(0b110101) == bits2int(1,1,0,1,0,1)
true

```

However, the above is really slow.

In the string case, I thought it might be possible to do with a string macro, but I am not sure how.

Any suggestions?

Thanks,  
Jeremy

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [July 6, 2018, 12:52am UTC](https://discourse.julialang.org/t/converting-bitstring-to-number/12199/2 "2018-07-06T00:52:03Z")

</div>

Using v0.7 / master, it’s simple:  
`parse(Int, "110101"; base=2)`

---

<div class="post-metadata">

### Author: ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)
#### Post date: [July 6, 2018, 12:56am UTC](https://discourse.julialang.org/t/converting-bitstring-to-number/12199/3 "2018-07-06T00:56:51Z")

</div>

Ah right of course. Is it possible to obtain the length of the string in a type stable way (i.e. the length will go in a type parameter) using a string macro?

---

<div class="post-metadata">

### Author: ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)
#### Post date: [July 6, 2018, 12:58am UTC](https://discourse.julialang.org/t/converting-bitstring-to-number/12199/4 "2018-07-06T00:58:30Z")

</div>

Do you want the length of the string, or the position of the leftmost bit (i.e. without any leading zeros)?

---

<div class="post-metadata">

### Author: ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)
#### Post date: [July 6, 2018, 12:59am UTC](https://discourse.julialang.org/t/converting-bitstring-to-number/12199/5 "2018-07-06T00:59:20Z")

</div>

The total length including leading zeros.

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [July 6, 2018, 1:25am UTC](https://discourse.julialang.org/t/converting-bitstring-to-number/12199/6 "2018-07-06T01:25:40Z")

</div>

It sounds as if you have the string. The length of that string (its a vanilla `string`, presumably) is `length(string)` that returns the number of characters. If your leading zeros are in the string, you are done. If your strings are implicitly left padded with zeros, then you know the length already, you are done. (`length(str::AbstractString}` is type stable)

---

<div class="post-metadata">

### Author: ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)
#### Post date: [July 6, 2018, 3:08am UTC](https://discourse.julialang.org/t/converting-bitstring-to-number/12199/7 "2018-07-06T03:08:36Z")

</div>

Not if I want to put that length in a type parameter, as I explained:

```julia
julia> struct Bits{N}; num::Int; end

julia> Bits(s) = Bits{length(s)}(parse(Int,s,2))
Bits

julia> Bits("110101")
Bits{6}(53)

julia> @code_warntype Bits("110101")
Variables:
  #self# <optimized out>
  s::String

Body:
  begin
      return ((Core.apply_type)(Main.Bits, $(Expr(:invoke, MethodInstance for length(::String), :(Main.length), :(s))))::Type{Bits{_}} where _)($(Expr(:invoke, MethodInstance for parse(::Type{Int64}, ::String, ::Int64), :(Main.parse), :(Main.Int), :(s), 2)))::Bits{_} where _
  end::Bits{_} where _

```

---

<div class="post-metadata">

### Author: ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)
#### Post date: [July 6, 2018, 3:11am UTC](https://discourse.julialang.org/t/converting-bitstring-to-number/12199/8 "2018-07-06T03:11:56Z")

</div>

Otherwise, does anyone have an idea with the `sub2ind` approach is slow?

```julia
julia> @btime bits2int(1,1,0,1,0,1)  
  1.403 μs (9 allocations: 240 bytes)
53                                   

```

edit: slow compared to sub2ind on its own, which is basically instantaneous.

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [July 6, 2018, 3:13am UTC](https://discourse.julialang.org/t/converting-bitstring-to-number/12199/9 "2018-07-06T03:13:38Z")

</div>

Is your goal to accept strings that are a sequence of `1`s and `0`s and then convert each string as it is available into the corresponding datapair  
(stringlength, integer value of parsed string)? and then keep it in a struct

---

<div class="post-metadata">

### Author: ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)
#### Post date: [July 6, 2018, 3:16am UTC](https://discourse.julialang.org/t/converting-bitstring-to-number/12199/10 "2018-07-06T03:16:36Z")

</div>

Yes, but the length should be a type parameter. Obviously, with a tuple input that length is very easy to obtain as a type parameter. I was wondering if that was possible to also do with a string.

I thought it might be possible with a macro by doing the computation of the length during compilation.

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [July 6, 2018, 3:24am UTC](https://discourse.julialang.org/t/converting-bitstring-to-number/12199/11 "2018-07-06T03:24:18Z")

</div>

```julia
julia> struct BitStrFun{L} # L is the parameter which gets the string length
           value::UInt
           
           function BitStrFun(str::String) where {L}
               value = parse(UInt, string("0b", str))
               nbits = length(str)
               return new{nbits}(value)
           end
       end

julia> a = BitStrFun("11011")
BitStrFun{5}(0x000000000000001b)

julia> a.value
0x000000000000001b

julia> a = BitStrFun("0000000000000101")
BitStrFun{16}(0x0000000000000005)

julia> struct BitStrFun2{L} # L is the parameter which gets the string length
           value::Int
           
           function BitStrFun2(str::String) where {L}
               value = parse(Int, string("0b", str))
               nbits = length(str)
               return new{nbits}(value)
           end
       end

julia> 

julia> a = BitStrFun2("0000000000000101")
BitStrFun2{16}(5)

julia> a.value
5

julia> nbits(x::BitStrFun2{L}) where {L} = L
nbits (generic function with 1 method)

julia> nbits(a)
16

```

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [July 6, 2018, 3:26am UTC](https://discourse.julialang.org/t/converting-bitstring-to-number/12199/12 "2018-07-06T03:26:47Z")

</div>

note: the above presupposes your strings have only ones and zeros (no leading `0b`); if the incoming strings are formatted with a leading `0b` just replace `value = parse(Int, string(..` with `value = parse(Int, str)`

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [July 6, 2018, 3:43am UTC](https://discourse.julialang.org/t/converting-bitstring-to-number/12199/13 "2018-07-06T03:43:35Z")

</div>

Yes, you can do this with a string macro:

```julia
julia> struct Bits{L}
         value::Int
       end

julia> macro bits_str(bits::String)
           quote
               Bits{$(length(bits))}($(parse(Int, bits, 2)))
           end
       end
@bits_str (macro with 1 method)

julia> bits"1001"
Bits{4}(9)

```

Since the length computation happens when the macro is expanded, the length is essentially baked into the resulting expression, and the result is type-stable:

```julia
julia> f() = bits"1001"
f (generic function with 1 method)

julia> @code_warntype f()
Variables:
  #self# <optimized out>

Body:
  begin 
      # meta: location REPL[1] # line 3:
      SSAValue(0) = $(Expr(:new, Bits{4}, 9))
      # meta: pop location
      return SSAValue(0)
  end::Bits{4}

```

---

<div class="post-metadata">

### Author: ![jebej](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jebej/32/1784_2.png) [@jebej](https://discourse.julialang.org/u/jebej)
#### Post date: [July 6, 2018, 2:11pm UTC](https://discourse.julialang.org/t/converting-bitstring-to-number/12199/14 "2018-07-06T14:11:06Z")

</div>

Great thanks! I’m not very experienced with the macro notation so this is helpful 🙂

---

<div class="post-metadata">

### Author: ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)
#### Post date: [February 13, 2022, 8:35am UTC](https://discourse.julialang.org/t/converting-bitstring-to-number/12199/15 "2022-02-13T08:35:14Z")

</div>

> [@rdeits](#):
>
> `Bits{$(length(bits))}($(parse(Int, bits, 2)))`

Maybe its a julia version issue, I modified this part into, to get it to work properly (julia v1.4):

```julia
Bits{$(length(bits))}($(parse(Int, bits; base=2)))

```
