Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Differentiability of BitXor function

  1. Jul 4, 2011 #1
    in many programming languages there is a function of two variables called BitXor (which is also known as nim-sum, since it is used in solving de nim game) which represents each number as a string of its binary digits and then takes the Xor of each pair of terms, forming a new number.

    For Example :

    5*7= 1 0 1 * 1 1 1 (binary) =0 1 0 (binary) = 2

    (where '*' is the BitXor operator)

    and 2.5*1.5=10.1*1.1 (binary)=11(binary)=3

    Ok. So let f(x)=c*x (where '*' is the BitXor operator and c a real number).

    is f differentiable??
  2. jcsd
  3. Jul 4, 2011 #2


    User Avatar
    Science Advisor

    "BitXor" can be applied only to integers, not to general real numbers. Since the domain is discrete, the derivative cannot be defined and your question does not make sense.
  4. Jul 4, 2011 #3
    yes,I know that. but it's simple to see how to extend the function to all real numbers, that's why I gave the example 2.5*1.5=10.1*1.1(binary)=11(binary)=3. I don't see there is any problem in doing that, it seems pretty straight forward. we can call it the realbitxor function, or whatever.

    here is a code for the 'RealBitXor' function, written for mathematica:

    RealBitXor[x_, y_] := N[FromDigits[BitXor[First[#], Last[#]] &[
    First[RealDigits[Min[x, y],
    2]], {Last[RealDigits[Max[x, y], 2]] -
    Last[RealDigits[Min[x, y], 2]], 0}],
    First[RealDigits[Max[x, y], 2]]}
    ], 2]/2^Length[First[PadRight[
    First[RealDigits[Min[x, y],
    2]], {Last[RealDigits[Max[x, y], 2]] -
    Last[RealDigits[Min[x, y], 2]], 0}],
    First[RealDigits[Max[x, y], 2]]}
    ]]] - Last[RealDigits[Max[x, y], 2]]]

    not very tidy, but well... and I didn't take the signs into account, but it's correct for positive real numbers.

    if you graph the function f(x)=c*x, you will see that it is differentiable for some values of c; while it looks like a fractal for some others, which amazed me.

    for the particular case where c=0; f(x)=x and f'(x)=1. obvious.
    and for integer values of c, it seems to be differentiable by parts
    Last edited: Jul 4, 2011
  5. Jul 15, 2011 #4
    Between 0.0000 and 0.1111111....
    we can only express 2^n numbers. The only numbers we can express exactly in binary have 2^n in the denominator. To be on the safe side, the precision for the domain should be several more digits (3 binary digits?) than the precision for taking the derivative. What you've done may work well.

    I would trust a difference quotient, closely related to a derivative, more thoroughly. It is intended for discrete domains.

    I only have WolframAlpha.com, not Mathematica, but I'm sure the pictures/fractals are cool.
  6. Jul 19, 2011 #5
    It's an interesting idea. However, you need some additional work to make the definition consistent.
    For example, 1.000... and 0.1111... (in binary) both represent the same real number (1).
    Then 1.000... xor 0.111... = 1.111... = 2,
    but 0.111... xor 0.111... = 0.000 = 0, which is a contradiction.
  7. Jul 19, 2011 #6
    0.1111... = 1.0 ??
    The regular decimal system has the same issue. Considering computers don't store an infinite number of digits whether decimals or binary, this shouldn't be a major issue.
  8. Jul 19, 2011 #7
    I see your point, but I was considering the original question as a mathematical one (with the programming part just to confirm/disprove OP's hypothesis). Notions of continuity and differentiability don't make much sense to me when working with numbers represented by computers.
  9. Jul 19, 2011 #8

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    This is a huge issue, and it destroys the concept of differentiability of this function. A function that is nowhere continuous (and this one is) is not differentiable.

    That 0.111...2 = 1.02 introduces a huge ambiguity in the definition of this function. Example: What is c*x, where * is our "real bit xor" operator, for c=1/3, x=1/4? In binary, c is 0.01(01).... An ambiguity exists for x: do we represent it as 0.01 or 0.001(1)... ? We need to pick a rule for terminating binary numbers so that our function is well-defined. There are two possibilities:
    • Use the terminating representation for x.
      With this choice, c*x=0.01(01)...*0.01=0.0001(01)...=1/1210.
    • Use the repeating representation for x.
      With this choice, c*x=0.01(01)...*0.001(1)...=0.01(10)...=5/1210.

    With regard to differentiability, it doesn't really matter which choice is made. The function is discontinuous at x=1/4. A number slightly less than 1/4 will have a representation that starts with 0.0011, so 1/3 bitxor (1/4-ε) is approximately equal to 5/12. A number slightly greater than 1/4 will have a representation that starts with 0.0100, so 1/3 bitxor (1/4-ε) is approximately equal to 1/12. Make epsilon approach zero and those "approximately equal to" become exact. There is a discontinuity at x=1/4. Similarly, there is a discontinuity at every number that has a terminating binary representation. Since the set of numbers with a terminating binary representation is a dense subset of the reals, this function is nowhere continuous.
    Last edited: Jul 19, 2011
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook