Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing rotate left using AND, NOT, and ADD operations

Tags:

assembly

I'm implementing a 16-bit bit shifter to rotate bits to the left by r. I only have access to the AND, NOT and ADD operations. There are 3 condition codes: negative, zero and positive, which are set when you use any of these operations.

This is my approach:

  1. AND the number with 1000 0000 0000 0000 to set condition codes to positive if the most significant bit is 1.
  2. ADD the number with itself. This shifts bits to the left by one.
  3. If the MSB was 1, ADD 1 to the result.
  4. Loop through steps (1)-(3) r times.

Are there any other efficient ways I can do this?

like image 532
Justin Avatar asked Mar 01 '11 02:03

Justin


1 Answers

Since this is homework, I'll help you think about it.

2 * 2 = 4
4 * 2 = 8
8 * 2 = 16

0010 * 0010 = 00100
0100 * 0010 = 01000
1000 * 0010 = 10000

A left shift is a [some unknown] operation. That [some unknown] operation can be implemented using AND, NOT and ADD by doing...

like image 129
Nick Larsen Avatar answered Oct 21 '22 16:10

Nick Larsen