Compact boolean array ?

Go To Last Post
6 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello !

This is actually my first post on the forums, and not sure if this is the right section or not

I have a small problem :

I am using an ATmega324pa and need to have arbitrary long sequence of bits (well the size will not change after compilation, it will be known at compilation)

The closest thing to this would be the std_logic_vector from VHDL

In fact, i need something like :

#define ARRAY_SIZE 2048
boolArray myArray[ARRAY_SIZE];

and then, i coud do something like

#define ONE 0b1
#define ZERO 0b0

myArray[0] = ONE;

bool myValue = myArray[0];

I know I can simply make an array of bool, but a bool in avr-gcc is 8 bits ?

I have seen the https://www.avrfreaks.net/forum/c-packed-array-bool thread, but I wanted to know if theirs is something else like this, but for large arrays instead of just a uint8_t

I know I can simply make a wrapper around this and an array of uint8_t, but I just wanted to know if their was an alternative before starting to work on this.

 

Thank you verry much !

I hate coffee, so I take caffeine pill T.T

Last Edited: Sat. May 2, 2015 - 10:41 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can hold 2048 booleans 'packed' into a 256 byte array. You write a getbit() and setbit() function that takes the bitnumber 0-2047. Index into the array is bitnumber/8 and bit to set in the byte is bitnumber & 0x07.  I guess I can try an example... it might be wrong...

<

unsigned char buffer[256];

//------------------------------

void setbit(unsigned int n){

//set bit 0 to 2047

unsigned char ndx,bit;

 

  ndx = n >>3;

  bit = n & 0x07;

  buffer[ndx] |= (1 << bit);

>

 

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Allright ! Thank you verry much !

 

Oh, and now I feel dumb, didnt think about dooing n >>3; to replace n/8 or n &0x07 instead of n % 8, really smart ! Thank you again !

 

Here is what I have for now (didnt try it yet tho, I dont have any board on hand an no simulator :S)

For the setBit

 bool BitArray::setBit(uint16_t pos, bool value)
{
    if (pos >= SizeOfKey)
        return false;

    uint8_t realPos = pos >> 8;
    if (value == True_8)
        bitArray[realPos] |= True_8 << (pos & 0x07);
    else if (value == False_8)
        bitArray[realPos] &= ~(True_8 << (pos& 0x07));
    return true;
}

Where pos is the position (in bit) in the array, and value would be the value of that bit (1 or 0)

 

And for the getBit

 bool BitArray::getBit(uint16_t pos)
{
    uint8_t realPos = pos >> 3;
    uint8_t value = bitArray[realPos];
    uint8_t boolValue = value >> (pos & 0x07);
    return (boolValue & 0x01);
}

 

 

I hate coffee, so I take caffeine pill T.T

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
uint8_t realPos = pos >> 8;

Surely you mean >>3 or /8 not >>8? You only get 8 bits per byte not 256 (>>8 is effectively /256).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
Last Edited: Sun. May 3, 2015 - 08:49 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

uint8_t realPos = pos >> 8;

Surely you mean >>3 or /8 not >>8? You only get 8 bits per byte not 256 (>>8 is effectively /256).

 

Oups ! ya, i meant >>3

 

 

Yep, I saw it, I am trying to do something similary, but with operator overloading.

Thank you all

I hate coffee, so I take caffeine pill T.T