simple question I think?

Lets say you have 10, 4 digit binary sequences. And you want a different sequence to be output based on whether a variable is a 0,1,2,3,4,5,6,7,8 or 9. Can you do this in a table format? As opposed to 10 if then statements? and if so please say how. Just the rough strokes I can figure it out from there.

Create an integer array of 10 elements, and fill it with 10 numbers, with the values of the sequences you want.

Read the values back later with yourArray[yourVariable].

switch-case is normally used for this sort of thing, or data-driven using arrays.

Correct me if I'm missing something, here.

But if you have integer inputs in the range 0 - 9 then why not simply store these binary sequences as an array and use the integer to reference the array elements?

Anything else will only really complicate things - if you're looking for a way to programatically generate these sequences based on those integer values then you should look into bitwise operators. What are the binary sequences that you're returning and do they actually relate the the integers they're associated with?

Because if integer 0 returns 0000, and integer 1 returns 0001 and so on, in ascending order, you don't really need to return anything or even call a function since you'd be returning the same thing you're passing into the function. An integer already is a unique binary sequence, it's a matter of interpretation or presentation, really. If your binary sequences are in order but offset from something other than 0, some bit shifting might be useful.

As you have 10 binary sequences of 4 bits, tha maximum decimal value you can represent is 15 unless you're using some bespoke encoding/numbering scheme. Maybe I'm missing something but it just seems like you're trading a king for a king with overheads.

Use a 2D array

const uint16_t sequences[][4] = {
  {0x0001, 0x0002, 0x0003, 0x0004}, // sequence 0
  {0x1001, 0x0002, 0x0003, 0x0004}, // sequence 1
  {0x2001, 0x0002, 0x0003, 0x0004}, // sequence 2
  {0x3001, 0x0002, 0x0003, 0x0004}, // sequence 3
  {0x4001, 0x0002, 0x0003, 0x0004}, // sequence 4
  {0x5001, 0x0002, 0x0003, 0x0004}, // sequence 5
  {0x6001, 0x0002, 0x0003, 0x0004}, // sequence 6
  {0x7001, 0x0002, 0x0003, 0x0004}, // sequence 7
  {0x8001, 0x0002, 0x0003, 0x0004}, // sequence 8
  {0x9001, 0x0002, 0x0003, 0x0004}, // sequence 9
};

If index is your number from 0 to 9, you have the sequence at sequences[index][0] to sequences[index][3]

You could also use a 1D array of uint64_t if your binary sequence fits on 8 bytes. Just mind Endianness

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.