Home > Software design >  Is there a logical algorithm to generate unique outputs for the given inputs?
Is there a logical algorithm to generate unique outputs for the given inputs?

Time:07-03

I have a table of inputs that needs to output a unique formatted byte. The bytes outputted from the algorithm need to have only 1 bit on, giving 8 unique outputs. The inputs do not have to correlate with a specific output as long as each input has a unique output. The following is a list of all possible inputs and outputs.

Inputs: -00001000 -00001001 -00000001 -00000101 -00000100 -00000110 -00000010 -00001010

Outputs: -10000000 -01000000 -00100000 -00010000 -00001000 -00000100 -00000010 -00000001

I would like to know if there is a logical algorithm which is designed to do this. I'm currently using a lookup table for this which is not very optimized. I have access to all the operations used in 6502 assembly.

Edit: This is the code for my lookup table. It uses 18 bytes and 11 clock cycles. The obvious flaw is that you are wasting 2 of the bytes in the lookup table and that the lookup table itself uses 11 bytes.

LDX input     ;Zero Page
LDA lookup,X
STA output    ;Zero Page
lookup:
  .byte $00,$20,$02,$00,$08,$10,$04,$80,$40,$01

CodePudding user response:

I don't know the details of 6502, so this could or could not be efficient than a lookup table approach.

out = 1 << (in == 8 ? 7 : in & 7 ^ (in & 8) >> 2)

It has the following mapping for your given inputs.

in -> out (hex)
 8 -> 80
 9 -> 8
 1 -> 2
 5 -> 20
 4 -> 10
 6 -> 40
 2 -> 4
 a -> 1

The formula came out by scribbling random things on a paper, so not much logic.


Test

CodePudding user response:

Unfortunately, I do not think there is a more efficient method than a lookup table. Thanks to everyone who contributed their solutions. If anyone ever somehow has the same problem, just copy the code in the edit to the question.

  • Related