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??
No. The closest thing one could probably get without becoming to far-fetched is to compare them to decimal operators:
&
operator could be compared to multiplication (0*0=0
,0*1=0
,1*1=1
)^
operator could be compared to carry-less addition (0+0=0
,0+1=1
,1+1=0
)|
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.
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.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:
Convert the two
int
operands into arrays of integers, where each element of each array represents a decimal digit in the correspondingint
.Perform a bitwise operation on the corresponding elements of each
int
array giving a 3rdint
array.Turn the 3rd
int
array back into anint
.(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.
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)
andb_2=(a_1&b_1)<<1
enough times (untilb_n==0
) then you'll get the result of the addition ina_n
.Well, you can rewrite any boolean operation in terms of NOT, OR, AND only, e.g.
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:
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
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.