Yet another way, if only arithmetic right shift (>>) worked "properly": (-2>>x)&(-2>>y)&1 On 2014-04-17 15:10, Mike Speciner wrote:
Seems like people are choosing to ignore various words in the question. Words such as "unsigned". But of course, this is a somewhat ill-defined question. Are you asking for a given programming language? A given machine language? What do you want the result of the test to be? A boolean? 0 or 1? Something zero or nonzero? Your numbered points make it even less clear.
For example, on a pdp-11, multiplying (MUL) does indeed work, as it sets the Z flag based on the full result, whether or not it overflows a single word. (It treats the operands as signed, but that doesn't matter in this case.)
In python, x and y does what you might want, since the semantics are to return x if x evaluates false and otherwise return y. (Various objects evaluate false, such as: False, 0, and empty strings and lists.) Of course, internally lots of machine instructions get executed to make this happen.
Another idea is 0/x/y which will raise a divide-by-zero exception unless x and y are both nonzero.
--ms
On 2014-04-16 16:45, Warren D Smith wrote:
If x and y both are unsigned integers, and we want to tell whether (x>0 and y>0), then how can we do it?
1. naive method: test x>0 and (if not) then also y>0.
2. x*y>0, but can fail if multiply overflows.
3. The gnu superoptimizer found 44 nonobvious methods, each involving 4 instructions, at least one of which always is subtract-with-borrow and/or add-with-carry. I think the basic idea is: if you regard (x,y) as a 2-digit number, and subtract (1,1), then you will generate at least one borrow if and only if (x=0 or y=0).
4. ((-x)&(-y))>>31 should be nonzero if x>0 and y>0, provided neither x nor y was greater than 2^31 - 1. On a 32-bit machine.
_______________________________________________ math-fun mailing list math-fun@mailman.xmission.com http://mailman.xmission.com/cgi-bin/mailman/listinfo/math-fun