Unsigned Characters ,strings

Please help me with this program ,i have a final exam tomorrow and i literally don't know whats going to happen.


#include<stdio.h>
#include<conio.h>
struct byte1
{
unsigned char a13:1;
unsigned char a22:2;
unsigned char a64:3;
unsigned char a75:2;

};

union byteu
{
unsigned char c;
struct byte1 b;
};
void printbyte(char c)
{
byteu u;
u.c=c;
printf("%d",u.b.a13);
printf("%d",u.b.a22);
printf("%d",u.b.a64);
printf("%d",u.b.a75);

}
void main()
{
byteu u;

u.c=26;
u.b.a13=22;
u.b.a64=23;
u.b.a75=22;
u.b.a22=74;
u.b.a13=u.b.a64;
printbyte(u.c);
}


Please explain whats actually happening in the program.
This code plays a lot with bit order. It would help to write down each number in binary and see which bits are put where. Here I've done it for you:
1
2
3
4
5
6
7
//code            //binary value      //where it is put
u.c=26;           //0 0 0 1 1 0 1 0   00011010
u.b.a13=22;       //0 0 0 1 0 1 1 0          0
u.b.a64=23;       //0 0 0 1 0 1 1 1     111   
u.b.a75=22;       //0 0 0 1 0 1 1 0   10
u.b.a22=74;       //0 1 0 0 1 0 1 0        10
u.b.a13=u.b.a64;  //          1 1 1          1 
Topic archived. No new replies allowed.