Quantcast
Channel: First steps - JuliaLang
Viewing all articles
Browse latest Browse all 2795

Add two binary numbers

$
0
0

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

    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:]

4 posts - 4 participants

Read full topic


Viewing all articles
Browse latest Browse all 2795

Trending Articles