# Add two binary numbers

**URL:** https://discourse.julialang.org/t/add-two-binary-numbers/44556
**Category:** New to Julia
**Created:** [August 8, 2020, 9:04am UTC](https://discourse.julialang.org/t/add-two-binary-numbers/44556 "2020-08-08T09:04:07Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Fuad\_Sami](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fuad_sami/32/16900_2.png) [@Fuad\_Sami](https://discourse.julialang.org/u/Fuad_Sami)
#### Post date: [August 8, 2020, 9:04am UTC](https://discourse.julialang.org/t/add-two-binary-numbers/44556/1 "2020-08-08T09:04:07Z")

</div>

i need to add two binary number.

in the first place how i display a binary number in julia ? i try a lot things but doesn’t work  
like , `parse(Int,"111",2) or Int(0b110111)`  
in python the code is like that

```julia
    def addBinary(self, a, b) -> str:

        x,y = int(a,2),int(b,2) # **how i can do that in julia ??**
        if a=='0' : return b
        if b=='0' : return a
        while y:
            #x, y = x ^ y, (x & y) << 1
            result = x^y #XOR =(1+0)=1 , 1+1=0
            carry = (x&y) << 1 # just in 1+1 = 1 ,all zero # **in julia they have a and ?** 
            print(result , carry)
            x , y = result , carry
        return bin(x)[2:]

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [August 8, 2020, 11:16am UTC](https://discourse.julialang.org/t/add-two-binary-numbers/44556/2 "2020-08-08T11:16:47Z")

</div>

> [@Fuad\_Sami](#):
>
> i need to add two binary number.

How is it represented? If you are using `0b11` etc to construct it, that will just use the applicable `<:Unsigned` type, which of course has `+` defined in `Base`, so there is no need to do anything special.

See [Integers and Floating-Point Numbers · The Julia Language](https://docs.julialang.org/en/v1/manual/integers-and-floating-point-numbers/)

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [August 8, 2020, 1:13pm UTC](https://discourse.julialang.org/t/add-two-binary-numbers/44556/3 "2020-08-08T13:13:29Z")

</div>

`parse` and `Int` have nothing to do with displaying. Every number is a binary number, the syntax `0b110111` (base 2) make easy you pass the number `55` (base 10) to the program while looking at its binary representation, but it is a number like any other and it is represented in binary inside the computer.

Are you talking about strings representing binary numbers with characters `'0'` and `'1'`? To make them an `Int` you can use `parse(Int, "01011", base = 2)`. I do not know, however, if there is a standard library function in `Julia` that does transform the number in a `String` of `'0'` and `'1'` (base 2). The `Prinf.@sprintf` does not accept any format identifier that prints the number base 2.

---

<div class="post-metadata">

### Author: ![anon37204545](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon37204545](https://discourse.julialang.org/u/anon37204545)
#### Post date: [August 8, 2020, 2:33pm UTC](https://discourse.julialang.org/t/add-two-binary-numbers/44556/4 "2020-08-08T14:33:32Z")

</div>

> [@Fuad\_Sami](#):
>
> in python the code is like that
> 
> ```julia
> def addBinary(self, a, b) -> str:
> 
> x,y = int(a,2),int(b,2) # **how i can do that in julia ??**
> if a=='0' : return b
> if b=='0' : return a
> while y:
> #x, y = x ^ y, (x & y) << 1
> result = x^y #XOR =(1+0)=1 , 1+1=0
> carry = (x&y) << 1 # just in 1+1 = 1 ,all zero # **in julia they have a and ?** 
> print(result , carry)
> x , y = result , carry
> return bin(x)[2:]
> 
> ```

In Julia the code is like this:

```julia
julia> 0b1 + 0b11
0x04

```

In case you want to see it in the true binary representation, use `bitstring(n)`:

```julia
julia> bitstring(0b1 + 0b11)
"00000100"

```
