How to create a 3-bit variable as a data type in C?

I can typedef char to CHAR1 , which is 8 bits. But how can I make a 3-bit variable as a data type?

+5
source share
3 answers

You might want to do something similar to the following:

 struct { . . . unsigned int fieldof3bits : 3; . . . } newdatatypename; 

In this case, fieldof3bits takes 3 bits in the structure (depending on how you define everything else, the size of the structure may vary).

This usage is called a bit field .

From Wikipedia:

The bit field is a term used in computer programming to store several logical adjacent bits, where each of the sets of bits and individual bits can be addressed. A bit field is most often used to represent integral types of known fixed bit widths.

+4
source

It seems you are asking for bitcodes https://en.wikipedia.org/wiki/Bit_field Just keep in mind that in some cases this can be safer, just use char or unsigned char instead of bits (compiler specific, physical memory layout, etc.) .d.).

Happy coding!

+1
source
 typedef struct { int a:3; }hello; 

This is only possible if it is inside an else structure, it is not

+1
source

All Articles