Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In case of bit fields, which one is better to use, unsigned char or unsigned int and why?

Tags:

People also ask

What are the important points to be considered when implementing bit fields in structures?

Bit fields can be used to reduce memory consumption when a program requires a number of integer variables which always will have low values. For example, in many systems storing an integer value requires two bytes (16-bits) of memory; sometimes the values to be stored actually need only one or two bits.

How many bits is an unsigned char?

The smallest group of bits the language allows use to work with is the unsigned char , which is a group of 8 bits.

What are Bitfields What is the use of bit fields in a structure declaration?

These space-saving structure members are called bit fields, and their width in bits can be explicitly declared. Bit fields are used in programs that must force a data structure to correspond to a fixed hardware representation and are unlikely to be portable.

Why bit field is used in C?

In programming terminology, a bit field is a data structure that allows the programmer to allocate memory to structures and unions in bits in order to utilize computer memory in an efficient manner. Since structures and unions are user-defined data types in C, the user has an idea of how much memory will they occupy.


I just want to know about following structure declarations. Which one is better to use for memory allocation and why? And what about padding in case of unsigned char and unsigned int?

struct data{
 unsigned char a:3;
 unsigned char b:4;
}; 

and

struct data{
 unsigned int a:3;
 unsigned int b:4;
};