I could not understand how Union works..
#include <stdio.h>
#include <stdlib.h>
int main()
{
union {
int a:4;
char b[4];
}abc;
abc.a = 0xF;
printf(" %d, %d, %d, %d, %d, %d\n", sizeof(abc), abc.a, abc.b[0], abc.b[1], abc.b[2], abc.b[3]);
return 0;
}

In the above program.
I made int a : 4;
So, a should taking 4 bits.
now I am storing, a = 0xF; //i.e a= 1111(Binary form)
So when I am accessing b[0 0r 1 or 2 or 3] why the outputs are not coming like 1, 1, 1, 1
Your union’s total size will be at least
4 * sizeof(char).Assuming the compiler you are using handles this as defined behavior, consider the following:
abcis never fully initialized, so it contains a random assortment of zeros and ones. Big problem. So, do this first:memset(&abc, 0, sizeof(abc));00000000 00000000 00000000 0000000000000000 00000000 00000000 00001111or11110000 00000000 00000000 00000000.I’m not sure how your compiler handles this type of alignment, so this is the best I can do.
You might also consider doing a char-to-bits conversion so you can manually inspect the value of each and every bit in binary format:
Access individual bits in a char c++
Best of luck!