K
karthikbalaguru
Hi,
I have a query related to the printing of bit fields .
Code Set - 1
===========
#include<stdio.h>
int main(void)
{
struct dummy
{
unsigned int a : 1;
unsigned int b : 1;
unsigned int c : 1;
unsigned int d : 1;
unsigned int e : 1;
unsigned int f : 1;
unsigned int g : 1;
unsigned int h : 1;
};
struct dummy d1;
d1.a = 1;
d1.b = 1;
d1.c = 1;
d1.d = 1;
d1.e = 1;
d1.f = 1;
d1.g = 1;
d1.h = 1;
printf("%x \n",d1);
return 0;
}
For the above code, i got the output as :-
ccccccff
I was wondering of the occurance of C's
even after repeated execution and changed
the struct definition as below -
static struct dummy d1;
After the above change, i got the output
as ff .
Later,
I tried to change the code as below to check
with other datatypes for bit fields.
Code Set - 2
=============
#include<stdio.h>
int main(void)
{
struct dummy
{
char a : 1;
char b : 1;
char c : 1;
char d : 1;
char e : 1;
char f : 1;
char g : 1;
unsigned int h : 1;
};
static struct dummy d1;
d1.a = 1;
d1.b = 1;
d1.c = 1;
d1.d = 1;
d1.e = 1;
d1.f = 1;
d1.g = 1;
d1.h = 1;
printf("%x \n",d1);
return 0;
}
But, i got the output as 7f instead
of ff that i got for the 'Code Set - 1' ?
I do not have any bit fields as zero
in the 'Code Set - 2', But, how
did it gives the ouput as 7f ?
Further, can we use any kind of
datatype for declaring and using of
bit fields in a structure ?
Thx in advans,
Karthik Balaguru
I have a query related to the printing of bit fields .
Code Set - 1
===========
#include<stdio.h>
int main(void)
{
struct dummy
{
unsigned int a : 1;
unsigned int b : 1;
unsigned int c : 1;
unsigned int d : 1;
unsigned int e : 1;
unsigned int f : 1;
unsigned int g : 1;
unsigned int h : 1;
};
struct dummy d1;
d1.a = 1;
d1.b = 1;
d1.c = 1;
d1.d = 1;
d1.e = 1;
d1.f = 1;
d1.g = 1;
d1.h = 1;
printf("%x \n",d1);
return 0;
}
For the above code, i got the output as :-
ccccccff
I was wondering of the occurance of C's
even after repeated execution and changed
the struct definition as below -
static struct dummy d1;
After the above change, i got the output
as ff .
Later,
I tried to change the code as below to check
with other datatypes for bit fields.
Code Set - 2
=============
#include<stdio.h>
int main(void)
{
struct dummy
{
char a : 1;
char b : 1;
char c : 1;
char d : 1;
char e : 1;
char f : 1;
char g : 1;
unsigned int h : 1;
};
static struct dummy d1;
d1.a = 1;
d1.b = 1;
d1.c = 1;
d1.d = 1;
d1.e = 1;
d1.f = 1;
d1.g = 1;
d1.h = 1;
printf("%x \n",d1);
return 0;
}
But, i got the output as 7f instead
of ff that i got for the 'Code Set - 1' ?
I do not have any bit fields as zero
in the 'Code Set - 2', But, how
did it gives the ouput as 7f ?
Further, can we use any kind of
datatype for declaring and using of
bit fields in a structure ?
Thx in advans,
Karthik Balaguru