-4

Is there a way to implement the Left Arithmetic Shift and Right Arithmetic Shift, using only operations AND, OR, NOT, XOR?

Hey
  • 13
  • 5

3 Answers3

2

In each of the operations AND, OR, NOT, and XOR, each bit in the result is solely a function of the one (OT) or two (AND, OR, XOR) bits in the same position in the operands. In a shift by any amount other than zero, each bit in the result is a function of a bit in a different position in the operand being shifted. Therefore, it is not possible to compute a shift solely from AND, OR, NOT, and XOR.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
1

Consider a = 0b0011.

Then we have ~a = 0b1100.

We also have a | ~a = 0b1111.

And also a & ~a = 0b0000.

You can manually check all possible combinations of &, ^, ~, and | to see that we can't make anything more than those four binary values. None of which are 0b0110 (what we want from left shift) or 0b0001 (what we want from right shift).

Since we found a number for which it can't be done, then we know in general it can't be done.

druckermanly
  • 2,694
  • 15
  • 27
  • The OP does not mention that constants are not allowed. If constants are allowed we can surely create other values and without having thought it completely through, I am quite sure we can construct shift operators. – nielsen Sep 07 '19 at 10:39
  • @nielsen: Constants do not perform shifts. If you select which constants to use using operations such as `? :` or control statements such as `if`, then you are not using just AND, OR, NOT, and XOR. – Eric Postpischil Sep 07 '19 at 12:11
  • @Eric Postpischil You are right, I was thinking more in terms of mathematics, but since the question is tagged `C` we must assume that the AND, OR, NOT and XOR operations are the `C` operations and that we are not allowed to pick out bits and use them as input in other positions (which would make the solution quite trivial since the shift operators really do not involve logic operations). Hence I believe that your answer is the most correct one. – nielsen Sep 07 '19 at 15:19
-1

IT IS POSSIBLE

Basically, the main logic of shifting is using shifters (barrel shifter and barrel shifter with multiplexers) which in turn can be created using multiplexers. And you can create multiplexers easily using the logical operations that you mentioned. Here is 2-to-1 MUX created with NAND gates: Mux with NAND

Miradil Zeynalli
  • 423
  • 4
  • 18