Electronic – Bitwise shift operation from NAND

computer-architecturelogic-gatesnand

I'm building an 8-bit computer using the Ben Eater's videos and I would like to improve the ALU, e.g. add nand bitwise operation because every logic gates can be built from this.

But I ask myself if it would be possible to have shift (left/right) operation using this nand gate?

Best Answer

A shift operation like what a >> or << compiles to is done with a (barrel) shifter.

You can build one by using a decoder and a cross bar of links with AND gates connecting the verticals and the horizontals. Then the output of the decoder will travel diagonally.

Another option is to use muliplexers and have several layers:

schematic

simulate this circuit – Schematic created using CircuitLab

This is a 4 bit logical right-shift circuit. ShiftAmount0 is the least significant bit and shiftAmount1 is the next least significant bit.

You can grow this circuit by making the 1 input of the mux double the jump it makes. You can also connect the 1 inputs of the higher significant bits to the lower significant bits (for barrel shift) or the highest bit (for arithmetic shift). This you can do with a few AND gates to select the correct behavior.