arrays question

While I was programming I needed a memory of some sorts, that could hold numbers from 0 to 3. so I used an array because it was the simplest to programme. The programme worked as intended, but I found out that arrays can hold very big numbers and therefore take up a lot of memory (tried int order[1000]{}; and it took up 96% of the memory). so is it possible that an array can hold smaller numbers like 2 bit numbers (0-3)?

Thanks in advance.
(Sorry there is no code, I accidentally saved over a blank code, but it worked as intended)

You can use byte instead of int I'm sure. Half the size at 8 bits, although still a lot bigger than you need.

Well that is acctually 4 times smaller, so thanks, it really helps!!

andraz7:
Well that is acctually 4 times smaller, so thanks...

Not on 8-bit Arduinos, they have an int size of 2 bytes.

So what Arduino are you using?

Oops read it wrong it IS only twice smaller, but i figured you can use two boolean arrays to store a two bit number. It requires more coding but it uses less memory when you need an array to be extremly big.

BTW I'm using an ordinary arduino UNO.

Two boolean arrays take double the space, since they are really byte arrays.

If the data is constant it could be put to PROGMEM,
if it changes rarely it could be put to EEPROM (if it fits).

Well that's a bummer, but thanks anyway!!
At least this was just a question I was wondering and not a problem I was having.

It takes a little more coding, but you could use bit fields. If you only need the values 0-3 (i.e. two bits each), then you could pack 4 fields into a byte. Thus, a 1000 element array would only take up 250 bytes:

typedef struct {
  unsigned int field0 : 2;
  unsigned int field1 : 2;
  unsigned int field2 : 2;
  unsigned int field3 : 2;
} bitFields;

uint8_t getField(uint16_t);
void setField(uint16_t, uint8_t);

bitFields fieldArray[250];

void setup() {
  Serial.begin(115200);
  delay(1000);

  setField(0, 1);
  setField(100, 2);
  setField(375, 0);
  setField(721, 3);
  setField(801, 1);
  setField(999, 3);

  Serial.print("Element #  0 = ");Serial.println(getField(0));
  Serial.print("Element #100 = ");Serial.println(getField(100));
  Serial.print("Element #375 = ");Serial.println(getField(375));
  Serial.print("Element #721 = ");Serial.println(getField(721));
  Serial.print("Element #801 = ");Serial.println(getField(801));
  Serial.print("Element #999 = ");Serial.println(getField(999));  
}

void loop() {}

void setField(uint16_t index, uint8_t fieldValue) {
  uint8_t structIndex;
  bitFields *fPtr;

  fPtr = fieldArray + index / 4;
  structIndex = index % 4;
  switch (structIndex) {
    case 0:
      fPtr->field0 = fieldValue;
      break;

    case 1:
      fPtr->field1 = fieldValue;
      break;

     case 2:
      fPtr->field2 = fieldValue;
      break;

    case 3:
      fPtr->field3 = fieldValue;
      break;

    default:
      break;
  }  
}

uint8_t getField(uint16_t index) {
  uint8_t structIndex, returnVal;
  bitFields *fPtr;

  fPtr = fieldArray + index / 4;
  structIndex = index % 4;
  switch (structIndex) {
    case 0:
      returnVal = fPtr->field0;
      break;

    case 1:
      returnVal = fPtr->field1;
      break;

     case 2:
      returnVal = fPtr->field2;
      break;

    case 3:
      returnVal = fPtr->field3;
      break;

    default:
      returnVal = 0;
      break;
  }  
  return returnVal;
}

Note, this is minimalist code. It contains no error checking for going beyond the array boundaries or allowed element values. You could also change it to allow passing a pointer to the array to the setField / getField functions. That would allow them to operate on multiple instances.