Search

mardi 21 mars 2017

16-bit values: add / remove 1 (2)

Context

Note: read first part here.
As a reminder, we have 2 different structures, named "4 cells" and "3 cells" to represent 16-bit integers.
Let's now redefine instructions + and - on those 2 structures.

Structure 1 - "4 cells"

A, B, C, D to represent integer N; with
  • A = 0
  • B = 0
  • C and D so that N = D * 256 + C
Cursor on C

Structure 2 - "3 cells"

A, B, C to represent integer N; with
  • A and B so that N = B * 256 + A
  • C = 0
Cursor on A

We can see that these structures mean that we divided N by 256 and stored both quotient and remainder.

Add

To add 1, we need to increase remainder. And if null (if remainder was equal to 255), we just need to update the quotient as well.

Using 4 cells structure:
  • Increment C and set else bit in B
  • If C is not null, reset else bit in B
  • Move left (in A if C is not null, or B otherwise)
  • If current is not null (so, on B, with C = 0), then reset B, increment D, and back to A
  • Current position is A in all cases, move back to C
Using 3 cells structure (reminder: cell X before A is null as well, as it's the C from previous block):
  • Increment A
  • Copy A to C (using cell X on the left to restore A)
  • Set X to 1
  • If C is not null, reset C and X (and back to C)
  • If X is not null (so C was null, so A was null), reset X and increase B (and back to X)
  • Current position is X, move back to A

Remove

To remove 1, we need to decrease remainder. But before that, if it is null, then decrease quotient as well.

Using 4 cells structure:
  • Set else bit in B
  • If C is not null, reset else bit in B
  • Move left (in A if C is not null, or B otherwise)
  • If current is not null (so, on B, with C = 0), then reset B, decrease D, and back to A
  • Current position is A in all cases, move back to C and decrease.
Using 3 cells structure:
  • Copy A to C (using cell X on the left to restore A)
  • Set X to 1
  • If C is not null, reset C and X (and back to C)
  • If X is not null (so C was null, so A was null), reset X and decrease B (and back to X)
  • Current position is X, move back to A and decrease

Operation"4 cells""3 cells"
+(inc) +<+>[<-]<[->>+<<<]>> +[-<+>>>+<<]<[->+<]+>>>[[-]<<<->>>]<<<[->>+<<]>
-(dec) <+>[<-]<[->>-<<<]>>- [-<+>>>+<<]<[->+<]+>>>[[-]<<<->>>]<<<[->>-<<]>-

As mentioned initially, the 4 cells structure implements new instructions in a more compact way (but uses more cells)

1 commentaire:

  1. Top 5 best online casinos - Kadangpintar.com
    Best online casinos in Indonesia. Read reviews of the best online casinos. Discover casino games & promotions, sports 온카지노 가입쿠폰 betting, and live casino

    RépondreSupprimer