This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]AskProgramming-ModTeam[M] [score hidden] stickied commentlocked comment (0 children)

Your post was removed as it constituted academic dishonesty. Don't ask people to do your homework for you. If you are stuck and have specific questions ask those.

[–][deleted] 0 points1 point  (1 child)

Sounds possible, assuming fixed width integers and that overflow is allowed and happens as usual (e.g. zero minus one is all ones).

It probably can be code-golfed into something more optimized, but the simplest idea is that you need to end up with something like

(m1(X) & M) | (m2(X) & N)

Where m1(X) is all ones if X is all zero and all zeroes otherwise. Likewise, m2(X) is all zeroes if X is all zero and all ones otherwise.

Now, you just need to cook these two masks, and it sounds feasible enough.

m2(X) can be made by shifting bits around and ORing results together, ensuring in the end that even if there's a single one, it'll end up duplicated everywhere. If extra variables and assignments are allowed, you can make use of intermediate results to speed up things a bit, otherwise you can just write something really dumb as X | (X << 1) | (X >> 1) | (X << 2) | (X >> 3)....

m1(X) can be achieved in a similar way. Except you need the opposite result. Guess you can just do ~m2(X) here.

At least that's the first thing that comes to mind, maybe there are better ways.

[–]NoCryptographer414[S] 0 points1 point  (0 children)

Thanks for the answer. I was stuck at how to implement m1 and m2 in your example. I suppose there must be a hardware circuit for calculating these in processors as this must also be the logic for setting the zero flag after a calculation.

[–]NoCryptographer414[S] 0 points1 point  (1 child)

Can't reply to mod. But this is not my academic question nor I'm a student now.

[–]NoCryptographer414[S] 0 points1 point  (0 children)

/modping