LeetCode --- --- 67. binary mathematical sum

I see this problem when the brain circuitry Qing Qi, has allowed involuntary thought-bit computing.

It can be calculated by fast & ^ and two operations. And stop calculating the condition evaluates to 0 & exit time, if not has been shifting, and ^ & computing. I also hard to describe the specific principles, but there are  official documents 

 public string AddBinary1(string a, string b)
        {
            var num_a = Convert.ToInt64(a, 2);
            var num_b = Convert.ToInt64(b, 2);
            // 0x 

            var and = num_a & num_b;
            var xor = num_a ^ num_b;
            while (and != 0)
            {
                and = and << 1;
                var tmpand = and & xor;
                xor = xor ^ and;
                and = tmpand;
            }

            return Convert.ToString((and + xor), 2);
        }

But be careful Oh, so written will not pass Oh, I'm here to convert int type but is subject to a string (can be very long), but for the range so much when you can use (¯ ▽ ¯) "

Published 71 original articles · won praise 89 · views 80000 +

Guess you like

Origin blog.csdn.net/u012371712/article/details/104620912