Do operations that contain bit-wise AND (&) or bit

2019-07-09 04:24发布

Consider the following expressions in which both operand are decimal : a^b or a&b

I know what the operators do on binary digits of operands and hence i know how the answer of a^b or a&b is calculated. What I do not know is if those operations can be translated to decimal form

for example : we can say that a<<b is equivalent to this operation :a*pow(2,n)

Is a^b or a&b equivalent to anything like that??

5条回答
Explosion°爆炸
2楼-- · 2019-07-09 04:32

No. The closest thing one could probably get without becoming to far-fetched is to compare them to decimal operators:

  • The & operator could be compared to multiplication (0*0=0, 0*1=0, 1*1=1)
  • The ^ operator could be compared to carry-less addition (0+0=0, 0+1=1, 1+1=0)
  • The | operator could be compared to regular addition (0+0=0, 0+1=1, 1+1=2)

However, these comparisons obviously break down when applied to anything other than 0 or 1.

查看更多
你好瞎i
3楼-- · 2019-07-09 04:35

No. There is no "decimal" equivalent.

First of all, integers are integers ... not binary integers or decimal integers.

Second, the ^, & and | operators are defined (mathematically) in terms of their. effect on individual bits, so it is not even clear what a decimal equivalent might be.

For example : we can say that a<<b is equivalent to this operation: a*pow(2,n). Is a^b or a&b equivalent to anything like that??

In a word ... no.

(And besides, the example you have is nothing to do with binary versus decimal.)


The only thing I can think of that could be construed as the decimal equivalent of something like this:

  1. Convert the two int operands into arrays of integers, where each element of each array represents a decimal digit in the corresponding int.

  2. Perform a bitwise operation on the corresponding elements of each int array giving a 3rd int array.

  3. Turn the 3rd int array back into an int.

(Or you could do the same using strings rather than integer arrays ...)

However, it is not clear to me that this is either meaningful or ... what you are trying to achieve.

查看更多
Rolldiameter
4楼-- · 2019-07-09 04:54

The bit XOR operation a^b is like an addition without the carry.
The bit AND operation a&b is like computing presence of a carry.
Thus, you can get this not so useful relation (a^b) + ((a&b)<<1) == (a+b)
Not so useful because it transforms an addition into another more complex addition.
It's more useful when proceeding bit after bit.

Note that if you repeat this transformation on resulting operands a_2=(a_1^b_1) and b_2=(a_1&b_1)<<1 enough times (until b_n==0) then you'll get the result of the addition in a_n.

查看更多
孤傲高冷的网名
5楼-- · 2019-07-09 04:55

Well, you can rewrite any boolean operation in terms of NOT, OR, AND only, e.g.

  a XOR b = (a AND (NOT b)) OR ((NOT a) AND b)  
  a EQU b = (a AND b) OR ((NOT a) AND (NOT b))  
  a IMP b = (NOT a) OR b

etc. In partial case when a and b are bits (a is equal either to 0 or to 1 and so does b) you can put simple arithemtic equivalents for the operations:

    NOT a == 1 - a
  a AND b == a * b
   a OR b == a + b - a * b 
  (for XOR, see the formula above and put expressions for AND, NOT and OR)

In general case, however, logical operations (NOT, OR, AND) are performed on number's bits that's why there're no simple decimal equivalents for these operations

查看更多
走好不送
6楼-- · 2019-07-09 04:56

Bit operations (AND, OR, NOT, XOR) are logical extensions of Boolean algebra. Boolean algebra operates on binary values (true/false if you want). So no, there is no extension to decimal values, for this operators.

查看更多
登录 后发表回答