# How can I convert a bitstring to the binary form

**URL:** <https://discourse.julialang.org/t/how-can-i-convert-a-bitstring-to-the-binary-form/93134>\
**Category:** General Usage\
**Tags:** question, bitstring\
**Created:** [January 18, 2023, 8:31am UTC](https://discourse.julialang.org/t/how-can-i-convert-a-bitstring-to-the-binary-form/93134 "2023-01-18T08:31:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmtrung14](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmtrung14/32/45964_2.png) [@dmtrung14](https://discourse.julialang.org/u/dmtrung14)\
**Post date:** [January 18, 2023, 8:31am UTC](https://discourse.julialang.org/t/how-can-i-convert-a-bitstring-to-the-binary-form/93134/1 "2023-01-18T08:31:57Z")

</div>

I am using bitstring to perform an xor operation on the ith bit of a string:

```julia
string = bitstring(string ⊻ 1 <<i)

```

However the result will be a string, so I cannot continue with other i.

So I want to know how do I convert a bitstring (of the form “000000000000000000000001001”) to (0b1001)?

Thanks

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [January 18, 2023, 8:56am UTC](https://discourse.julialang.org/t/how-can-i-convert-a-bitstring-to-the-binary-form/93134/2 "2023-01-18T08:56:17Z")

</div>

You probably just shouldn’t call bitstring? But to answer your question:

```julia
julia> parse(UInt, "000000000000000000000001001", base = 2)
0x0000000000000009

```

---

<div class="post-metadata">

**Author:** ![dmtrung14](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmtrung14/32/45964_2.png) [@dmtrung14](https://discourse.julialang.org/u/dmtrung14)\
**Post date:** [January 18, 2023, 10:50am UTC](https://discourse.julialang.org/t/how-can-i-convert-a-bitstring-to-the-binary-form/93134/3 "2023-01-18T10:50:25Z")

</div>

I think your solution does work.  
The reason I call bitstring is to perform the \xor operation. Otherwise it won’t work on ::String

---

<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:** [January 18, 2023, 11:32am UTC](https://discourse.julialang.org/t/how-can-i-convert-a-bitstring-to-the-binary-form/93134/4 "2023-01-18T11:32:04Z")

</div>

> [@dmtrung14](#):
>
> The reason I call bitstring is to perform the \xor operation. Otherwise it won’t work on ::String

But why do you need the string? Can’t you just work directly on the numbers? Converting to string and back to numbers is slow.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [January 18, 2023, 12:05pm UTC](https://discourse.julialang.org/t/how-can-i-convert-a-bitstring-to-the-binary-form/93134/5 "2023-01-18T12:05:52Z")

</div>

There’s no method of `xor` which works with strings and you’re calling `bitstring` on the result of the `xor` anyway, so it’s hard to figure how `bitstring` helps you in any way here.

---

<div class="post-metadata">

**Author:** ![dmtrung14](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmtrung14/32/45964_2.png) [@dmtrung14](https://discourse.julialang.org/u/dmtrung14)\
**Post date:** [January 18, 2023, 1:23pm UTC](https://discourse.julialang.org/t/how-can-i-convert-a-bitstring-to-the-binary-form/93134/6 "2023-01-18T13:23:16Z")

</div>

Oh I just realized that it also works for numbers. I initially thought if I write without bitstring then the result will be interpreted in base 10, which would not work sequentially as desired!

---

<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:** [January 18, 2023, 2:54pm UTC](https://discourse.julialang.org/t/how-can-i-convert-a-bitstring-to-the-binary-form/93134/7 "2023-01-18T14:54:22Z")

</div>

The docstring says “Bitwise exclusive or”, but the examples are all on `Bool`s. Maybe there should be an update to the examples.
