2

The bitwise operators AND and OR work as follows: "a AND b" is true if both A and B are true, and "a OR b" is false if both A and B are false. However, you can also preform these operations on numbers. If we represent "true" as "1" and "false" as "0", by converting a and b into binary, you can work out a AND b by comparing each binary digit and putting a 1 where both digits are 1, and work out a OR b by comparing each binary digit and putting a 0 where both digits are 0. The, the binary strings are converted back into decimal. I know how to work this out using the "column method" — writing the first binary number above the second and writing the answer underneath — but is there a formula I can use to plug in a and b and get a AND b? And, by extention, a formula for a OR b? Thanks in advance! (question edited for clarity)

The_Animator
  • 772
  • 3
  • 23
  • You should be able to control how a specific function is obtained - shouldn't you? Let's say this is a new variable. Then writing a general function is not difficult. – zkutch Dec 23 '23 at 21:20
  • @zkutch I think the closest I've gotten is that I think the formula might involve expressing each number as a sum of powers of two, but I'm not sure exactly how to do the rest. – The_Animator Dec 23 '23 at 21:23
  • 1
    Looks like homework. Starting with "Convert both numbers into binary", what both numbers? – Gyro Gearloose Dec 23 '23 at 21:23
  • @GyroGearloose This isn't actually homework. I just found out about how you could do operations like "43 AND 26" and I wondered what the specific formula was. – The_Animator Dec 23 '23 at 21:25
  • Sorry, but I have the impression that you did not pay attention to my question: you want to have a general function and get "and" or "or" from it. So you want to somehow control this process - right? – zkutch Dec 23 '23 at 21:30
  • @zkutch What do you mean by "control"? – The_Animator Dec 23 '23 at 21:32
  • General function should work in some cases as "and" and in other cases as "or" - correct? – zkutch Dec 23 '23 at 21:33
  • @zkutch Ah, I think that's where my question was unclear. I want to find 2 different formulas; one for AND and one for OR. – The_Animator Dec 23 '23 at 21:34
  • 1
    Would you like a recursive definition using $a\bmod 2$ and $\left\lfloor a/2\right\rfloor$? – peterwhy Dec 23 '23 at 21:38
  • 4
    In any case you should choose some operation for basic formula - which operation(s) you are allowed(want) to use? – zkutch Dec 23 '23 at 21:41
  • See also "Nim sum" ... https://en.wikipedia.org/wiki/Nimber#Addition – GEdgar Dec 23 '23 at 22:08

3 Answers3

0

Well, if you have positive integers $a,b$, it should be obvious how to chose $a_i,b_i\in \{0,1\}$ so that $a=\sum_i^\infty a_i 2^i$ and $b$ respectively.

Then you can compute $c_i= a_i+b_i\mod 2$ and get, hopefully that is want to see, $c=\sum_i^\infty c_i 2^i$, where the $c_i$ do not overlap.

0

Using the floor function $\lfloor x \rfloor$, which rounds $x$ down to the nearest integer, we can get the the $n$th binary digit (counting from the right) of an integer $a$ by

$$\left\lfloor \frac{a}{2^{n-1}}\right\rfloor -\left\lfloor \frac{a}{2^n}\right\rfloor2.$$

To AND the $n$th digits of $a$ and $b$ you just multiply:

$$\left(\left\lfloor \frac{a}{2^{n-1}}\right\rfloor -\left\lfloor \frac{a}{2^n}\right\rfloor2\right)\cdot\left(\left\lfloor \frac{b}{2^{n-1}}\right\rfloor -\left\lfloor \frac{b}{2^n}\right\rfloor2\right).$$

Now just make a binary number out of it: (The number of digits of the larger of $a$ and $b$ is given by $\log_2(a+b)+1$.)

$$a \mbox{ AND } b =\sum_{n=1}^{\log_2 (a+b)+1} \left(\left\lfloor \frac{a}{2^{n-1}}\right\rfloor -\left\lfloor \frac{a}{2^n}\right\rfloor 2\right)\cdot\left(\left\lfloor \frac{b}{2^{n-1}}\right\rfloor -\left\lfloor \frac{b}{2^n}\right\rfloor2\right )2^{n-1}.$$

To do OR, note that if $r$ and $s$ are binary digits, then

$$r \mbox{ OR } s = r+s - rs.$$

So we can OR the digits of $a$ and $b$ and make a binary number of out those:

$$a \mbox{ OR } b =\sum_{n=1}^{\log_2 (a+b)+1} \left(\left(\left\lfloor \frac{a}{2^{n-1}}\right\rfloor -\left\lfloor \frac{a}{2^n}\right\rfloor2\right)+\left(\left\lfloor \frac{b}{2^{n-1}}\right\rfloor -\left\lfloor \frac{b}{2^n}\right\rfloor2\right) -\left(\left\lfloor \frac{a}{2^{n-1}}\right\rfloor -\left\lfloor \frac{a}{2^n}\right\rfloor2\right)\cdot\left(\left\lfloor \frac{b}{2^{n-1}}\right\rfloor -\left\lfloor \frac{b}{2^n}\right\rfloor2\right)\right) 2^{n-1}. $$

0

(Not sure if this should be a comment, but posting as an answer.)

As B.Goddard has shown, a formula can be produced. But I hope you realize that absolutely no-one uses that to compute the bit-wise AND/OR of two numbers, and certainly not any computer. For the following reasons:

  • Computers already store numbers in binary. There's no "convert to binary" step

  • Computers are built on circuits that do AND and OR, so they would just do as you indicated with the binary numbers.

  • In fact, things like plus, times, and divide are implemented in terms of the logical operations, and are more complicated to carry out than AND and OR.

JonathanZ
  • 10,615