I wanted to create a bit sized variable to save the only possible values 2^1 = 0 | 1
My initial approach was to create a class with variables of type int for storing the value 0 | 1
But then i found that i could also use a bitfield and also create my own struct with custom bits for each type. My question is that does using a struct with bits set to 1 provide better memory performance and hence faster implementation than the class approach for a array of struct like ( 4000 x 4000 )
The code :
#include<iostream>
using namespace std;
struct maze
{
unsigned int top : 1;
unsigned int right : 1;
unsigned int bottom : 1;
unsigned int left : 1;
};
int main()
{
maze access;
cout<<sizeof(access);
access.top=1;
access.right=1;
access.bottom=1;
access.left=1;
cout<<endl<<sizeof(access);
return 0;
}
Edit:
I think i have found the answer : https://stackoverflow.com/a/46544000/13868755