Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mathematical equation for AND bitwise operation?

In a shift left operation for example,

5 << 1 = 10

10 << 1 = 20

then a mathematical equation can be made,

n << 1 = n * 2.

If there is an equation for a shift left operation, then is it possible that there is also a mathematical equation for an AND operation or any other bitwise operators?

like image 773
e19293001 Avatar asked May 21 '26 20:05

e19293001


2 Answers

There is no straightforward single operation that maps to every bitwise operation. However, they can all be simulated through iterative means (or one really long formula).

(a & b)

can be done with:

(((a/1 % 2) * (b/1 % 2)) * 1) +
(((a/2 % 2) * (b/2 % 2)) * 2) +
(((a/4 % 2) * (b/4 % 2)) * 4) +
...
(((a/n % 2) * (b/n % 2)) * n)

Where n is 2 to the number of bits that A and B are composed minus one. This assumes integer division (remainder is discarded).

like image 54
Variable Length Coder Avatar answered May 23 '26 09:05

Variable Length Coder


That depends on what you mean by "mathematical equation". There is no easy arithmetic one.

If you look at it from a formal number-theoretic standpoint you can describe bitwise "and" (and "or" and "xor") using only addition, multiplication and -- and this is a rather big "and" from the lay perspective -- first-order predicate logic. But that is most certainly not what you meant, not least because these tools are enough to describe anything a computer can do at all.

like image 38
hmakholm left over Monica Avatar answered May 23 '26 09:05

hmakholm left over Monica