Take a common out of the bitwise 'OR' operation - bit-manipulation

If we have an operation (xA | xB) where '|' is the 'OR' operator then what would be required to take the x out of the operation and get multiplied by the outcome of the left.
e.g., Let P = (xA | xB)
Then somehow, P = x * (some expression here)
Please let me know if this is possible and what would be the expression.

For this answer I'll assume 32bit integers (it's more general actually, but it doesn't work for ℤ).
It's possible, but this is not an optimization. Consider the case that x is odd:
x has a modular multiplicative inverse, let's call it inv(x). Now plug into the template
P = x * Expr, Expr = inv(x) * (x * A | x * B)
The resulting expression is x * inv(x) * (x * A | x * B) = x * A | x * B.
Consider the case that x is even. Observe that multiplying x by 2 makes x * A | x * B twice as big too, even though a nonlinear "OR" operation is involved, because multiplying by two is just a left shift and left shift distributes over OR.
Factor x into y * d where y is odd and d is a power of two. Now let the "evenness" come from the final multiplication by x, and write Expr = inv(y) * (y * A | y * B).
Plugging that back into the template we get y * d * inv(y) * (y * A | y * B), which simplifies to d * (y * A | y * B), which is x * A | x * B again.
This case is actually the general case, because d can be set equal to 1 for odd x.

Related

sympy - parametric limit

Say I would like to solve a parametric limit: in the following example, alpha > 0 is the parameter.
import sympy as sp
x = sp.symbols("x", real=True)
alpha = sp.symbols("alpha", real=True, positive=True, nonzero=True)
expr = (x * sp.exp(x) - sp.exp(2 * sp.sqrt(1 + x**2))) / (sp.exp(alpha * x) + x** alpha)
sp.limit(expr, x, sp.oo)
If I execute the code I get the result -oo, which is arguably incorrect.
If I were to compute this limit by hand I would look at the numerator and conclude that exp(2 * sp.sqrt(1 + x**2)) is of the same order of exp(2*x), which dominates x * exp(x). Similarly, looking at the denominator I would say that exp(alpha * x) dominates the term x**alpha.
Therefore, I can compute the limit of the function -exp((2 - alpha) * x). The correct result would be:
-oo for 0 < alpha < 2
-1 for alpha = 2
0 for alpha > 2
Is there an easy way to achieve this result with sympy?
This should be considered a bug in SymPy. I would suggest opening an issue about it https://github.com/sympy/sympy/issues.
Regarding what you are asking for in general, it isn't implemented yet. See https://github.com/sympy/sympy/issues/13312.

Can someone explain how the shorthand assignment operator actually works?

#include <iostream>
using namespace std;
int main()
{
int x=2,a=3,b=2;
x*=a/b;
cout<<x<<" ";
x=2;
x=x*a/b;
cout<<x;
return 0;
}
I am getting output as:
2
3
while in my opinion x*=a/b; and x=x*a/b; mean the same thing.
Can anybody explain this behaviour?
They are not quite the same.
x *= a / b is grouped as x *= (a / b) and a / b takes place in integer arithmetic (it's 1).
x = x * a / b is grouped as x = ((x * a) / b). the integer division has a less drastic and different effect.
With integer division: 3/2 is 1.
x*=a/b; is evaluated as x *= (a / b) so x *= 3 / 2 -> x *= 1.
x=x*a/b; is evaluated as x = (x * a) / b; so (2 * 3) / 3 -> 6 / 2 -> 3
I am getting output as: 2 3 while in my opinion x*=a/b; and x=x*a/b;
mean the same thing. Can anybody explain this behaviour?
x *= a / b;
// ^^^^^
This is integer division, the rest is discarded and therefore 3 / 2 is 1.
Therefore the expression x *= a / b is the same as x *= 1 which stays 2.
x = x * a / b;
On the other hand is evaluated as
x = (x * a) / b;
The result is then
x = (2 * 3) / 2;
becomes
x = 6 / 2;
which is 3
Per [expr.ass]/6 E1 *= E2 is exactly he same as E1 = E1 * E2. That does not mean that x*=a/b; is the same as x=x*a/b;. Since E2 is a/b, x*=a/b; is actually equivalent to x=x*(a/b); which does produce the same results.

How can you calculate a factor if you have the other factor and the product with overflows?

a * x = b
I have a seemingly rather complicated multiplication / imul problem: if I have a and I have b, how can I calculate x if they're all 32-bit dwords (e.g. 0-1 = FFFFFFFF, FFFFFFFF+1 = 0)?
For example:
0xcb9102df * x = 0x4d243a5d
In that case, x is 0x1908c643. I found a similar question but the premises were different and I'm hoping there's a simpler solution than those given.
Numbers have a modular multiplicative inverse modulo a power of two precisely iff they are odd. Everything else is a bit-shifted odd number (even zero, which might be anything, with all bits shifted out). So there are a couple of cases:
Given a * x = b
tzcnt(a) > tzcnt(b) no solution
tzcnt(a) <= tzcnt(b) solvable, with 2tzcnt(a) solutions
The second case has a special case with 1 solution, for odd a, namely x = inverse(a) * b
More generally, x = inverse(a >> tzcnt(a)) * (b >> tzcnt(a)) is a solution, because you write a as (a >> tzcnt(a)) * (1 << tzcnt(a)), so we cancel the left factor with its inverse, we leave the right factor as part of the result (cannot be cancelled anyway) and then multiply by what remains to get it up to b. Still only works in the second case, obviously. If you wanted, you could enumerate all solutions by filling in all possibilities for the top tzcnt(a) bits.
The only thing that remains is getting the inverse, you've probably seen it in the other answer, whatever it was, but for completeness you can compute it as follows: (not tested)
; input x
dword y = (x * x) + x - 1;
dword t = y * x;
y *= 2 - t;
t = y * x;
y *= 2 - t;
t = y * x;
y *= 2 - t;
; result y

Range Update - Range Query using Fenwick Tree

http://ayazdzulfikar.blogspot.in/2014/12/penggunaan-fenwick-tree-bit.html?showComment=1434865697025#c5391178275473818224
For example being told that the value of the function or f (i) of the index-i is an i ^ k, for k> = 0 and always stay on this matter. Given query like the following:
Add value array [i], for all a <= i <= b as v Determine the total
array [i] f (i), for each a <= i <= b (remember the previous function
values ​​clarification)
To work on this matter, can be formed into Query (x) = m * g (x) - c,
where g (x) is f (1) + f (2) + ... + f (x).
To accomplish this, we
need to know the values ​​of m and c. For that, we need 2 separate
BIT. Observations below for each update in the form of ab v. To
calculate the value of m, virtually identical to the Range Update -
Point Query. We can get the following observations for each value of
i, which may be:
i <a, m = 0
a <= i <= b, m = v
b <i, m = 0
By using the following observation, it is clear that the Range Update - Point Query can be used on any of the BIT. To calculate the value of c, we need to observe the possibility for each value of i, which may be:
i <a, then c = 0
a <= i <= b, then c = v * g (a - 1)
b <i, c = v * (g (b) - g (a - 1))
Again, we need Range Update - Point Query, but in a different BIT.
Oiya, for a little help, I wrote the value of g (x) for k <= 3 yes: p:
k = 0 -> x
k = 1 -> x * (x + 1) / 2
k = 2 -> x * (x + 1) * (2x + 1) / 6
k = 3 -> (x * (x + 1) / 2) ^ 2
Now, example problem SPOJ - Horrible Queries . This problem is
similar issues that have described, with k = 0. Note also that
sometimes there is a matter that is quite extreme, where the function
is not for one type of k, but it could be some that polynomial shape!
Eg LA - Alien Abduction Again . To work on this problem, the solution
is, for each rank we make its BIT counter m respectively. BIT combined
to clear the counters c it was fine.
How can we used this concept if:
Given an array of integers A1,A2,…AN.
Given x,y: Add 1×2 to Ax, add 2×3 to Ax+1, add 3×4 to Ax+2, add 4×5 to
Ax+3, and so on until Ay.
Then return Sum of the range [Ax,Ay].

Need clarification about this loop performing multiplication

int x, y; // x is a non-negative integer
p = 0;
while (x > 0)
{
if ( x % 2 == 1 )
p = p + y;
y = y*2;
x = x/2;
}
// p == a*b here
I understand that this loop finds the product of 'a' and 'b' using the algebra:
a * b = (1/2)a * 2b
but I don't understand the code:
if ( x % 2 == 1 )
p = p + y;
I was hoping someone could explain why 'p' is assigned 'p + y' on odd values of x.
while (x > 0) {
if (x % 2 == 1)
p = p + y;
y = y*2;
x = x/2;
}
imagine x = 4, y = 5
iterations:
x is even, y = 10, x = 2 (i.e. x can be divided, y should be doubled)
x is even, y = 20, x = 1
x is odd, p = 20, y = 40, x = 0 (i.e. x can not be divided anymore, y should be added to p)
x > 0 is false, loop ends
p = 4 * y
now imagine x is odd at the beginning, let's say x = 5, y = 2:
x is odd, p = 2, y = 4, x = 2
(5/2 = 2.5, new value of x will be rounded down, y should be added BEFORE it is doubled)
x is even, y = 8, x = 1
x is odd, p = 10, y = 16, x = 0
p = y + 4*y
that first y is the reason, adding it to the result before it is doubled (1 * y) is in this case equivalent to 0.5 * (2 * y)
Because these are integers, a / 2 will be an integer. If a is odd, that integer has been rounded down, and you’re missing one-half b in the next iteration of the loop, i.e. one whole b in the current iteration of the loop (since b [y] is doubled each time).
If x is odd, x = x/2 will set x to 0.5 less than x/2 (because integer division rounds it down). p needs to be adjusted to allow for that.
Think of multiplication as repeated addition, x*y is adding y together x times. It is also the same as adding 2*y together x/2 times. Conceptually it is somewhat unclear what it means if x is odd. For example, if x=5 and y=3, what does it mean to add 2.5 times? The code notices when x is odd, adds y in, then does the y=y*2 and x=x/2. When x is odd, this throws away the .5 part. So in this example, you add y one time, then x becomes 2 (not 2.5) because integer division throws away the fraction.
At the end of each loop, you will see that the product of the original x and y is equal to p + x*y for the current values of p, x, and y. The loop iterates until x is 0, and the result is entirely in p.
It also helps to see what is going on if you make a table and update it each time through the loop. These are the values at the start of each iteration:
x | y | p
----------
5 | 3 | 0
2 | 6 | 3
1 | 12 | 3
0 | 24 | 15
This works by observing that (for example) y * 10 = y * 8 + y * 2.
It's pretty much like doing multiplication on paper in school. For example, to multiply 14 x 21, we multiply one digit at a time (and shift left a place where needed) so we add 1x14 + 2 x 14 (shifted left one digit).
14
x 21
----
14
280
Here, we're doing pretty much the same thing, but working in binary instead of decimal. The right shifting has nothing to do with the numbers being odd, and everything to do with simply finding which bits in the number are set.
As we shift one operand right to find whether a bit is set, we also shift the other operand left, just like we add zeros to shift numbers left when doing arithmetic on paper in decimal.
So, viewing things in binary, we end up with something like:
101101
x 11010
--------
1011010
+ 101101000
+ 1011010000
If we wanted to, instead of shifting the operand right, we could just shift the mask left so instead of repeatedly anding with 1, we'd and with 1, then with 2, then with 4, and so on (in fact, it would probably make a lot more sense that way). For better or worse, however, in assembly language (where this sort of thing is normally done) it's usually a little easier to shift the operand and use a constant for the mask than load the mask in a register and shift it when needed.
You should rewrite x as 2*b+1 (assuming x is odd). Then
x*y = (2*b+1)*y = (2*b)*y + y = b*(2*y) + y = (x/2)*(2*y) + y
where (x/2) is meant to be the integer division. With the operation rewritten this way, you see the x/2, the 2y and the +y appear.